Results 1 to 4 of 4

Thread: Adding Controls at Runtime **SOLVED**

  1. #1

    Thread Starter
    Hyperactive Member Sgt-Peppa's Avatar
    Join Date
    Mar 2003
    Location
    Munich - Germany
    Posts
    476

    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

  2. #2
    Fanatic Member
    Join Date
    May 2002
    Posts
    746
    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.

  3. #3

    Thread Starter
    Hyperactive Member Sgt-Peppa's Avatar
    Join Date
    Mar 2003
    Location
    Munich - Germany
    Posts
    476
    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

  4. #4

    Thread Starter
    Hyperactive Member Sgt-Peppa's Avatar
    Join Date
    Mar 2003
    Location
    Munich - Germany
    Posts
    476
    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
  •  



Click Here to Expand Forum to Full Width