【问题标题】:Why does TextBox ClientRectangle, DisplayRectangle, or Bounds not return displayed size?为什么 TextBox ClientRectangle、DisplayRectangle 或 Bounds 不返回显示的大小?
【发布时间】:2013-11-02 08:03:03
【问题描述】:

我正在尝试在 TextBox 中放置一个标签。但是,我在让标签在文本框中正确定位时遇到了一些麻烦。我可以让它显示在文本框中,但这就是我的问题出现的地方。当我尝试定位标签时,它似乎无法正常工作。我有以下 sn-p 在 TextBox 的构造函数中添加标签:

        Label lblClear = new Label();
        lblClear.Text = "X";
        lblClear.Font = this.Font;
        lblClear.Location = new Point(this.DisplayRectangle.X + (this.DisplayRectangle.Width - 15), this.Bounds.Y);
        lblClear.Size = new Size(15, 15);

        this.Controls.Add(lblClear);

但是,这并没有像我期望的那样一直放在文本框的右侧,而是放在文本框中间的某个地方。为什么 ClientRectangle、DisplayRectangle 或 Bound 没有像我想的那样返回文本框的大小?底层的 texbox 矩形实际上是否比屏幕上显示的要小?

感谢任何帮助。谢谢。

编辑:这是我正在谈论的屏幕截图:

这是我的课程:

public class SearchTextBox : TextBox
{
    public SearchTextBox()
    {
        InitializeComponent();
        Label lblClear = new Label();
        lblClear.Text = "X";
        lblClear.Font = this.Font;
        lblClear.Location = new Point(this.DisplayRectangle.X + (this.DisplayRectangle.Width - 15), this.Bounds.Y);
        lblClear.Size = new Size(15, 15);

        this.Controls.Add(lblClear);
    }
}

编辑:我让它工作了,但这只有在我从表单中删除文本框并重新添加它时才生效......

【问题讨论】:

  • “我不是在寻找这个来调整大小。”这看起来比放置在表单上的标准文本框

标签: c# textbox height size width


【解决方案1】:

你能展示一下它是如何不工作的吗?

如果您希望“X”在调整 TextBox 大小时保持在右侧,那么您需要将其 Anchor() 到右侧。

这是我得到的代码:

public class MyTextBox : TextBox
{

    public MyTextBox()
    {
        Label lblClear = new Label();
        lblClear.Text = "X";
        lblClear.Font = this.Font;
        lblClear.Location = new Point(this.DisplayRectangle.X + (this.DisplayRectangle.Width - 15), this.Bounds.Y);
        lblClear.Size = new Size(15, 15);

        this.Controls.Add(lblClear);
    }

}

【讨论】:

  • 我编辑了我的原始帖子以包含一张图片和我拥有的课程。我不是在寻找这个来调整大小。这将是一个固定宽度的文本框,但我似乎无法让标签一直向右移动,即使我的构造函数代码看起来和你的差不多。
猜你喜欢
  • 1970-01-01
  • 2020-04-09
  • 2021-08-24
  • 1970-01-01
  • 1970-01-01
  • 2022-12-15
  • 1970-01-01
  • 2013-07-19
  • 1970-01-01
相关资源
最近更新 更多