首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取PropertyGrid TextBox

获取PropertyGrid TextBox
EN

Stack Overflow用户
提问于 2013-07-20 00:21:25
回答 1查看 868关注 0票数 0

如何从指定字段获取PropertyGrid的TextBox?我需要这个TextBox来设置指向文本末尾的指针。

代码语言:javascript
复制
var num = txtBox.GetCharIndexFromPosition(Cursor.Position);
txtBox.SelectionStart = num + 1;
txtBox.SelectionLength = 0;

那么,如何从PropertyGrid获取此TextBox呢?此外,PropertyGrid中的属性是只读的。

EN

回答 1

Stack Overflow用户

发布于 2013-07-20 01:33:18

如果您希望光标位于文本框中写入的最后一个字符之后,则可以依赖以下代码(由TextBoxTextChanged Event触发):

代码语言:javascript
复制
private void txtBox_TextChanged(object sender, EventArgs e)
{
    int newX = txtBox.Location.X + TextRenderer.MeasureText(txtBox.Text, txtBox.Font).Width;
    int newY = txtBox.Bottom - txtBox.Height / 2;
    if (newX > txtBox.Location.X + txtBox.Width)
    {
        newX = txtBox.Location.X + txtBox.Width;
    }
    Cursor.Position = this.PointToScreen(new Point(newX, newY));
}

请记住,它的Y位置始终位于中间。

-在KINGKING评论之后更新

只要问题中的代码涉及到TextBox,我的答案就集中在TextBox上。尽管如此,KingKing是正确的,PropertyGrid必须被考虑在内。在这些代码行下面,我改编了您可以在PropertyGridMSDN中找到的代码

代码语言:javascript
复制
private void Form1_Load(object sender, EventArgs e)
{
    PropertyGrid propertyGrid1 = new PropertyGrid();
    propertyGrid1.CommandsVisibleIfAvailable = true;
    propertyGrid1.Location = new Point(10, 20);
    propertyGrid1.Size = new System.Drawing.Size(400, 300);
    propertyGrid1.TabIndex = 1;
    propertyGrid1.Text = "Property Grid";

    this.Controls.Add(propertyGrid1);

    propertyGrid1.SelectedObject = txtBox;
}

propertyGrid1中添加txtBox后,它的位置会更新,因此可以毫无问题地使用原始代码。

总之,我们的想法不是在PropertyGrid中查找TextBox,而是直接访问TextBox控件(该控件在运行时添加到PropertyGrid中)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17750926

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档