简体   繁体   English

根据 C# 中的长度更改标签文本行

[英]Changing Label Text line depending on Length in C#

So I am developing an application in WinForms and I am populating data from an Access database into a Combobox .因此,我正在WinForms开发一个应用程序,并将数据从 Access 数据库填充到Combobox After populating it I will use the items from the Combobox to display data on my labels.填充后,我将使用Combobox的项目在我的标签上显示数据。 This is what I have to populate data:这是我必须填充数据的内容:

public void AutoCompleteBrand()
{
    OleDbConnection con = new OleDbConnection(cs.DBConn);
    con.Open();

    adapter = new OleDbDataAdapter();
    adapter.SelectCommand = new OleDbCommand(@"SELECT DISTINCT RTRIM(Phone) FROM tblPhone", con);

    ds = new DataSet("ds");
    adapter.Fill(ds);
    dtable = ds.Tables[0];
    cmbPhone.Items.Clear();

    foreach (DataRow drow in dtable.Rows)
    {
        cmbPhone.Items.Add(drow[0].ToString());
    }
}

Then inside of Combobox selected index event I will use this code:然后在Combobox选择的索引事件中,我将使用以下代码:

private void cmbPhone_SelectedIndexChanged(object sender, EventArgs e)
{
    try
    {
        OleDbConnection con = new OleDbConnection(cs.DBConn);
        con.Open();

        cmd = new OleDbCommand(@"SELECT DISTINCT 
                                    Brand, Phone, Tecnology
                                    FROM tblPhone", con);
        OleDbDataAdapter mAdapter = new OleDbDataAdapter(cmd);
        DataSet mDataSet = new DataSet();
        OleDbDataReader mReader;
        mReader = cmd.ExecuteReader();

        while (mReader.Read())
        {
            string sBrand = mReader.GetString(0);
            string sPhone = mReader.GetString(1);
            string sTec = mReader.GetString(2);

            lblBrand.Text = sBrand;
            lblPhone.Text = sPhone;
            lblTec.Text = sTec;
        }
    }
    catch (Exception ex)
    {
            MessageBox.Show("Erro\nDetalhes: " + ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

Basically the technology label is too big and when it reaches a length changes line.基本上技术标签太大了,当它达到长度时会改变线。 It is doable?可行吗?

Your question is not very clear.你的问题不是很清楚。 You are working with WPF or Winforms ?您正在使用 WPF 或 Winforms 吗?

Basicly you can try a few things like :基本上你可以尝试一些事情,比如:

-If winforms, you can set the autosize property = true of your label. - 如果使用 winforms,您可以设置标签的 autosize 属性 = true。

you can try Something like this :你可以尝试这样的事情:

    while (mReader.Read())
    {
        string sBrand = mReader.GetString(0);
        string sPhone = mReader.GetString(1);
        string sTec = mReader.GetString(2);

        lblBrand.Text = sBrand;
        lblPhone.Text = sPhone;
        lblTec.Text = sTec;
        int wdth = sTec.Length * 16; //you can put another value depending your charachter size. 
        lblTech.Size = new Size(wdth, 22);




    }

Or if you can give a fix width to your label and you can check the length of your string to add a new line :或者,如果您可以为标签提供固定宽度,并且可以检查字符串的长度以添加新行:

     int lngth = 100;
     if(sTech.Length > lngth)
     {
       sTech = sTech.SubString(0, lngth) + Environment.NewLine + sTech.SubString(lngth);
     lblTech.Text = sTech;
     }

Tell me if it works.告诉我它是否有效。

you just need to set two properties to your label: AutoSize and MaximumSize .您只需要为标签设置两个属性: AutoSizeMaximumSize AutoSize tells label to grow (both horizontally and vertically) but MaximumSize limits it to certain Width and Height . AutoSize告诉 label 增长(水平和垂直),但MaximumSize将其限制为特定的WidthHeight So.所以。 just do something like this:只是做这样的事情:

label1.AutoSize = true;
label1.MaximumSize = new Size(100, 600);
label1.Text = "test string which is pretty long";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM