在C#中无模式对话框中单击按钮时无法关注父窗体

问题描述:

大家好,


我是c#的初学者。我正在尝试使用Windows窗体在c#中开发一个记事本编辑器应用程序..我创建了一个查找无模式对话框。当我点击查找按钮时,它能够找到文本的索引,但主窗体中的文本没有突出显示或被选中..请帮我解决这个问题。

Hi everyone,

i m a beginner in c#. i m trying to develop a notepad editor application in c# using windows forms.. i have created a find modeless dialog. when i click the find button, it is able to find the index of the text but the text in the main form is not getting highlighted or selected.. pls help me how to do this.

您是如何选择主表单中的文本的?您使用了什么控件?

如果您使用的是TextBox,则可以选择文本:
How did you select text in you main form? What control did you use?
If you''re using a TextBox, you can select text that way:
展开 | 选择 | 换行 | 行号


谢谢ChBinder ..


但是我在mainform中使用了richtextbox。

在我的查找对话框窗体中,单击查找按钮后,它会找到文本索引并将其传递给调用mainform中TextFound方法的事件处理程序。


Finddialog代码:


记事本n =新记事本();

private void find_Click(object sender,EventArgs e)

{

this.index = holdText.IndexOf(textBox1.Text);

if(index> = 0)

{

this.length = textBox1.Text.Length;

TextFound + = new TextFoundEventHandler(n.dlg_TextFound);

TextFound(this,EventArgs。空);

}


}


Mainform中的TextFound方法:


public void dlg_TextFound(object sender,EventArgs e)

{

FindDialog dlg =(FindDialog)sender;

richT extBox1.Focus();

richTextBox1.Select(dlg.index,dlg.length);

richTextBox1.Focus();

}
thanks ChBinder..

But i hav used a richtextbox in mainform.
In my find dialog form, once the find button is clicked, it finds the text index and passes it thro an event handler that invokes the TextFound method in mainform.

Finddialog code:

Notepad n = new Notepad();
private void find_Click(object sender, EventArgs e)
{
this.index=holdText.IndexOf(textBox1.Text);
if (index >= 0)
{
this.length = textBox1.Text.Length;
TextFound += new TextFoundEventHandler(n.dlg_TextFound);
TextFound(this,EventArgs.Empty);
}

}

TextFound method in Mainform:

public void dlg_TextFound(object sender, EventArgs e)
{
FindDialog dlg = (FindDialog)sender;
richTextBox1.Focus();
richTextBox1.Select(dlg.index, dlg.length);
richTextBox1.Focus();
}


RichTextBox还具有属性 HideSelection 。将它设置为False(在设计器中)它应该可以工作。
RichTextBox also has the property HideSelection. Set it to False (in designer) and it should work.