代码问题。在 RTB C# 中搜索文本
我有 2 种形式,
一种包含 Richtextbox,另一种用于在此 rtb 中搜索文本
我的代码显示错误,我不知道如何修复它。
这是显示错误的行,
RichTextBox box = ((Form1)base.Owner).rtxtEditor;
它说“对象引用未设置到对象的实例。”
这是我的全部代码。
private void frmFind_Shown(object sender, EventArgs e)
{
this.txtSearch.Focus();
}
private void cmdFind_Click(object sender, EventArgs e)
{
RichTextBox box = ((Form1)base.Owner).rtxtEditor;
int start = box.Find(this.txtSearch.Text, 0);
if (start == -1)
{
this.lblMatch.Text = "No match found";
this.cmdFindNext.Enabled = false;
}
else
{
this.lblMatch.Text = "";
box.Select(start, this.txtSearch.Text.Length);
this.cmdFindNext.Enabled = true;
box.ScrollToCaret();
((Form1)base.Owner).Focus();
}
}
private void cmdFindNext_Click(object sender, EventArgs e)
{
RichTextBox box = ((Form1)base.Owner).rtxtEditor;
int start = box.Find(this.txtSearch.Text, ((Form1 base.Owner).rtxtEditor.SelectionStart + 1, 0);
if (start == -1)
{
this.lblMatch.Text = "No more matches";
this.cmdFindNext.Enabled = false;
}
else
{
box.Select(start, this.txtSearch.Text.Length);
box.ScrollToCaret();
((Form1)base.Owner).Focus();
}
请帮忙!我 我的截止日期是明天 2 点
I have 2 forms
One includes a richtextbox the other is used to search for text in this rtb
My code is showing an error and i dont know how to fix it.
This is the line that shows the error
RichTextBox box = ((Form1)base.Owner).rtxtEditor;
It's saying "Object reference not set to an instance of an object."
This is my whole code.
private void frmFind_Shown(object sender, EventArgs e)
{
this.txtSearch.Focus();
}
private void cmdFind_Click(object sender, EventArgs e)
{
RichTextBox box = ((Form1)base.Owner).rtxtEditor;
int start = box.Find(this.txtSearch.Text, 0);
if (start == -1)
{
this.lblMatch.Text = "No match found";
this.cmdFindNext.Enabled = false;
}
else
{
this.lblMatch.Text = "";
box.Select(start, this.txtSearch.Text.Length);
this.cmdFindNext.Enabled = true;
box.ScrollToCaret();
((Form1)base.Owner).Focus();
}
}
private void cmdFindNext_Click(object sender, EventArgs e)
{
RichTextBox box = ((Form1)base.Owner).rtxtEditor;
int start = box.Find(this.txtSearch.Text, ((Form1 base.Owner).rtxtEditor.SelectionStart + 1, 0);
if (start == -1)
{
this.lblMatch.Text = "No more matches";
this.cmdFindNext.Enabled = false;
}
else
{
box.Select(start, this.txtSearch.Text.Length);
box.ScrollToCaret();
((Form1)base.Owner).Focus();
}
Please help! I My deadline is 2morrow
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Owner
属性将为null
,除非您手动设置它或调用采用Form
参数的Show
重载。您需要将显示“查找”表单的代码更改为
form.Show(this)
。The
Owner
property will benull
unless you set it manually or call theShow
overload that takes aForm
parameter.You need to change the code that shows the Find form to
form.Show(this)
.