views:

366

answers:

2

Hi

I have 2 forms, one with containing a richtextbox and the other used for finding text in this rtb.

On this Find form is a textbox and 2 buttons , "Find" and "FindNext"

I've no idea how to link the 2 forms together to find the text and also how to find the text

:-S

Any help please??

+1  A: 

To each of your forms you can add a property that will reference the other form. This will give you access to the other form and all the controls on it through the property.

public property Form RTForm { get; set;}

You can then set this property in the place you construct the forms.

Form myForm = new Form();
Form rtForm = new RTForm();
myForm.RTForm = rtForm();
Oded
She'll will also have to make the rich text box she's trying to refer to public; change the declaration like protected Syste.Windows.Forms.RichTextBox RTB;to public Syste.Windows.Forms.RichTextBox RTB;
Steve Cooper
@Steve Your suggestion would require modification of the Designer.cs file for the Form which is not a good thing to do for many reasons : if you examine a typical WinForm Designer.cs file, you'll see that declarations of TextBoxes and other controls use the 'private modifier, not 'protected. If however, the OP is creating the RichTextBox at run-time, and inserting it into another Form, that would be a different story : but, in this case, we can be almost sure they are not doing that.
BillW
@BillW -- changing the access modifier is exactly what the designer will do itself if you show the form in the designer and change the box's 'Modifier' property to public. It's a perfectly fine thing to do.
Steve Cooper
@Steve +1 for getting me to question my assumptions, thanks. You've now set me on a "quest" to find out exactly why I made the assumption that WinForms controls should never have their access properties changed ! I am sure I have read many times that you should not do this (i.e., make controls public), but it's time to re-examine that. However, I do subscribe to the idea that "communication" between forms should be by "raising events," or by "public properties" : i.e., to the general idea of "loose coupling" is best.
BillW
BillW - what you shouldn't do is manually change generated files such as the `.designer` files, as these can and will get overwritten.
Oded
@Oded Please see my revised comment above. Yes, I believe I expressed strongly my view you should never "touch" the Designer.cs file in any case. Thanks !
BillW
Indeed you did. Teach me to read the whole discussion!
Oded
+1  A: 

There are several ways. You could just define a property on the one form which contains the richtextbox, which can be found by your other form.

public static string RTextboxText
{
    get
    {
        return myrichtextbox.Text;
    }
    set
    {
        myrichtextbox.Text = value;
    }
}

Setter can be dropped ofc.

Another way is to use a class between the 2 forms. I think you also use specific actions on the text? You might want to put all your code about that, in that class as well.

J. Vermeire