简体   繁体   English

如何使用Windows窗体使用同一事件的多种窗体?

[英]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. 我在SO-pt上问了同样的问题,但似乎没有人理解这个问题,尽管我已明确指出。

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 . 如您所见,我正在从名为mnuRegisterToolStripMenuItem调用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. 现在,我在Load`事件中自定义此表单的许多属性,这将使其更具体地用于注册客户。

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? 那可能吗?

If you find yourself configurating a great deal of UI elements, then just create separate Forms. 如果发现自己配置了大量UI元素,则只需创建单独的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. 但是看起来您仅更改了几个UI元素(如标签),所以还不错。

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: 将配置逻辑移到Form上的两个独立方法中,例如ConfigureForRegistrationConfigureForViewingRegistration ,然后在实例化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: 或者,您可以为每个可能的视图创建一个枚举,并在实例化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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM