Click here to Skip to main content
15,912,493 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a case
When label is inside the panel, and some text is written inside it then label show the value up to panel size.
After using auto size property = true panel size can be increased acc. to Form size, width wise only.

My question is now that when text is entered inside panel(where panel width is fixed) and text is greater than the panel size then the next alphabet will automatically send to the next line (instead of hiding next characters behind panel) in panel. I mean height of the panel increased accordingly to the size of the text not width increased.



This is all in WINDOWS FORM.

Any logic or snippet is greatly appreciated

Any doubt in question then plz ask it
Posted
Updated 28-Jan-21 1:32am
v2

I would suggest you to use SizeChanged event[^]. Inside this event check the with of Label control and add brake the line character in proper place of string. To measure the text width, you can use TextRenderer.MeasureText method[^].

Example code to read text, label and panel width:
C#
private void CmdAddText2Label_Click(object sender, EventArgs e)
{
    label1.Text += "Veeeeerrryyyyy Loooooonnnnngggggg Teeeexxxxxxtttttt!";
}

private void label1_SizeChanged(object sender, EventArgs e)
{
    Label lbl = (Label)sender;
    String s = lbl.Text;
    Font f = lbl.Font;
    //calculate the length of string
    Size txtSize = TextRenderer.MeasureText(s, f);
    MessageBox.Show("Width of: \r\n - Text: " + txtSize.Width.ToString() + "\r\n - Label: " + lbl.Width.ToString() + "\r\n - Panel: " + panel1.Width.ToString());
}


This part is for start. The rest belongs to you ;)


[EDIT]
OK, i'll show you how to achieve. It's quite simple....
1) create new project (Windows application).
2) drop on the form:
   - 1 button (button1),
   - 1 panel (panel1)
3) add new class (change the file name to: MultiLineLabel.cs); copy and paste below code
C#
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace WindowsApplication1
{
    class MultiLineLabel : System.Windows.Forms.Label 
    {
        bool mline = false;

        public bool MultiLine
        {
            get {return mline;}
            set {mline = value;}
        }
        
        protected override void  OnResize(EventArgs e)
        {
            int tw = 0;
            int i = 0;
            String s = base.Text;
            String c = String.Empty;
            Font f = base.Font;
            Control p = base.Parent;
            //get the width of label
            int lw = base.Width;
            //get the with of parent control
            int cw = p.Width;
            StringBuilder sb = null;
            if(mline ==true )
            {
                if (lw>cw)
                {
                    while (tw < cw)
                    {
                        i += 1;  
                        c = s.Substring(0, i);
                        //calculate the length of string
                        Size txtSize = TextRenderer.MeasureText(c, f);
                        //get the width of text 
                        tw = txtSize.Width;
                    }
                    //decrease the length of text 
                    i -= 1;
                    //insert brakes
                    sb = new StringBuilder();
                    int j = 0;
                    while (j < s.Length) 
                    {
                        if (j + i > s.Length) i = s.Length - j;
                        c = s.Substring(j, i); //+ "/r/n";
                        sb.AppendLine (c);
                        j += i;
                    }
                    base.Text = sb.ToString();
                }
            }
            base.OnResize(e);
        }

    }
}

4) copy and paste below code to form class (form1.cs)
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        MultiLineLabel ml = new MultiLineLabel();
        public Form1()
        {
            InitializeComponent();
            ml.Parent = this.panel1;
            ml.AutoSize = true;
            ml.MultiLine = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            const String s = "Veeeerrrryyyyy Loooooonnnngggggg TTTeeeeeeexxxxxxxtttttt";
            ml.Text += s;
        }

    }
}

5) run the application ;) Now your label has new functionality: multiline property and changes its size to the width not greater than parent control ;)


Note: the code is not optimized!
[/EDIT]
 
Share this answer
 
v2
Comments
binadi007 9-Aug-13 11:49am    
Thankx for it....but can u tell me if the text size is greater than the size of panel then how to transfer half text to the next line (the text which is not visible)
Maciej Los 9-Aug-13 15:07pm    
See my answer now ;)
binadi007 9-Aug-13 15:26pm    
Yes thankx for it
But there is a little problem in that when I make panel width to 127, height to 100 then o/p be

Veeeerrrryyyyy Loooooon
nnngggggg TTTeeeeee
exxx
xxxxtttttt

why last two lines be different even it can able to come in one line
Maciej Los 10-Aug-13 8:24am    
Debug the code and check why it happens... Just try to correct it yourself.

By The Way, in this thread: Split one line into many lines in c#[^] you should mark my answer as a solution (green button).
binadi007 10-Aug-13 10:49am    
One more thing i want to ask in this program. Is it possible that the word which is not fully able to come in one line can go to the next line. If that single word is greater than the width of the panel then only the other part of the word to be continued to the next line else that word to be shifted to the next line.
Any logic behind it or snippet regarding it
The Answer is very simple and easy
C#
 public Config_CardItem()
        {
            InitializeComponent();
lblProductName.AutoSize=true;
}
private void lblProductName_Resize(object sender, EventArgs e)
        {
            try
            {
                if (lblProductName.Height > pnl.Height)
                {
                   pnl.Height = lblProductName.Height;
                }
               
            }
            catch(Exception ex) {MessageBox.Show(ex.Message); }
        }
 
Share this answer
 
v2
Comments
CHill60 28-Jan-21 7:41am    
Reason for my vote of 1 (apart from the fact the question was answered over 7 years ago)
- you have code "floating" in the middle of nowhere - where does
lblProductName.AutoSize=true;
actually belong?
- you have a try with an empty catch block - that is very bad practice
- you have an empty if-clause - the following means the same thing, is much neater and doesn't look as if something has been forgotten
if (lblProductName.Height > pnl.Height)
{
    pnl.Height = lblProductName.Height;
}
Naeem Shah 28-Jan-21 7:50am    
updated my answer and thanks for your good sugesstion
CHill60 28-Jan-21 12:04pm    
That is better. I have changed my vote upwards

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900