[英]How to add PAGESET UP, FIND, REPLACE Dialog Box in C# code for Notepad Application
[英]How to create a Find/Replace Dialog box in MDI text editor using c#?
我在查找對話框中使用以下代碼(它本身是一個單獨的表單)。 它為While循環提供了Null參考。任何人都可以幫助解決這段代碼的錯誤,或者它是在mdi編輯器中查找文本的正確方法嗎? 謝謝
// Form1 is the main form having all editors functionalities
private void findbtn_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
int index = 0;
while (index < f1.GetRichtextbox().Text.LastIndexOf(textBox1.Text))
{
f1.GetRichtextbox().Find(textBox1.Text,index,f1.GetRichtextbox().TextLength,RichTextBoxFinds.None);
f1.GetRichtextbox().SelectionBackColor = Color.Yellow;
index = f1.GetRichtextbox().Text.IndexOf(f1.GetRichtextbox().Text, index) + 1;
}
}
Form1是具有所有編輯器功能的主要形式
對...你需要一個對用戶在屏幕上可見的Form1實際實例的引用。 在創建它時將其引用傳遞到“查找”對話框,或者如果“查找”對話框也是MdiChild,請使用MdiParent()屬性獲取主MDI表單,然后遍歷其MdiChildren()屬性直到找到所需的Form1實例。
編輯:
好的,您可以在創建和顯示“查找”表單時在Show()命令中傳遞對MdiParent的引用:
public partial class frmMdiParent : Form
{
public frmMdiParent()
{
InitializeComponent();
}
private void findToolStripMenuItem_Click(object sender, EventArgs e)
{
frmFind find = new frmFind();
find.Show(this);
}
}
現在,在“查找”表單中,您可以將Owner()屬性強制轉換回MdiParent。 使用其ActiveMdiChild()屬性獲取對Form1的引用:
public partial class frmFind : Form
{
private frmMdiParent frmMDI = null;
public frmFind()
{
InitializeComponent();
this.Shown += new EventHandler(frmFind_Shown);
}
void frmFind_Shown(object sender, EventArgs e)
{
if (this.Owner != null && this.Owner is frmMdiParent)
{
this.frmMDI = (frmMdiParent)this.Owner;
}
}
private void btnFind_Click(object sender, EventArgs e)
{
if (this.frmMDI != null && this.frmMDI.ActiveMdiChild is Form1)
{
Form1 frm1 = (Form1)this.frmMDI.ActiveMdiChild;
// ... do something with "frm1" ...
frm1.Text = "Hello from `frmFind`...";
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.