当文本大于WinForms中c#中面板的大小时,如何增加面板的大小 [英] how to increase the size of panel when text is greater than the size of the panel in c# in WinForms

查看:74
本文介绍了当文本大于WinForms中c#中面板的大小时,如何增加面板的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个案例

当标签在面板内部,并且其中写有一些文字,然后标签显示最大面板尺寸的值。

使用自动后size property = true面板大小可以增加acc。只有表格大小,宽度明智。



现在我的问题是当在面板内输入文字时(面板宽度固定)并且文字大于面板尺寸然后下一个字母表将自动发送到面板中的下一行(而不是隐藏面板后面的下一个字符)。我的意思是面板的高度相应增加,文字的大小没有增加宽度。







这些都是WINDOWS形式。



非常感谢任何逻辑或片段



有疑问的问题然后请问它

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

推荐答案

我建​​议你使用 SizeChanged事件 [ ^ ]。在此事件内部检查带有Label控件并在字符串的适当位置添加制动线字符。要测量文本宽度,可以使用 TextRenderer.MeasureText方法 [ ^ ]。



读取文字,标签和面板宽度的示例代码:

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:
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());
}





这部分是为了开始。其余属于你;)







好​​的,我会告诉你如何实现。这很简单....

1)创建新项目(Windows应用程序)。

2)表格上的下划线:

     - 1个按钮(button1),

    - 1个面板(panel1)

3)添加新类(将文件名更改为: MultiLineLabel.cs );复制并粘贴下面的代码



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



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

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)复制并粘贴下面的代码以形成类( form1。 cs


4) copy and paste below code to form class (form1.cs)

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)运行应用程序;)现在您的标签具有新功能:multiline property并将其大小更改为不大于父控件的宽度;)





注意:代码未优化!

[/ EDIT]


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]


这篇关于当文本大于WinForms中c#中面板的大小时,如何增加面板的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆