My issue here is that I am developing a tiny application to do some operations on a SCCM 2012 server.
For the moment it is just creating, deleting and moving folders around.
What I want to do is have a form where you log in. This is already done. From here you should choose what kind of operation you want do to (eg. move or delete). Then a new form should pop up, and you do your magic here.
This is all working perfectly, however, I do not think I am doing it the "proper" way. Right now, I am passing the connection object to the constructor to all of the created forms, and the forms are created inside each other. Does it make sense?
What I would like to do, is have some sort of control class, from where I create the login form, decide if it should be hidden or shown, and pass the connection object to where i'd like it to go, when it has been returned/saved.
My problem is, that when I create the form, all of the control flow goes to that form. I would prefer if I could keep the flow in the controlling class, and from here control if the form is hidden/shown according to a successfull login.
I hope you can understand my question, and might have a solution?
Here is a code snippet of form1:
 public Form1()
    {
        InitializeComponent();
    }
    private void button2_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        userName = textBox1.Text;
        password = textBox2.Text;
       getConnection = Connect(COMPUTER, userName, password);
       if (connection != null)
       {
           Form2 form2 = new Form2(connection);
           form2.Show();
           this.Hide();
       }
    }
    public WqlConnectionManager Connect(string serverName, string userName, string userPassword)
    {
        try
        {
            SmsNamedValuesDictionary namedValues = new SmsNamedValuesDictionary();
            WqlConnectionManager connection = new WqlConnectionManager(namedValues);
            if (System.Net.Dns.GetHostName().ToUpper() == serverName.ToUpper())
            {
                // Connect to local computer.
                connection.Connect(serverName);
            }
            else
            {
                // Connect to remote computer.
                connection.Connect(serverName, userName, userPassword);
            }
            return connection;
        }
        catch (SmsException e)
        {
            MessageBox.Show("Failed to connect. Error: " + e.Message);
            return null;
        }
        catch (UnauthorizedAccessException e)
        {
            MessageBox.Show("Failed to authenticate. Error: " + e.Message);
            return null;
        }
    }