开发者

problem with code. Searching text in a rtb c#

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开发者_StackOverflow社区 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


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜