I'm using visual studio .NET 2003 and C#. In vb, to edit a textbox's text on another form all you have to do is frmOther.txtBla.Text = "chicken". How do I do this in C#?
Printable View
I'm using visual studio .NET 2003 and C#. In vb, to edit a textbox's text on another form all you have to do is frmOther.txtBla.Text = "chicken". How do I do this in C#?
Yeah, that's a bit of a pain. It's an example of C# doing things the "right" way, and VB6 doing things the easy way.
Forms in VB6 are static. So you can access them from anywhere in your code. C# forms are actually objects.
So a form doesn't exist until you instantiate it. If you look in the "Main" function of the Program.cs file that exists in your windows project, you'll see where your initial form is actually instantiated.
"new Form1()" is actually creating a new instance of the Form1 object and passing it to Application.Run.Code:Application.Run(new Form1());
When you make other forms in your project, you have to instantiate them as well.
You need to structure your program in a way where the handle to the instance of that new form is passed to wherever you need it. Something I like to do is modify the declaration of my primary form, and pass an instance of any secondary forms I'll need during instantiation.Code:Form2 mySecondForm = new Form2();
Like this
That gives you a handle to the second form instance inside your first form instance. Extend this same concept to as many forms as you need.Code://in Form1 change
public Form1()
{
InitializeComponent();
}
//to
public Form2 mySecondForm = null;
public Form1(Form2 tForm)
{
mySecondForm = tForm;
mySecondForm.Visible = true;
InitializeComponent();
}
//in Main change
Application.Run(new Form1());
//to
Application.Run(new Form1(new Form2()));
Objects are objects in .NET, whether you're talking about C#, VB.NET or some other language. A form is an object like any other, and to access a member of an object you need a reference to that object. You can get a reference to an object in a number of ways. What umilmi81 has posted will work but is, in my opinion, rather an odd way to achieve it. What you need to do is get an understanding of how objects work and how to treat a form as an object. Once that's done the answer to your question will be obvious. I suggest that you read the "Forms in VB.NET" tutorila in my signature. The code examples are in VB.NET but the principles are exactly the same for C#. You can simply use one of the code converter links in my signature to convert the examples if you need C# code.
I don't know if frmOther is an instance name (object) it doesn't look like a class name to me [Unless you don't follow the naming conventions]
Anyway, if frmOther is an object, all you have to do is change txtBla from "private" to "internal" or public
see, VB by default marks all controls internal(Friend in VB) but in C#, the default is "private"
Good point CJ. :thumb: I always declare all my controls Private in VB too. That's another example of VB being "loose" to make things easier, but making you a worse programmer as a result. If you declare a control public then it is possible to actually replace the entire control from outside the form. I'me guessing that that's not desirable. If you want to change a property of a control from outside the form you should declare the control private, as C# does by default, and then declare a property or method of the form that will make the necessary changes. That way you only expose what is absolutely necessary. Declaring a control public is like issuing everyone an Access All Areas pass. You should only allow the minimum access for what is required. At least, that's good programming practice.