0

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;
        }
    }
Jesper Plantener
  • 229
  • 3
  • 16
  • i think the connect method should be in your form2 and call it upon(depending on what u want to do),1)on form load event or 2)user interaction(what the user wants to do).Also connections should be placed in a diferent class(access layer). – terrybozzio Jul 15 '13 at 14:04
  • You could have it as a singleton by sounds of it – Sayse Jul 15 '13 at 14:07

1 Answers1

0

A common way of doing what you are trying to do is to have you main form with all of your options on it. When the form loads, you launch the login form (usually in the main form's constructor or OnShow method). If the the login fails, you call Application.Exit(). Otherwise, you continue to the main form. Essentially, the main form is your controller.

Below is a very simple sample of what I mean..

public Form1()
{
    InitializeComponent();

    LoginForm loginForm = new LoginForm();
    DialogResult result = loginForm.ShowDialog();
    if (result != System.Windows.Forms.DialogResult.OK)
        Application.Exit();
}

private void CreateButton_Click(object sender, EventArgs e)
{
    CreateForm createForm = new CreateForm();
    createForm.Show();
}

private void DeleteButton_Click(object sender, EventArgs e)
{
    DeleteForm deleteForm = new DeleteForm();
    deleteForm.Show();
}

private void MoveButton_Click(object sender, EventArgs e)
{
    MoveForm moveForm = new MoveForm();
    moveForm.Show();
}
John Kraft
  • 6,811
  • 4
  • 37
  • 53
  • Thanks John! If I do it this way, and follow the other comments about making the connection in its own layer/class, where would you suggest I create the connection? Pass the username and password back to the controlling form, and making the connection from there, or make the connection in the login form, and pass the connection object back to the control form? Or another way? – Jesper Plantener Jul 15 '13 at 15:18
  • I'd create the connection at the highest possible level, then pass it down to all children. In this simple example, I'd probably do it in the main form constructor and keep it as a private variable on the form. – John Kraft Jul 15 '13 at 15:39
  • Just a little update. I noticed, that when doing the: if (result != System.Windows.Forms.DialogResult.OK) Application.Exit(); my application doesn't actually exit. I have to do Environment.exit(0) instead. Why is that? – Jesper Plantener Jul 16 '13 at 08:18
  • I don't know. I've never encountered that myself. Maybe this question can answer that for you... http://stackoverflow.com/q/905544/7495 – John Kraft Jul 16 '13 at 13:12