简体   繁体   English

Windows窗体:以模式方式打开另一个对话框窗口时,如何在文本框中保持焦点和光标

[英]Windows Forms: How keep focus and cursor on a textbox when another dialog window is open modally

I have a form frmFirst where I have a textBox for text entry. 我有一个表格frmFirst,其中有一个用于输入文本的textBox。 Then I want to open a second form (frmSecond) modally. 然后,我想模态地打开第二个表单(frmSecond)。 This second form is like the form in WinWord "Insert Symbol" where a set of special symbols are accessible through button. 第二种形式类似于WinWord“插入符号”中的形式,在该形式中,可以通过按钮访问一组特殊符号。 The problem is that, when we are in the 2nd form and want to insert a symbol or character in the first one, we cannot see where the cursor is position within the text, because the focus came to frmSecond. 问题在于,当我们处于第二种形式并想在第一种形式中插入符号或字符时,由于焦点位于frmSecond,因此无法看到光标在文本中的位置。

Any ideas to keep the cursor visually in the first form while pressing buttons on another one? 有什么想法可以使光标在第一个窗体中保持可视化,同时按下另一个窗体上的按钮? Thanks 谢谢

I don't think its possible to keep the cursor visually on the first form if you are using just the standard out of the box Textbox, but you can do the following: 如果您仅使用标准的即开即用文本框,则我认为将光标保持在第一个窗体上是不可能的,但是您可以执行以下操作:

Create a delegate to update the textbox on your first form: 创建一个委托以更新第一个表单上的文本框:

public delegate void UpdateText(string s);

Which does the following: 执行以下操作:

private void UpdateText(string s)
{
    textBox1.Paste(s);
}

When you create your 2nd form, pass in the delegate, and call this as often as you like to insert at the correct location in the textbox: 创建第二个表单时,请传递委托,并根据需要多次调用它,以将其插入文本框中的正确位置:

UpdateText updDelegate = new UpdateText(UpdateText);
FormInput frmInput = new FormInput(updDelegate);
frmInput.Show(this);

Paste() will insert at the correct location, you could also keep track of the cursor position using the Leave/Click/KeyPress events and then when your 2nd form is closed give the focus back to the textbox in the correct position. Paste()将插入正确的位置,您还可以使用Leave / Click / KeyPress事件来跟踪光标位置,然后在关闭第二个窗体时将焦点放回到正确位置的文本框中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM