【问题标题】:Show all text in a title of a form [duplicate]显示表单标题中的所有文本[重复]
【发布时间】:2013-11-23 04:01:15
【问题描述】:

我的文本标题可能有不同的长度,我希望该文本完全适合表单的整个标题栏。所以我需要改变表格的宽度。但是如何知道标题中文本的宽度,在不同的系统中,标题文本的字体和大小可能会有很大差异? 提前致谢!

【问题讨论】:

标签: c# winforms size width titlebar


【解决方案1】:

这可能会有所帮助,

    private void YourFormName_SizeChanged(object sender, EventArgs e)
    {
        this.Width = 300; // the default or initial width
    }

    private void textBox1_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            e.Handled = true;

           if (textBox1.Text.Length == 0)
            {
                this.Width = 300;
                goto END;
            }
            if (textBox1.Text.Length < this.Width)
                this.Width = textBox1.Text.Length;
            else
                this.Width = this.Width + textBox1.Text.Length;
        END:this.Text = textBox1.Text;
        }
    }

【讨论】:

    【解决方案2】:

    在 WPF 中很容易解决,但是对于 Windows 窗体,我认为您可以从标准的Window 控件继承并在初始化代码中分配标题标题宽度(计算起来必须简单,正如 Andrei V 在 cmets 中提到的那样)转换为一种形式。

    【讨论】:

      【解决方案3】:

      在表单中,将以下代码放入构造函数中:

      this.Width = this.Width + this.Text.Length;
      

      this.text 返回标题的文本。 this.width 返回表单的宽度。只需根据标题中文字的长度调整宽度即可。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-07-09
        • 2019-12-15
        • 1970-01-01
        • 2015-01-09
        • 1970-01-01
        • 2019-03-13
        • 2014-05-10
        • 1970-01-01
        相关资源
        最近更新 更多