gpt4 book ai didi

C# 创建类似搜索框的 Notepad++

转载 作者:行者123 更新时间:2023-11-30 15:45:59 30 4
gpt4 key购买 nike

我想要做的是一个与 VS 或 Notepad++ 中完全相同的搜索窗口,其中两个窗口都是事件的(因为 FindBox 显示为 Show 而不是 ShowDialog),当您在 FindBox 中按查找时,父执行搜索。这是一个例子:

class MainForm : Form
{
public void FindNext(string find)
{
// Do stuff
}

public void OpenFindWindow()
{
FindBox find = new FindBox();
find.customParent = this;
find.Show();
}
}

class FindBox : Form
{
public customParent;

public void FindButtonPressed()
{
((MainForm)customParent).FindNext(textBox1.text);
}
}

但我必须手动设置这个新字段“customParent”似乎很奇怪。执行此类操作的官方方法是什么?

最佳答案

您可以接受 customParent 作为构造函数中的参数,或者更好的是,FindBox表格应采用 Action<string>一旦按下查找按钮,它就会调用。

示例:

class MainForm : Form
{
public void FindNext(string find)
{
// Do stuff
}

public void OpenFindWindow()
{
FindBox find = new FindBox(this.FindNext);
find.Show();
}
}


class FindBox : Form
{
private Action<string> callback;

public FindBox(Action<string> callback)
{
this.callback = callback;
}
public void FindButtonPressed()
{
callback(textBox1.text);
}
}

关于C# 创建类似搜索框的 Notepad++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4687695/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com