Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 2nd, 2005, 12:15 PM   #1
Arla
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 223
Rep Power: 4 Arla is on a distinguished road
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?
Arla is offline   Reply With Quote
Old Aug 2nd, 2005, 9:11 PM   #2
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 852
Rep Power: 4 The Dark is on a distinguished road
You can use the Graphics.MeasureString method to find the length of each string in pixels, then set the column width accordingly.
The Dark is offline   Reply With Quote
Old Aug 4th, 2005, 11:01 AM   #3
Arla
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 223
Rep Power: 4 Arla is on a distinguished road
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?
Arla is offline   Reply With Quote
Old Aug 4th, 2005, 1:56 PM   #4
Arla
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 223
Rep Power: 4 Arla is on a distinguished road
Okay nevermind, I just used the paint event on the datagrid to call since that has the painteventargs parameter.

Thanks
Arla is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:53 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC