![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 223
Rep Power: 4
![]() |
Resizing fields to match text
Okay, so here is the situation,
I run my program, which populates a list box, all fine and good so far. What I'd like to do is resize the table grid columns to match (approximately) the data that was returned for the particular operation I used to fill the list box. Currently, I'm looking at the string lengths going into the text box, and then resizing if the size of the text going in is larger than the current width of the field, something like
if (drModel["Description"].ToString().Length*7 > dGTBCModel.Width)
dGTBCModel.Width = drModel["Description"].ToString().Length*7;this seems to work, however I'm not TOO happy with the multiplier, depending on what characters are in the field, and what font is used depends on how close this is to being large enough for the text, so... any other ways I could do this that would make it more accurately size the width of the field so that the text fits, but there isn't a vast white space? Any thoughts? |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 852
Rep Power: 4
![]() |
You can use the Graphics.MeasureString method to find the length of each string in pixels, then set the column width accordingly.
|
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 223
Rep Power: 4
![]() |
Dark,
Thanks, that helps some, although I'm getting totally lost with how to call Graphics.MeasureString, Seems I can't call it from anywhere but within a PaintEventArgs action block, which of course I don't have? Example, What I want to happen is the width of a field to be set to the length of the string in pixels, What i'd like to say is TextBox.Width = Graphics.MeasureString(TextBox.HeaderText,TextBox.Font) or something like that, however I can't work out how that would be done? Any ideas? |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 223
Rep Power: 4
![]() |
Okay nevermind, I just used the paint event on the datagrid to call since that has the painteventargs parameter.
Thanks |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|