代码问题。在 RTB C# 中搜索文本

发布于 2024-08-23 09:52:59 字数 1550 浏览 5 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲念泪 2024-08-30 09:52:59

Owner 属性将为 null,除非您手动设置它或调用采用 Form 参数的 Show 重载。

您需要将显示“查找”表单的代码更改为 form.Show(this)

The Owner property will be null unless you set it manually or call the Show overload that takes a Form parameter.

You need to change the code that shows the Find form to form.Show(this).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文