Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Friday, September 2, 2016

Word wrap for a label in Windows Forms

Word wrap for a label in Windows Forms


How can we get word wrap functionality for a label in Windows Forms?

I placed a label in a panel and added some text to label dynamically. But it exceeds the panel length. How can I solve this?

Answer by Jonathan C Dickinson for Word wrap for a label in Windows Forms


The quick answer: switch off AutoSize.

The big problem here is that the label will not change its height automatically (only width). To get this right you will need to subclass the label and include vertical resize logic.

Basically what you need to do in OnPaint is:

  1. Measure the height of the text (Graphics.MeasureString).
  2. If the label height is not equal to the height of the text set the height and return.
  3. Draw the text.

You will also need to set the ResizeRedraw style flag in the constructor.

Answer by hypo for Word wrap for a label in Windows Forms


From MSDN, Automatically Wrap Text in Label:

using System;  using System.Text;  using System.Drawing;  using System.Windows.Forms;    public class GrowLabel : Label {      private bool mGrowing;      public GrowLabel() {          this.AutoSize = false;      }      private void resizeLabel() {          if (mGrowing)               return;          try {              mGrowing = true;              Size sz = new Size(this.Width, Int32.MaxValue);              sz = TextRenderer.MeasureText(this.Text, this.Font, sz, TextFormatFlags.WordBreak);              this.Height = sz.Height;          }          finally {              mGrowing = false;          }      }      protected override void OnTextChanged(EventArgs e) {          base.OnTextChanged(e);          resizeLabel();      }      protected override void OnFontChanged(EventArgs e) {          base.OnFontChanged(e);          resizeLabel();      }      protected override void OnSizeChanged(EventArgs e) {          base.OnSizeChanged(e);          resizeLabel();      }  }  

Answer by John Gietzen for Word wrap for a label in Windows Forms


Actually, the accepted answer is unnecessarily complicated.

If you set the label to AutoSize, it will automatically grow with whatever text you put in it. (This includes vertical growth.)

If you want to make it word wrap at a particular width, you can set the MaximumSize property.

myLabel.MaximumSize = new Size(100, 0);  myLabel.AutoSize = true;  

Tested and works.

Answer by Sebastian Castaldi for Word wrap for a label in Windows Forms


Bad news: there is not an autowrap property.

Good news: there is a light at the end of the tunnel!

You could accomplish this programmatically to size it dynamically, but here is the easiest solution:

  • Select the properties of the label
  • AutoSize = True
  • MaximumSize = (Width, Height) where Width = max size you want the label to be and Height = how many pixels you want it to wrap

    Sample Properties

Answer by Sweety Jain for Word wrap for a label in Windows Forms


Use style="overflow:Scroll" in the label as in the below HTML. This will add the scroll bar in the label within the panel.

  

Answer by alex555 for Word wrap for a label in Windows Forms


In my case (label on a panel) I set label.AutoSize = false and label.Dock = Fill. And the label text is wrapped automatically.

Answer by binki for Word wrap for a label in Windows Forms


If your panel is limiting the width of your label, you can set your label?s Anchor property to Left, Right and set AutoSize to true. This is conceptually similar to listening for the Panel?s SizeChanged event and updating the label?s MaximumSize to a new Size(((Control)sender).Size.Width, 0) as suggested by a previous answer. Every side listed in the Anchor property is, well, anchored to the containing Control?s respective inner side. So listing two opposite sides in Anchor effectively sets the control?s dimension. Anchoring to Left and Right sets the Control?s Width property and Anchoring to Top and Bottom would set its Height property.

This solution, as C#:

label.Anchor = AnchorStyles.Left | AnchorStyles.Right;  label.AutoSize = true;  

Answer by noelicus for Word wrap for a label in Windows Forms


  1. Put the label inside a panel
  2. Handle the ClientSizeChanged event for the panel, making the label fill the space:

    private void Panel2_ClientSizeChanged(object sender, EventArgs e)  {      label1.MaximumSize = new Size((sender as Control).ClientSize.Width - label1.Left, 10000);  }  
  3. Set Auto-Size for the label to true

  4. Set Dock for the label to Fill

Answer by Kjell Verbeke for Word wrap for a label in Windows Forms


If you really want to set the label width independent of the content, I find that the easiest way is this:

  • Set autosize true
  • Set maximum width to how you want it
  • Set minimum width identically

Now the label is of constant width, but it adapts its height automatically.

Then for dynamic text, decrease the font size. If necessary, use this snippet in the sub where the label text is set:

If Me.Size.Height - (Label12.Location.Y + Label12.Height) < 20 Then      Dim naam As String = Label12.Font.Name      Dim size As Single = Label12.Font.SizeInPoints - 1      Label12.Font = New Font(naam, size)  End If  

Answer by user3356581 for Word wrap for a label in Windows Forms


I had to find a quick solution, so I just used a TextBox with those properties:

var myLabel = new TextBox                      {                          Text = "xxx xxx xxx",                          WordWrap = true,                          AutoSize = false,                          Enabled = false,                          Size = new Size(60, 30),                          BorderStyle = BorderStyle.None,                          Multiline =  true,                          BackColor =  container.BackColor                      };  

Answer by Mic for Word wrap for a label in Windows Forms


This helped me in my Form called InpitWindow: In Designer for Label:

AutoSize = true;  Achors = Top, Left, Right.    private void InputWindow_Shown(object sender, EventArgs e) {      lbCaption.MaximumSize = new Size(this.ClientSize.Width - btOK.Width - btOK.Margin.Left - btOK.Margin.Right -          lbCaption.Margin.Right - lbCaption.Margin.Left,           Screen.GetWorkingArea(this).Height / 2);      this.Height = this.Height + (lbCaption.Height - btOK.Height - btCancel.Height);      //Uncomment this line to prevent form height chage to values lower than initial height      //this.MinimumSize = new Size(this.MinimumSize.Width, this.Height);  }  //Use this handler if you want your label change it size according to form clientsize.  private void InputWindow_ClientSizeChanged(object sender, EventArgs e) {      lbCaption.MaximumSize = new Size(this.ClientSize.Width - btOK.Width - btOK.Margin.Left * 2 - btOK.Margin.Right * 2 -          lbCaption.Margin.Right * 2 - lbCaption.Margin.Left * 2,          Screen.GetWorkingArea(this).Height / 2);  }  

Whole code of my form

Answer by rjain for Word wrap for a label in Windows Forms


If the dimensions of the button need to be kept unchanged:

myButton.Text = "word\r\nwrapped"  

Answer by Pragmateek for Word wrap for a label in Windows Forms


Not sure it will fit all use-cases but I often use a simple trick to get the wrapping behaviour: put your Label with AutoSize=false inside a 1x1 TableLayoutPanel which will take care of the Label's size.

Answer by march1993 for Word wrap for a label in Windows Forms


Have a better one based on @hypo 's answer

public class GrowLabel : Label {      private bool mGrowing;      public GrowLabel() {          this.AutoSize = false;      }      private void resizeLabel() {          if (mGrowing)              return;          try {              mGrowing = true;              int width = this.Parent == null ? this.Width : this.Parent.Width;                Size sz = new Size(this.Width, Int32.MaxValue);              sz = TextRenderer.MeasureText(this.Text, this.Font, sz, TextFormatFlags.WordBreak);              this.Height = sz.Height + Padding.Bottom + Padding.Top;          } finally {              mGrowing = false;          }      }      protected override void OnTextChanged(EventArgs e) {          base.OnTextChanged(e);          resizeLabel();      }      protected override void OnFontChanged(EventArgs e) {          base.OnFontChanged(e);          resizeLabel();      }      protected override void OnSizeChanged(EventArgs e) {          base.OnSizeChanged(e);          resizeLabel();      }  }  

int width = this.Parent == null ? this.Width : this.Parent.Width; here allows you to use auto-grow label when docked to a parent, e.g. a panel.

this.Height = sz.Height + Padding.Bottom + Padding.Top; here considers padding top and bottom.


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.