|
-
May 5th, 2004, 07:43 AM
#1
Thread Starter
Hyperactive Member
Adding Controls at Runtime **SOLVED**
Hey all,
I am adding some LinkedLabels to my Form at runtime.
If I am adding a text to it:
Code:
LinkLabel myLabel = new LinkLabel();
myLabel.Text=fileName;
and next I want to set the width Object to exactly the length of the text, how would I do that?
Code:
myLabel.Width = myLabel.Text.Length;
does not work, since text.length only returns the number of Characters contained in that string.
Any help is appreciated,
Thanks,
Stephan
Last edited by Sgt-Peppa; May 5th, 2004 at 10:25 AM.
Keep Smiling - even if its hard 
Frankie Says Relax, wossname Says Yeah!
wossname:--Currently I'm wearing a gimp suit and a parachute.
C# - Base64 Blog
-
May 5th, 2004, 09:26 AM
#2
Fanatic Member
Look up Graphics.MeasureString. May be more work than you feel like putting into it though. If the string size isn't truly random (i.e., it tends to fit within a set of parameters) you could do some design time measurements and work from there.
-
May 5th, 2004, 10:02 AM
#3
Thread Starter
Hyperactive Member
How can I use the Graphics class?
This aint working
Code:
System.Drawing.Graphics myGraphics = new Graphics();
System.Drawing.SizeF mySize = myGraphics.MeasureString(myLabel.Text,myLabel.Font);
myLabel.Width = Int32.Parse(mySize.Width.ToString());
Good I feel like a beginner,.....
Thanks Stephan
Keep Smiling - even if its hard 
Frankie Says Relax, wossname Says Yeah!
wossname:--Currently I'm wearing a gimp suit and a parachute.
C# - Base64 Blog
-
May 5th, 2004, 10:25 AM
#4
Thread Starter
Hyperactive Member
Oh, OK I got it,
Code:
System.Drawing.Graphics myGraphics = Graphics.FromHwnd(System.IntPtr.Zero);
System.Drawing.SizeF mySize = myGraphics.MeasureString(myLabel.Text,myLabel.Font);
myLabel.Width = (int)mySize.Width +1;
this works, thanks,
Stephan
Keep Smiling - even if its hard 
Frankie Says Relax, wossname Says Yeah!
wossname:--Currently I'm wearing a gimp suit and a parachute.
C# - Base64 Blog
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|