4 Replies - 1751 Views - Last Post: 25 May 2012 - 09:14 AM Rate Topic: -----

#1 dehlaryia1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 25-May 12

resize buttons according to the text inside the button

Posted 25 May 2012 - 08:07 AM

I am reading in information from a CSV file displaying them into a DataGrid table and then relabiling the column headings accrodingly. I am then takeing the column headings and creating buttons. I am having difficulty with getting the buttons to auto size in order to fit all of the text that is being displayed within the button. I am able to create the new buttons and display them where I want but they are either way to large for the text that has been placed in them or to small. Any input would be great!

Is This A Good Question/Topic? 0
  • +

Replies To: resize buttons according to the text inside the button

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5535
  • View blog
  • Posts: 11,861
  • Joined: 02-June 10

Re: resize buttons according to the text inside the button

Posted 25 May 2012 - 08:18 AM

http://msdn.microsof...y/6xe5hazb.aspx
Was This Post Helpful? 1
  • +
  • -

#3 dehlaryia1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 25-May 12

Re: resize buttons according to the text inside the button

Posted 25 May 2012 - 08:25 AM

View PosttlhIn`toq, on 25 May 2012 - 08:18 AM, said:



its Not homework I just can't figure out how to do it and I have been searching for almost an hour now and can't find anything that will work

 
dim g as graphics 
dim s as sizeF
s= g.measurestring(headerText,defaultFont)
newbutton.width = s.width
newbutton.height = s.height




but now I am getting an error with Object reference not set to an instance of an object. refering to the DefaultFont
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5535
  • View blog
  • Posts: 11,861
  • Joined: 02-June 10

Re: resize buttons according to the text inside the button

Posted 25 May 2012 - 08:46 AM

  • Nobody said anything about it being homework. So chill.
  • You should look at how to create a new graphics object - because you aren't doing it right. That's what is causing the error.
    http://msdn.microsof...y/5y289054.aspx


I've never done this before. All I did was read the MSDN for MeastureString and Make New Graphics and put the info together. Total time: 10 mins. I think you need to work on actually reading the available documentation, setting up your test code to mimic the documentation, and experiment a bit more before just giving up.

The margin variable exists just to adjust the spacing. The size of the string is how much space it will take up, but the button needs to be a bit larger so its available text space is big enough to accommodate it. With a bit more playing you could keep increasing the height and width until that space was perfect, but I didn't see the need for this example.

        private void btnMakeOtherButton_Click(object sender, EventArgs e)
        {
            string caption = "This is a really long string for a button";
            Graphics g;
            g = this.CreateGraphics();
            int margin = 3;

            SizeF stringSize = new SizeF();
            stringSize =g.MeasureString(caption, DefaultFont);

            Button newButton = new Button();
            newButton.Text = caption;
            newButton.Height = (int)stringSize.Height + 3*margin;
            newButton.Width = (int) stringSize.Width + margin;

            this.Controls.Add(newButton);

        }



Oh yeah - I did it in C#. You should be able to translate what I've done - or read the documentation pages I linked to you and assemble similar code. I've given you all the steps. This will give you a little of your own effort in the finished code.
Was This Post Helpful? 0
  • +
  • -

#5 dehlaryia1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 25-May 12

Re: resize buttons according to the text inside the button

Posted 25 May 2012 - 09:14 AM

That really help THANK YOU!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1