【发布时间】:2011-02-19 13:43:50
【问题描述】:
我正在尝试为我的程序查找下一个函数,我确实设法用这段代码完成了:
int findPos = 0;
private void button1_Click(object sender, EventArgs e)
{
try
{
string s = textBox1.Text;
richTextBox1.Focus();
findPos = richTextBox1.Find(s, findPos, RichTextBoxFinds.None);
richTextBox1.Select(findPos, s.Length);
findPos += textBox1.Text.Length;
//i = richTextBox1.Find(s, i + s.Length, RichTextBoxFinds.None);
}
catch
{
MessageBox.Show("No Occurences Found");
findPos = 0;
}
}
它在 form1 中运行良好,但如果我使用此代码并尝试从 form2 调用它,它不会做任何事情:
//Form1
public void FindNext()
{
try
{
this.Focus();
Form2 frm2 = new Form2();
string s = frm2.textBox1.Text;
richTextBox1.Focus();
findPos = richTextBox1.Find(s, findPos, RichTextBoxFinds.None);
richTextBox1.Select(findPos + 1, s.Length);
findPos += textBox1.Text.Length;
}
catch
{
MessageBox.Show("No Occurences Found");
findPos = 0;
}
}
//Form2
private void button1_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
frm1.FindNext();
}
有人知道这是为什么吗? 谢谢,坦纳。
【问题讨论】:
-
请为您的表单和控件命名。