【问题标题】:How to set the vertical alignment for the WindowsForms TextBox?如何设置 WindowsForms 文本框的垂直对齐方式?
【发布时间】:2016-11-02 01:11:37
【问题描述】:

在 WPF 中,它默认支持 TextBox 控件的垂直对齐方式。但在 Windows 窗体中,无法设置 TextBox 控件的垂直对齐方式。

在我的例子中,我使用了多行文本框,文本需要显示在文本框的底部,并且在输入时需要保持对齐。

我尝试获取输入文本的行数,并尝试根据文本长度计算文本框的边界。但是在使用自动换行编辑文本时,行数不正确。

谁能帮助我在编辑时保持 TextBox 的垂直对齐?

我尝试使用该线程中给出的建议更改文本框的位置。编辑文本时。当我尝试编辑文本时,边界没有正确更新,并且文本的某些部分隐藏在文本框中。我已经根据字体大小和文本宽度计算了边界。

Size textSize = TextRenderer.MeasureText(TextBoxText, this.textBoxControl.Font);
int textBoxTop = this.textBoxControl.Bounds.Top;
int nol = (textSize.Width > this.textBoxControl.Width) ? ((textSize.Width) / this.textBoxControl.Width) + 1 : 1;
{
    if (nol > n)
    {
        n = nol;
        rect1 = this.textBoxControl.Bounds;
        if (top + (height - nol * textSize.Height) > top)
        {
            rect1.Y = top + (height - nol * textSize.Height);
            rect1.Height = nol * textSize.Height;
            this.textBoxControl.Bounds = rect1;
        }
        else
        {
            this.textBoxControl.Bounds = rect1;
        }
    }
    else if (nol < n)
    {
        n = nol;
        rect1 = this.textBoxControl.Bounds;
        if (rect1.Y + nol * textSize.Height < top + height)
        {
            rect1.Y += textSize.Height - this.textBoxControl.Margin.Top;
            rect1.Height -= textSize.Height;
            //this.textBoxControl.Bounds = rect1;
        }
        if (nol == 1)
        {
            rect1.Y = top + height - textSize.Height;
            rect1.Height = textSize.Height;
            //this.textBoxControl.Bounds = rect1;
            }
            this.textBoxControl.Bounds = rect1;
    }
}

它在编辑文本时工作正常,但在某些情况下,nol 行数计算错误。如何获取包含换行的文本框的实际行数。?

【问题讨论】:

  • 我尝试使用该线程中给出的建议更改文本框的位置。编辑文本时。
  • 您可以在 WinForms 中从 WPF 托管 TextBox。
  • 使用 .Top 进行某种形式的计算以改变垂直对齐方式,例如textBox1.Top = (this.Height - textBox1.Height) / 2;) 中心对齐或使用标签怎么样?它具有垂直对齐并且没有文本框的重新对齐垃圾。

标签: c# winforms vertical-alignment


【解决方案1】:

我创建了一个控件,它有一个TextBox 停靠在一个看起来有点像TextBox 的面板底部:

// Make sure you have this.
    using System.Linq;

public class BottomAlignTextBox : Panel
{
    public BottomAlignTextBox()
    {
        this.BackColor = Color.White;
        this.BorderStyle = (Application.RenderWithVisualStyles) ? BorderStyle.FixedSingle : BorderStyle.Fixed3D;
        this.Size = new Size(200, 200);
        this.Padding = new Padding(5, 0, 4, 2);

        bottomAlignTextBox.Dock = DockStyle.Bottom;
        bottomAlignTextBox.Multiline = true;
        bottomAlignTextBox.WordWrap = true;
        bottomAlignTextBox.AcceptsReturn = true;
        bottomAlignTextBox.BorderStyle = BorderStyle.None;
        bottomAlignTextBox.Height = 20;

        bottomAlignTextBox.TextChanged += delegate
        {
            if (bottomAlignTextBox.Height < this.Height - 20)
            {
                if (TextRenderer.MeasureText(bottomAlignTextBox.Text, bottomAlignTextBox.Font).Width > bottomAlignTextBox.Width + 6)
                {
                    string longestLine = bottomAlignTextBox.Lines.OrderByDescending(s => TextRenderer.MeasureText(s, bottomAlignTextBox.Font).Width).First();
                    bottomAlignTextBox.Text = bottomAlignTextBox.Text.Replace(longestLine, longestLine.Substring(0, longestLine.Length - 1) + Environment.NewLine + longestLine[longestLine.Length - 1]); 
                    bottomAlignTextBox.Height += 19;
                    bottomAlignTextBox.SelectionStart = bottomAlignTextBox.Text.Length + 2;
                    bottomAlignTextBox.SelectionLength = 0;
                }
            }
        };

        this.Controls.Add(bottomAlignTextBox);
        this.Click += delegate { bottomAlignTextBox.Focus(); };
    }

    public new string Text
    {
        get { return bottomAlignTextBox.Text; }
        set { bottomAlignTextBox.Text = value; }
    }

    private TextBox bottomAlignTextBox = new TextBox();
}

【讨论】:

  • 感谢您的建议,我已经尝试过了,但是当我输入文本时高度计算错误。我已经落后了一点,我无法获得文本框中显示的实际行。一旦我得到正确的行数,我就可以使用LineCount * FontHeight 之类的代码轻松计算文本框的高度。有没有办法让这些行数..?
  • @Adhi 我现在修改了它,它将最长行的最后一个字符移动到换行符。
  • 嗨@Shad0wk,感谢您的示例,我可以通过将选择开始修改为行尾来连续键入文本。 bottomAlignTextBox.SelectionStart = bottomAlignTextBox.Text.Length; bottomAlignTextBox.Height += bottomAlignTextBox.Font.Height; 现在工作正常,但我还需要做一件事。当我从文本框中删除或退格字符时,我需要根据行数减少行高。我怎样才能做到这一点。
  • 创建一个名为 height 的 int 并将其与 Textbox.Lines.Length 进行比较,以确定是否需要降低高度。 我会更新答案,但我正忙于一个项目。
  • 嗨 Shad0wk,感谢您的关注,现在我可以根据行的长度增加或减少文本框的高度。现在我在进入编辑模式时又遇到了一个麻烦。我们如何计算已分配文本的行数。谢谢。
【解决方案2】:

我不确定这是否完全可能,但我认为您可以使用 Panel 控件或某种形式包装控件并将其停靠到包装 Panel 控件的底部?如果需要动态调整大小,也可以使用 Anchor 属性。

【讨论】:

  • 好主意!如果您显示代码会更好。
  • 你真的不需要代码吗?您可以将文本框或 RTB(无论您使用的是什么)的 Dock 位置设置为“底部”,将面板的背景颜色设置为与文本框相同,您实际上应该完成...Only if我知道你是什么问对了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-15
  • 1970-01-01
  • 1970-01-01
  • 2012-01-20
  • 1970-01-01
  • 2011-06-22
相关资源
最近更新 更多