Results 1 to 4 of 4

Thread: Access controls on a different form.

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2009
    Location
    Oshawa
    Posts
    214

    Access controls on a different form.

    I've searched for resolutions to not being able to access controls on one form from another and I'm having some issues.

    From a separate form, I use this code...

    Code:
                   frmMain.tab_BottomPanel_MDITabs.TabPages(Convert.ToInt32(this.Tag)).Tag = true;
    But I get the object reference required error.

    I've tried doing this on frmMain...

    Code:
           public string tab_BottomPanel_MDITabs_Tag
            {
                get { return (string) this.tab_BottomPanel_MDITabs.Tag; }
                set { this.tab_BottomPanel_MDITabs.Tag = value; }
            }
    but from the other form, even tab_BottomPanel_MDITabs_Tag doesn't show up in the list.

    How do I access the controls on the main form? The reason for this is because the secondary form is loaded into the main form in an MDI tab control so I need to be able to access the controls on the main form.

  2. #2
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: Access controls on a different form.

    you need to send a reference of the from which you want to use its controls to the target form

    let's say you have form1 with this methods:
    public void SayHello() {
    MessageBox.Show("Hello");
    }

    now you want to call the "SayHello" Method from form2, so in form2 constructor you you pass form1 instance

    from2
    Code:
    public class form2{
    
    private form1 _MainForm
    public form2(form1 MainForm) {
    this._MainForm = MainForm;
    }
    
    // now you can call form1 method
    this._MainForm.SayHello();
    
    }
    and from form1 you open form2 like so:
    Code:
    form2 f = new form2(this);
    f.Show();

    I wrote all of the code by hand since i'm not next to my computer sorry errors might be present ;
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Access controls on a different form.

    Quote Originally Posted by taigon View Post
    I've searched for resolutions to not being able to access controls on one form from another and I'm having some issues.
    The resolution is to not even try. No form should ever access controls on another. All communication between forms should involve getting and setting properties, calling methods and raising and handling events.

    Generally speaking, when you have two forms, only one of them should be aware of the other. One form will create the other and the one being created shouldn't really have to know that the one that created it exists.

    If the creator wants to change something on the created, it should either set a property or call a method. The code inside that property or method will then update the appropriate control(s), so the created form is updating its own controls and the creator doesn't even have to know that the controls exist.

    Conversely, if the created wants to change something on the creator, it should raise an event. The creator handles that event and then, again, updates its own controls. This is the one and only "proper" way for forms to pass data around.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Oct 2009
    Location
    Oshawa
    Posts
    214

    Re: Access controls on a different form.

    Hi jmc,

    If you don't mind, could you possibly post a very simple example with 2 forms showing how to properly update say a textbox for example. With a string generated on Form2, how can I update the textbox.text with the string value?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width