|
-
Jun 26th, 2006, 01:36 PM
#1
Thread Starter
Member
Edit text of a textbox on another form
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#?
-
Jun 26th, 2006, 02:18 PM
#2
Hyperactive Member
Re: Edit text of a textbox on another form
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.
Code:
Application.Run(new Form1());
"new Form1()" is actually creating a new instance of the Form1 object and passing it to Application.Run.
When you make other forms in your project, you have to instantiate them as well.
Code:
Form2 mySecondForm = new Form2();
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.
Like this
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()));
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.
-
Jul 1st, 2006, 11:10 AM
#3
Re: Edit text of a textbox on another form
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.
-
Jul 1st, 2006, 04:26 PM
#4
Re: Edit text of a textbox on another form
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"
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Jul 1st, 2006, 07:37 PM
#5
Re: Edit text of a textbox on another form
Good point CJ. 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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|