Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to change Label contrl's height and width runtime

How to change Label contrl's height and width runtime

Scheduled Pinned Locked Moved C#
tutorialquestion
5 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Software_Guy_123
    wrote on last edited by
    #1

    Hello, I would like label control with fixed width but length should increase as per the text data. OR run time how can I control label control's width and height depending on text of label control.

    Thanks & Regards, Kumar

    C 1 Reply Last reply
    0
    • S Software_Guy_123

      Hello, I would like label control with fixed width but length should increase as per the text data. OR run time how can I control label control's width and height depending on text of label control.

      Thanks & Regards, Kumar

      C Offline
      C Offline
      Corayzon
      wrote on last edited by
      #2

      Hello Kumar, If you would like your label's width to increase depending on the text property of the control, simply set the AutoSize property to true. By default in VS 2008 this property will be set to true

      S 1 Reply Last reply
      0
      • C Corayzon

        Hello Kumar, If you would like your label's width to increase depending on the text property of the control, simply set the AutoSize property to true. By default in VS 2008 this property will be set to true

        S Offline
        S Offline
        Software_Guy_123
        wrote on last edited by
        #3

        Hi Corayzon, This I had tried, but it is increasing form width also. I want form width should be fixed like 300 and label's text should be wrap to next line.

        Thanks & Regards, Kumar

        S 1 Reply Last reply
        0
        • S Software_Guy_123

          Hi Corayzon, This I had tried, but it is increasing form width also. I want form width should be fixed like 300 and label's text should be wrap to next line.

          Thanks & Regards, Kumar

          S Offline
          S Offline
          Software_Guy_123
          wrote on last edited by
          #4

          Hi, I have solved using Graphics.measurestring wih 3rd parameter as fixed width.

          Thanks & Regards, Kumar

          H 1 Reply Last reply
          0
          • S Software_Guy_123

            Hi, I have solved using Graphics.measurestring wih 3rd parameter as fixed width.

            Thanks & Regards, Kumar

            H Offline
            H Offline
            harschel
            wrote on last edited by
            #5

            Ya thanks Kumar, Measurestring really helped it. IF any one wants the code snippet : int iTop = 0; for (int icounter = 0; icounter < someCollectionObject.Count; icounter++) { Label lblctrl = new Label(); lblctrl.Name = "lbl" + icounter.ToString(); lblctrl.Image = "Put the image locaiton here " lblctrl.ImageAlign = ContentAlignment.TopLeft; string txtToShow = string.Empty; txtToShow = " " + someCollectionObject[icounter]; lblctrl.Text = txtToShow; lblctrl.Font = new System.Drawing.Font("Arial", 7f, System.Drawing.FontStyle.Bold); lblctrl.ForeColor = Color.LemonChiffon; lblctrl.Top = iTop; Graphics g = Graphics.FromHwnd(this.Handle); SizeF sz = g.MeasureString(txtToShow, lblctrl.Font, 100); lblctrl.Width = "You can have any constant width you wanted"; lblctrl.Height = (int)sz.Height -10; iTop += lblctrl.Height; }

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups