如何从指定字段获取PropertyGrid的TextBox?我需要这个TextBox来设置指向文本末尾的指针。
var num = txtBox.GetCharIndexFromPosition(Cursor.Position);
txtBox.SelectionStart = num + 1;
txtBox.SelectionLength = 0;那么,如何从PropertyGrid获取此TextBox呢?此外,PropertyGrid中的属性是只读的。
发布于 2013-07-20 01:33:18
如果您希望光标位于文本框中写入的最后一个字符之后,则可以依赖以下代码(由TextBox的TextChanged Event触发):
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必须被考虑在内。在这些代码行下面,我改编了您可以在PropertyGrid的MSDN中找到的代码
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中)。
https://stackoverflow.com/questions/17750926
复制相似问题