Results 1 to 4 of 4

Thread: Pass value between forms

  1. #1

    Thread Starter
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861

    Pass value between forms

    I have 2 forms (Main and Username).
    My Main form has a mainMenu and a dropdown list (combobox) which displays usernames. The mainMenu allows an person to click the "Add User" menu item, which then displays my Username form, which is basically a simple input box, when the user clicks the Submit button on the Username form I want the username to be added to the cboUsers drop down box on the Main form...something like this.
    Code:
    In Username form
    
    private void button1_Click(object sender, System.EventArgs e)
    {
       //Add the username to the Main forms Username dropdown list
       
       Main.cboUsername.Items.Add(txtUsername.Text);	
    }
    but obviously, it doesn't work that way...any ideas on how to get this to work?
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  2. #2
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    That code will work fine as long as:
    1. Main is the name of an instance of the form class, not the name of the actual class.
    2. cboUsername is declared as a public member instead of private or protected.
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  3. #3
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    you need to declare the username form slightly differently when showing it, here's a quick example i knocked up for you...
    in main ( IE the first Form )...
    VB Code:
    1. [Color=Blue]private[/COLOR] [Color=Blue]void[/COLOR] button1_Click([Color=Blue]object[/COLOR] sender, System.EventArgs e)
    2. {
    3.  
    4. username frmUsers=[Color=Blue]new[/COLOR] username() ; [Color=blue]
    5. base[/COLOR].AddOwnedForm(frmUsers); [Color=Green]//[/COLOR] [Color=Green]add[/COLOR] [Color=Green]it[/COLOR] [Color=Green]as[/COLOR] [Color=Green]an[/COLOR] [Color=Green]owned[/COLOR] [Color=Green]Form[/COLOR] [Color=Green].[/COLOR]
    6. frmUsers.Show() ;
    7.  
    8. }
    in the username Form ...
    VB Code:
    1. [Color=Blue]private[/COLOR] [Color=Blue]void[/COLOR] button1_Click([Color=Blue]object[/COLOR] sender, System.EventArgs e)
    2. {
    3.  
    4. main frmMain=(main)[Color=Blue]base[/COLOR].Owner;
    5. frmMain.cboUsername.Items.Add(txtUserName.Text) ;
    6.  
    7. }
    i'll include a zipped example to make it easier just incase
    Attached Files Attached Files
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  4. #4

    Thread Starter
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    AHA! that was the problem...the modifier for the combobox was still set to private...Thanks!
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

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