Click here to Skip to main content
15,499,289 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I Program in VB.NET and I am now moving to C #, and I have some doubts. In VB.NET when I add a class to an application type WindowsFormApliccation I can access the controls directly, using the form name followed by the name of the control I want.
For example: I have a textbox on the form and add a class to the project, at the class I have the method below.

Public Class Class1
     Public Sub method ()
         Form1.TextBox1.Text = "text"
     end Sub
end Class

When this method is called it will write the string "Text" in TextBox1. I tried this in C # and could not. Does anyone know how I do it?
Updated 10-Jan-12 10:39am

Why do it this way (even though it's doable). IMO the proper way would be to call the class from the form and send the contents of the textbox to the method of the class. The method can also return data . So basically you could have in your form code like:
MyClass instance = new MyClass()
TextBox1.Text = instance.GetText();

and the class:
public class MyClass {
   public string GetText() {
      return "text";

Depending on the situation the class and the method could also be static when no instantation would take place.

Addition (not advisable):

Passing the form as a parameter, the calss:
public class MyClass {
   public void SetText(Form1 formInstance) {
      formInstance.TextBox1.Text = "Text";

And calling:
MyClass instance = new MyClass()
Share this answer
Edson Rodrigues da Silva 10-Jan-12 17:03pm     CRLF
Hello Mika Wendelius. I have several methods in a class and I need these methods do several tasks that involve a number of form controls. As an example, change the background color of a button, change the text of it, change the text of a label, access the items in a listbox, etc.. In VB.NET I could do this within a class simply by accessing the form's name preceded by the name of the control and ownership of this control that I wanted to change, as in: button1.BackColor = Color.Green listBox1.Items.Add ("Text") The problem is that I do not know how to do this in C #. Thanks in advance.
Wendelius 10-Jan-12 17:25pm     CRLF
In order to change the properties of controls on a form you would need to know the instance. As said this can be done be passing the instance of Form1 to a class and if the controls are public they can be referenced outside the Form1 class (from another class). However, it's typically not advisable to modify the controls from outside the window and that was the reason for my suggestion. However, I updated the solution to show an alternative but I really don't suggest this.
Edson Rodrigues da Silva 10-Jan-12 18:32pm    
Thanks worked like I wanted with your last answer. I'll try to follow your advice and not send the Form to the class.
Wendelius 11-Jan-12 0:50am    
You're welcome :)
manognya kota 11-Jan-12 0:33am     CRLF
Hi, Please mark the thread as solved/answered when the answer serves the purpose. Thanks
Mika Wendelius is right.

I would like to add my 2 cents here.
If you want to access a form in c# you may have to do. The Form1 is the form user created in design view.
Form1 f = new Form1();
then access the methods through instance "f".

The Form is a container type. You cannot directly access the controls on the form. You have to use f.Controls() method.

Often developers need to access a specific control from outside, maybe from another class. But, it is safe to set event handlers only to the controls from outside, for example button click event.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900