Pretty much as it says. I tried through every little Form1.(names).blah.blah But I could not find how to access a listbox on Form2 from form1. Please help.
Tim
Pretty much as it says. I tried through every little Form1.(names).blah.blah But I could not find how to access a listbox on Form2 from form1. Please help.
Tim
Tim, this question has come up a number of times, and it requires a decent understanding of object oriented programming to answer. All you need to know should be here: http://cboard.cprogramming.com/showthread.php?t=65533
I haven't seen anything on the internet about it, so I'm going to write an article on doing this soon. If the link above doesn't answer your question, be patient and I'll let you know when it's done.
Remember - each form is just a class. Its controls are its member variables. Use normal OO to interact with the forms just like you would any other class.
Aye, sorry. I have a good understanding of OOP in C++. Just a little confused with the way it's set up in C#. I tried changing the members to protected instead of private on the class form. But that didn't work either. I'll just wait until you have finished the article. Thanks!
Tim
Well, I finally figured out how to access them. I just turned them to public controls. But, even though I can access them and give them a command, such as form2.txtOutput.Text = "Hi";, it doesn't do it. I dunno, very very confusing...
Tim
Don't expose ANY variables as public. Thats about as contra-OOP as it gets. If you need to set something in Form2 from an instance of Form1, write a method or property in Form2 that takes the text as parameter and then sets it's own textbox. Call this method on your instance of Form2.
hth
-nv
She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."
When in doubt, read the FAQ.
Then ask a smart question.
Correct. This really is the best simple way of doing things, and its the method I use 90% of the time.Originally Posted by nvoigt
The parent (creating) form should set properties on the child form, and the child form should fire events that the parent is interested in knowing if something happens.
So for instance, as nvoigt said, if you are in MainForm, and you wish to set a textbox on Form2, Form2 really should have a property like this:
If Form2 has a button and MainForm wants to know when that button is clicked, Form2 should have an event:Code:public class Form2 : Form { [...Form2's Code...] public string TheText { get { return textBox1.Text; } set { textBox1.Text = value; } } [...The rest of Form2's Code...] }
This has the added advantage of allowing multiple forms know when the button is clicked.Code:public class Form2 : Form { [...Form2's Code...] public event EventHandler ButtonClicked; // called in Form2 when the button is clicked void button1_Click(object sender, EventArgs e) { // Tell our listeners, in this case MainForm, the button was clicked if (ButtonClicked != null) { ButtonClicked(this, EventArgs.Empty); } } [...The rest of Form2's Code...] }
In MainForm, this is how you could use these new properties of Form2:
Code:public class MainForm() { [...MainForm's Code...] void CreateNewForm2() { Form2 form2 = new Form2(); form2.TheText = "G'day world!"; // add a new event handler form2.ButtonClicked += new EventHandler(Form2_ButtonClicked); } // We then have the event handler: void Form2_ButtonClicked(object sender, EventArgs e) { MessageBox.Show("OMG! The button in Form2 was clicked, but I'm showing you this from MainForm!!!!1"); } [...The rest of MainForm's Code...] }
Last edited by nickname_changed; 06-09-2005 at 01:27 AM.
Right, I switcheds back to private, and I learned how to make the methods and things. But now, of course there's another problem, I ALWAYS have form2 and form1 showing at the same time, and the only way it works is when I do the change, and then hit form2.show. THen it upgrades it to the new change(ie, text = "Hello"), However, is there a way to refresh the form, without having to .show it everytime? I tried the refresh and update command, but that didn't work either.
Hope this is clear of what i'm doing.
Thanks!
Tim