Ezequiel
Ezequiel

Reputation: 3

How can I use multiple forms of the same event using windows forms?

I asked this same question on SO-pt but no one seemed to understand the question, though I made it clear.

I have a form that I'll use to

  1. Register customers in the database.
  2. Alter any of the registers.
  3. Show the registers.

Here I'm instantiating this form:

private void mnuRegister_Click(object sender, EventArgs e)
{
    var frmRegister = new frmRegisterScreen();
    frmRegister.Show();
}

As you can see, I'm calling the form from within a ToolStripMenuItem called mnuRegister.

Now, there are a number of properties from this form that I'm customizing at the Load` event, that'll make it more specific for registering the customers.

Below is the code:

private void frmRegisterScreen_Load(object sender, EventArgs e)
{
    //set the database connection and the Sql command to be used
    string conString = "Server = .\\sqlexpress; trusted_connection = yes; database=he_dados;";
    SqlConnection con = new SqlConnection(conString);

    string sel = "SET DATEFORMAT dmy;\n" //set date format to dd//mm/yyyy
                        + "Insert into Customer(" +
                         "Name,IDCard,Phone,Address,Observation)" +
                         "values(" +
                         "'" + txtName.Text +
                         "','" + mskIDCard.Text +
                         "','" + mskPhone.Text +
                         "','" + txtAddress.Text +
                         "','" + txtObs.Text + "');";    
    SqlCommand selCmd = new SqlCommand(sel, con);

    //set the form properties relate to the customer registration
    lblMain.Text = "Register Customer";
    tsbSave.Text = "Save Changes";
}

As you can see, this code is obviously intended to insert data in a table.

Now, what I want to do is to call another instance of this form:

private void mnuViewRegister_Click(object sender, EventArgs e)
{
    var frmViewRegister = new frmRegisterScreen();
    frmViewRegister.Show();
}

Then I want to set specific properties, required for me to make a simple query using the same form, for example:

private void frmRegisterScreen_Load(object sender, EventArgs e)
{
    //set the database connection and the Sql command to be used
    string conString = "Server = .\\sqlexpress; trusted_connection = yes; database=he_dados;";
    SqlConnection con = new SqlConnection(conString);

    string sel = "Select * from Customer;";    
    SqlCommand selCmd = new SqlCommand(sel, con);

    //set the form properties relate to the customer registration
    lblMain.Text = "View Customer Registers";
    tsbSave.Text = "View";
}

In other words, I would like to have event calls specific to the instance of the form, instead of having one event that's valid for any of the instances.

Is that possible?

Upvotes: 0

Views: 245

Answers (1)

Grant Winney
Grant Winney

Reputation: 66449

If you find yourself configurating a great deal of UI elements, then just create separate Forms. It's not like they cost you anything, and they'll be easier to maintain. But it looks like you're only changing a couple of UI elements (like the label), so that's not too bad.

Either move the configuration logic into two separate methods on the Form, like ConfigureForRegistration and ConfigureForViewingRegistration, and then call the appropriate one when you instantiate the Form:

var frmRegister = new frmRegisterScreen();
frmRegister.ConfigureForRegistration();
frmRegister.Show();

Or you could create an enumeration for each possible view, and pass a value in when you instantiate the Form:

public enum ScreenOption
{
    Register,
    AlterRegister,
    ViewRegister
}

public class frmRegisterScreen
{
    public frmRegisterScreen(ScreenOption option)
    {
        switch (option)
        {
            case ScreenOption.ViewRegister:
                //set the database connection and the Sql command to be used
                string conString = "Server = .\\sqlexpress; trusted_connection = yes; database=he_dados;";
                SqlConnection con = new SqlConnection(conString);
                break;
            ...
        }
    }
}

var frmRegister = new frmRegisterScreen(ScreenOption.ViewRegister);
frmRegister.Show();

Upvotes: 1

Related Questions