How to change object properties in another form?

EFileTahi-A

Contributor
Joined
Aug 8, 2004
Messages
623
Location
Portugal / Barreiro
How to change object properties in another form?
- THREAD RESOLVED -


Well, I have two forms, form1 and form2. How can I say throught form1 to the textbox inside form2 to have a a new Text value?

Form2 myForm2 = new Form2();
Form2.textbox1.text = "bla";

I just does not work! why? why?

the textbox is set to public, I also try it to public static but with no success...

Please aid me!
 
Last edited:
EFileTahi-A said:
Well, I have two forms, form1 and form2. How can I say throught form1 to the textbox inside form2 to have a a new Text value?

Form2 myForm2 = new Form2();
Form2.textbox1.text = "bla";

I just does not work! why? why?

the textbox is set to public, I also try it to public static but with no success...

Please aid me!

In Form2 create a Public Property.
In the set{} block assign the value to the TextBox.Text property.

Code:
public string TextBoxText
{
     set{this.textBox1.Text = [COLOR=Blue]value[/COLOR];}
}
 
Hi, tks for the reply.

Damn, what ur saying is that I cannot directly change the form2's.textbox1 object throught form1 :(

In VB6 this was truly simple, there was no need to add code to the targets form object...

Are u sure there is no other way to do it? Because, by that way ur telling me, is like: "No, u can't, u have to call a function in the target form (form2) which contain the chaging events you want to apply.

Anyway why do I need the "set" keywork? I think it works with either "set" or not... or, I am wrong and I did not exactly understood what u have just explain me?

What if I want to change other textbox's attributes like, visible, size, top, left etc..?

Hope to hear from u soon again
 
Last edited:
You can do that by setting the Modifiers property of the textbox in Form2 to public.

And in your example, you'd need to do
Form2 myForm2 = new Form2();
myForm2.textbox1.text = "bla";

However, directly accessing control on another form is considered bad programming.
What if you change your form in 3 months to not use a textbox, but a rich-textbox (or something else). You'd have to update all the places where you directly accessed the textbox. If however, you used the solution of pas30339: to create an extra proprety on the form, you only have to update one method in the form itself. A lot safer and quicker.
You'd might want to read http://www.xtremedotnettalk.com/showthread.php?t=87848 or http://www.xtremedotnettalk.com/showthread.php?t=87853
 
I've never worked with VB-6 but I do know that VB.Net is a paradigm shift compared to VB-6. VB.Net implements Object Oriented Programming, and one of the tenets of OOP is encapsulation.

You use Public Properties to expose those items that you want to be able to manipulate.

The 'set' key word is required by the compiler. You could create an accessor method to do the same thing but Properties is how such behavior is implemented in .Net.

If you're creating a complex application then the way I described will work but it is not the best choice. In such a case you'd probably want to consider implementing the MVC design pattern.
 
tks to all who post.

I have a correction to make:

Form1 opens Form2, and its Form2 that wants to change Form1 objects...

I don't know if this changes anything but I cannot make any of ur examples work. The textbox in Form1 is set to public, therefore...

Code:
Form1 myForm1 = new Form1();
myForm1.textbox.text = "bla"
...is the right thing i want to do.

By using pas30339 example code in Form1...

Code:
public string textboxtext
{
     set{this.textbox1.Text = value;
}
...and calling it through Form2...

Code:
Form1.textboxtext = "bla";
..makes nothing

What In Gods name Am I not understanding???

Am browinsg the web to see if i can get this solved and I found this post by some guy with the similar problem as mine who got the following answer:

"Dim N As New Form1()

Is the incorrect line, this creates a new instance of Form1 instead of
referencing the existing Form1 Instance..

You will need to pass a reference into Form2, either for the Instance of
Form1 you currently have or for the specific textbox on Form1 you wish to
change. (I'd go for the second option, as you can then use it with other
textboxes also.. reusable code..).

So in Form2, create a TextBoxToAlter Property (or some such).. then when you create Form2, set the Form2.TextBoxToAlter = Me.TextBox1 (Assuming you're starting Form2 from Form1)

Then in Form2, use Me.TextBoxToAlter.ForeColor = Color.Blue etc..."

Please, if this solution works, could someone be gentleman enough to translate it to C#?

OMG, I have so much to do until monday and am wasting so much time with this (incoming badword...)
 
Last edited:
Well, I found the way to do what I wanted...

Public static Form1 form1 = null;

This allows me exactly to have direct control over Form1's boject library.

All I have to do is make it ready to use by writing "form1 = this;" whenever I need to use it, no matter where I call it! It's static after all :D

Tks to all for the posts...

- THREAD RESOLVED -
 
Back
Top