创建查找对话框

时间:2015-03-30 15:59:12

标签: c# winforms richtextbox

因此,对于课程中的这个作业,我在C#winforms中创建了一个richtextbox编辑器,在这个作业中我必须包含一个find函数,但我似乎无法理解它 编辑:每次我点击搜索按钮时它都会关闭,但这不是我想要的,我希望它在用户通过右上角的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;
}

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

3 个答案:

答案 0 :(得分:0)

  

每次点击搜索按钮

时它都会关闭

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

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

但那不是我想要的,我想要它[...]

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

答案 1 :(得分:0)

使用此代码:

Application.Exit();

答案 2 :(得分:-1)

在主表单代码中,创建一个搜索文本并突出显示已找到字符串的方法。然后将此方法挂钩到搜索对话框按钮,如下面的示例所示。此外,我认为您应该拨打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();
}