1

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??

Christina
  • 11
  • 1
  • 2
  • You might want to refer to this: http://stackoverflow.com/questions/8566/best-way-to-access-a-control-on-another-form-in-c – o.k.w Feb 28 '10 at 12:27
  • It might be useful if you clarify the relationships of the two Forms: does the Form with RichTextBox create the Find Form, or are both created in some other way ? Is the Find Form a child Form of the Form with RichTextBox ? Are you showing the Find Form modally ? Also, are you familiar with how to define and raise custom events ? – BillW Feb 28 '10 at 12:40
  • um the form with the richtextbox has a button "Find". If this is clicked it opens up the find form. Im not sure what else your asking. Im very new to programming :-S – Christina Feb 28 '10 at 12:46
  • Hi Christina, It might help us to know "how new" you are. Do you think you are "ready" to learn about raising Events ? In the long run, imho, you are going to want to learn how to define and raise Events in one Form that another Form can subscribe to; and pass data via the Event from one Form to another. For right now my guess is you need to get a really good book on WinForms, like Matthew MacDonald's "Pro .NET 2.0 Windows Forms and Custom Controls," and systematically work your way through the introductory chapters. Also, look up the MSDN documentation for RichTextBox.Find in .NET. – BillW Mar 01 '10 at 04:50

2 Answers2

1

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
  • 489,969
  • 99
  • 883
  • 1,009
  • 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 Feb 28 '10 at 13:46
  • @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 Mar 01 '10 at 04:42
  • 1
    @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 Mar 03 '10 at 09:11
  • @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 Mar 08 '10 at 08:49
  • BillW - what you shouldn't do is manually change generated files such as the `.designer` files, as these can and will get overwritten. – Oded Mar 08 '10 at 08:50
  • @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 Mar 08 '10 at 08:52
1

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.

Jens
  • 3,249
  • 2
  • 25
  • 42