Reference method in 1.cs in 2.cs

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mcfly1204
    New Member
    • Jul 2007
    • 233

    Reference method in 1.cs in 2.cs

    I have a Windows Form Application with all classes under the same namespace. I have 1.cs that contains a method title Login(). I create an instance of 2.cs which is a login window. Upon clicking a submit button on the login window, I would like to fire a Login(). How do I call Login() from the 2.cs? I have made Login() public, and I would assume that this would be a simple task, but I am at a loss. Thanks in advance.

    **edit**
    I am not used to working with Windows Forms, and as such, do not know how to properly call objects, in the same namespace, but in a different class. In 2.cs, do I create a new instance of the form on 1.cs, and then access the method as such. For example:

    Code:
    private void btnSubmit_Click(object sender, EventArgs e)
            {
                string userPassword = txtAdminPassword.Text;
                string adminPassword = AdminPassword();
    
                if (userPassword == adminPassword)
                {
                    frmInkFormulas form = new frmInkFormulas();
                    form.Login();
    
                    this.Close();
                }
            }
  • mcfly1204
    New Member
    • Jul 2007
    • 233

    #2
    Code:
    formname form1 = (formname)Application.OpenForms["formname"];
                    form1.Login();
    Where formname is the name of the form from 1.cs that contains the method I am attempting to call from the the form on 2.cs. Note that the method being called needs to be set to public.

    Comment

    • Christian Binder
      Recognized Expert New Member
      • Jan 2008
      • 218

      #3
      If you want to show a form before you're calling the Loin()-method , you have to call form.Show() before Line 9.
      Or is there any other thing, that doesn't work well?

      Comment

      • mcfly1204
        New Member
        • Jul 2007
        • 233

        #4
        The form I want to show is open in the background, so when I call this.close(), that form has the focus.

        Comment

        Working...