How to pass variables between forms

Hi, I have a login form and the main form of my program, I would like to pass the username that the user typed in the login form to the main form, could somebody help me with this please. Thank you in advance.


Answer this question

How to pass variables between forms

  • Rachita

    Hi,

    This link may answer your question: http://themightycoder.spaces.live.com/blog/cns!EBFBA22CD769E10B!144.entry 

    Quote:

    Access a value in one form i.e. the value contained in a text box and use that in another form, possibly to set a label in the original form. 
    So below is a small fully functioning example (copy and build at will) which shows a form, Form1, with a button and a label. 
    When the button is clicked it opens up an instance of Form2 which has a textbox.  when the user closes Form2 the label in Form1 is updated with the value the user entered into the textbox.
     
    using System;
    using System.Drawing;
    using System.Windows.Forms;
     
    namespace

    WindowsApplication2

    {
       static class Program
       {
          /// <summary>
          /// The main entry point for the application.
          /// </summary>
          [STAThread]
          static void Main()
          {
             Application.EnableVisualStyles();
             Application.SetCompatibleTextRenderingDefault(false);
             Application.Run(new Form1());
          }
       }
     
       class Form1 : Form
       {
          private Label label1;
          private Button button;
     
          public Form1()
          {
             this.label1 = new Label();
             this.button = new Button();
             this.button.Location = new Point(100, 100);
             this.button.Click += new EventHandler(button_Click);
             this.Controls.Add(this.label1);
             this.Controls.Add(this.button);
          }
     
          void button_Click(object sender, EventArgs e)
          {
              Form2 f2 = new Form2();
              f2.ShowDialog();
     
              this.label1.Text = f2.UserEnteredValue;
           }
       }
     
       class Form2 : Form
       {
          private TextBox textBox1;
     
          public Form2()
          {
             this.textBox1 = new TextBox();
             this.Controls.Add(this.textBox1);
          }
     
          public string UserEnteredValue
          {
             get
             {
                return this.textBox1.Text;
             }
          }
       }
    }

    Thanks



  • NathanInNashville

    yes, an example would be good, please

  • Dale17677

    To do this the 'Correct' way (ie, the way it happens in the .Net framework), I would set the DialogResult of the form, and check for it in the return value of ShowDialog():


    private void btnOK_Click(object sender, EventArgs e)

    {

    this.DialogResult = DialogResult.OK;

    this.Hide();

    }


    private void btnCancel_Click(object sender, EventArgs e)

    {

    this.DialogResult = DialogResult.Cancel;

    this.Hide();

    }


    And check for it like this:


    private string username;

    private void btnLogin_Click(object sender, EventArgs e)

    {

    LoginForm loginForm = new loginForm();

    if (loginForm.ShowDialog == DialogResult.OK)

    {

    username = loginForm.getUsername();

    }

    }




  • testqh

    Say User Clicks the Submit Button underneath the Username and Password. Your username and password is tied to say user1TextBox and pass1TextBox...

    Just double click on the Submit button, it will automatically create the method for you if you are using VS 2003 or VS 2005.

    In that method do something like this :

    string username = user1TextBox.Text;

    string password = pass1TextBox.Text;

    // This is for Verification

    Now on this login form create an instance of your Main form...

    MainForm form = new MainForm();

    In your MainForm you have some method which verifies your Username and Password something like this :

    public bool CheckUserPass (string username, string password)

    {

    //Verify and return the result

    }

    Now you call the above function with form.CheckUserPass(username, password); from the Login form and your autnetication will go through....

    // If you only need to send the Username and Password to the main form, what you can do in that case is do a set and get method...

    // After you do that, you call the get function from the main form to get the USername and PAssword....

    Hopefully this helps.


  • Bill Gates II

    Hi !

    You should be able to this like that::

    Let doShowForm() be your method to display the form. Then you should define it like "public string doShowForm()"
    The method should display the form modal and return the edit's value on closing.

    If you need a code example - ask me :/\]

    regards

  • orouit

    No problem.. here it comes::

    I created a project with two forms: LoginForm and MainForm.
    On the main form I places a button and in it's onClick script I placed

    private void button1_Click(object sender, EventArgs e)
    {
    string lLoginName;
    LoginForm lLoginForm = new LoginForm(); // create instance of form
    lLoginName= lLoginForm.doGetLoginName(); // store return value of method here
    }


    This one will show the form and return users login.
    In the code of the login form you should type::

    public string doGetLoginName()
    {
    this.ShowDialog(); // show modal => wait for an modalResult <> 0
    return this.edtPassword.Text; // return value of edit-field
    }


    Your login-form should have an cancel and ok button - each of them should either cancel all the stuff or process the input.
    I did not do that here... so everything returned by the form will be stored in lLoginName.....

    regards

  • Newbie Kam

    Another way is to transfer the parameters in the constructors of the form (or its offspring).

    FYI: http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=1336126&SiteID=1

    Thanks



  • How to pass variables between forms