tags:

views:

169

answers:

7

I have a C# class library which uses a form (which is also in the library). Let's say I have an edit box on this form called editContents. In a regular form application, I'm used to being able to aquire the edit box like this:

class MainForm
{
     void Method()
     {
          this.editContents.Text = "Hi";
     }
}

I guess some magic occurs behind the scenes in a regular forms application, because the edit box member is private in the MainForm class, but I can still access it like a public member.

But in my class library I can't access the edit box like this. I instantiate and show the form "manually" like this:

form = new MyForm();
form.Show();

How do I properly acquire the editContents control from this form?

+13  A: 

You could make it a publicly accessible Property, by adding some code like this:

public String EditContents // for just the "Text" field
{
   get { return this.editContents.Text; }
   set { this.editContents.Text = value; }
}

Or:

public TextBox EditContents // if you want to access all of the TextBox
{
   get { return this.editContents; }
   set { this.editContents = value; }
}
Donut
The first method is much preferable, the second is weakening encapsulation and should not normally only be needed.
Henk Holterman
A: 

You could aquire it through the Controls collection.

form.Controls.Find("nameOfControl", true);
Robban
What happens if `"nameOfControl"` changes? The code will still compile, but it'll throw an exception **at runtime**.
Donut
+3  A: 

The "magic" is that a field is generated for your textbox in the *.Designer.cs file. By default, this field is private. If you want to change it's accessibility, select your text box in the forms designer, and change the "Modifiers" property to Public.

This might however not be a good idea to expose publicly all controls of your form. You can instead wrap it around in a property like Donut suggests, which is cleaner.

Julien Lebosquain
+1  A: 

Private members are accessible within their declaring class. That's why you're able to access editContents from within MainForm.
Private members are inaccessible from outside their declaring class.
(it's the definition of private, there is no magic)

You can wrap it in a public property:

public TextBox EditContents
{
   get { return this.editContents; }
}
najmeddine
A: 

The difference in accessing the form member inside the Method() function vs via the form reference in your last snippet is that your Method() function is a member of your form's class, and therefore is allowed to access other private members of the class.

Joel Coehoorn
A: 

In the first instance, you are able to access editContents because you are within the scope of the form.

As @Donut has said, you can expose a property for users of your library to play around with the control. If you want to limit the access, you could write a method instead.

e.g.

void SetContentForEditor(string text)
{
   editContent.Text = text;
}

And, you can then make a call to SetContentForEditor
e.g.

myForm.SetContentForEditor("hello world");
shahkalpesh
A: 

I would introduce an event in the library which notifies all subscribers to this event when a change happens to the text and should be set in the form. The form should attach to this event and set the textbox content on its own.

Scoregraphic