在C#中无模式对话框中单击按钮时无法关注父窗体 [英] Cannot get focus on the parent form while clicking button in modeless dialog in C#

查看:62
本文介绍了在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.


这篇关于在C#中无模式对话框中单击按钮时无法关注父窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆