Thread: Accessing Controls on a different form from one form.

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    11

    Accessing Controls on a different form from one form.

    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

  2. #2
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    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.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    11

    Reply

    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

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    11

    Controls

    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

  5. #5
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    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.

  6. #6
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Quote Originally Posted by nvoigt
    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.
    Correct. This really is the best simple way of doing things, and its the method I use 90% of the time.

    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:
    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...]
    }
    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 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...]
    }
    This has the added advantage of allowing multiple forms know when the button is clicked.

    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.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    11

    ..

    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

  8. #8
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Show your code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. placing user controls in a form...
    By AngKar in forum C# Programming
    Replies: 2
    Last Post: 07-21-2006, 04:02 PM
  2. Accessing main form from functions in other classes
    By pj_martins in forum C++ Programming
    Replies: 1
    Last Post: 11-05-2004, 09:27 AM
  3. My UserControls dissapear off my form
    By zMan in forum C# Programming
    Replies: 2
    Last Post: 09-15-2004, 08:55 AM
  4. Using the VS.NET form designer with tab controls
    By bennyandthejets in forum Windows Programming
    Replies: 1
    Last Post: 07-06-2004, 12:49 AM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM