创建查找对话框

Creating a find dialog

所以对于 class 中的这个作业,我在 C# winforms 中制作了一个 richtextbox 编辑器,在这个作业中我必须包含一个查找函数,但我似乎无法掌握它 编辑:每次我单击搜索按钮时它都会关闭,但这不是我想要的,我希望它在用户通过右上角的 X 关闭实际对话框时关闭,搜索按钮应该只通过使用突出显示找到的字符串.Find() 方法

到目前为止,这是我的代码:

private void zoekenToolStripMenuItem_Click(object sender, EventArgs e)
{
    string searchValue = SearchDialog();
    Search(searchValue);
}

public string SearchDialog()
{
    Form findDialog = new Form();
    findDialog.Width = 500;
    findDialog.Height = 142;
    findDialog.Text = "Zoeken";
    Label textLabel = new Label() { Left = 10, Top = 20, Text = "Zoek naar:", Width = 100 };
    TextBox inputBox = new TextBox() { Left = 150, Top = 20, Width = 300};
    Button search = new Button() { Text = "Zoek", Left = 350, Width = 100, Top = 70 };
    search.Click += (object sender, EventArgs e) => { findDialog.Close(); };
    findDialog.Controls.Add(search);
    findDialog.Controls.Add(textLabel);
    findDialog.Controls.Add(inputBox);
    findDialog.ShowDialog();
    return (string)inputBox.Text;
}

void Search(string searchValue)
{
    rtxtInhoud.Find(searchValue);
}

部分: search.Click += (object sender, EventArgs e) => { findDialog.Close(); }; 是我真正坚持的 提前致谢

编辑:这是我尝试做的事情,但没有成功

public string SearchDialog()
{
   Form findDialog = new Form();
   findDialog.Width = 500;
   findDialog.Height = 142;
   findDialog.Text = "Zoeken";
   Label textLabel = new Label() { Left = 10, Top = 20, Text = "Zoek naar:", Width = 100 };
   TextBox inputBox = new TextBox() { Left = 150, Top = 20, Width = 300};
   Button search = new Button() { Text = "Zoek", Left = 350, Width = 100, Top = 70 };
   Button findNext = new Button() { Text = "Volgende", Left 250, Width = 100, Top = 70};
   search.Click += (object sender, EventArgs e) => { rtxtInhoud.Find(inputBox.Text); };
   findDialog.Controls.Add(search);
   findDialog.Controls.Add(textLabel);
   findDialog.Controls.Add(inputBox);
   findDialog.ShowDialog();
   return (string)inputBox.Text;
}

这会等待对话框关闭,然后突出显示找到的字符串。这不是我想要的,我希望它保持对话框打开,但仍突出显示文本

it closes every time I click the search button

是的,那是因为您向关闭表单的搜索按钮添加了一个事件处理程序:

search.Click += (object sender, EventArgs e) => { findDialog.Close(); };

but that's not what I want, I want it to [...]

然后在该处理程序中编写代码,使其执行您真正希望它执行的操作,而不是让它关闭表单。

在您的主表单代码中,创建一个方法来搜索您的文本并突出显示找到的字符串。然后将此方法连接到您的搜索对话框按钮,如下例所示。另外,我认为你应该调用 form.Show() 而不是 .ShowDialog(),这样另一个表单可以响应输入。

private void HighlightString(string stringToHighlight)
{
    // Code here to search your text and highlight a string.
}

private void SearchDialog()
{
    var findDialog = new Form {Width = 500, Height = 142, Text = "Zoeken"};
    var textLabel = new Label() {Left = 10, Top = 20, Text = "Zoek naar:", Width = 100};
    var inputBox = new TextBox() {Left = 150, Top = 20, Width = 300};
    var search = new Button() {Text = "Zoek", Left = 350, Width = 100, Top = 70};

    search.Click += (object sender, EventArgs e) => HighlightString(inputBox.Text);

    findDialog.Controls.Add(search);
    findDialog.Controls.Add(textLabel);
    findDialog.Controls.Add(inputBox);
    findDialog.Show();
}

使用此代码:

Application.Exit();