views:

26

answers:

1

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

+1  A: 

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

SLaks
thank you! I hate how something so small is so easy to fix
Christi
You should accept this answer by clicking the hollow check
SLaks