【问题标题】:Prevent one form to open Twice while Passing some Data防止一个表单在传递一些数据时打开两次
【发布时间】:2014-01-30 17:22:00
【问题描述】:

我正在开发库存软件。在此软件中,当用户按下 Ctrl+Space 按钮时,系统将打开一个显示所有产品列表的表单 (frmProducts.cs)。在此表单上,用户可以按名称搜索产品,从该表单中选择产品,然后按“选择”按钮。所选产品将与产品代码一起显示在销售发票产品名称文本框中。

现在,当用户选择产品程序打开一个新的销售发票表单并且不将数据加载到旧表单中时,就会出现问题。程序应该显示已经打开的表格并填写了数据。

我写的打开产品信息的代码是:

  private void txtProductCode1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control && e.KeyCode == Keys.Space)
            {
               // frm4.Instance.Show();
                frmProducts cs = new frmProducts();
                cs.Show();
            }
        }

我在 frmProducts 选择按钮上写的代码是:

int ItemNo;
                string sql;

                if (lvProducts.SelectedItems.Count == 0)
                {
                    MessageBox.Show("Please Select Atleast one Column", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                else
                {
                    ItemNo = Convert.ToInt32(lvProducts.SelectedItems[0].Text.ToString());

                    sql = "";
                    sql += "SELECT * FROM ProductLog WHERE ProductLog.ItemNo = " + ItemNo + "";


                    SqlConnection cn = new SqlConnection();
                    SqlCommand rs = new SqlCommand();
                    SqlDataReader sdr = null;
                    clsConnection clsCon = new clsConnection();


                    clsCon.fnc_ConnectToDB(ref cn);


                    rs.Connection = cn;
                    rs.CommandText = sql;
                    sdr = rs.ExecuteReader();

                    this.Close();


                    frmSaleInvoice frm = new frmSaleInvoice(ref sdr);
                    frm.ShowDialog();                   

                    sdr.Close();
                    rs = null;
                    cn.Close();
                }

frminvoice 中捕获数据的代码是:

public frmSaleInvoice(ref SqlDataReader sdr)
    {

        InitializeComponent();

            while (sdr.Read())
            {
                txtProductCode1.Text = Convert.ToString(sdr["ProductCode"]);
            }            
    }        

我也在“选择”按钮上尝试了此代码,但它只是没有将任何内容加载到销售发票中:

frmSaleInvoice frm = new frmSaleInvoice(ref sdr);
                    if (frm.Visible)
                    {
                        frm.BringToFront();
                    }

也试过了

            frmSaleInvoice frm = new frmSaleInvoice(ref sdr);
            frmSaleInvoice.Instance.Show();

我也尝试用frmSaleInvoice.Instance.Show(); 传递值,但它不允许我传递像frmSaleInvoice.Instance.Show(ref sdr); 这样的数据

请帮帮我,我真的被困在里面了。

  • 要么告诉我如何通过实例传递值
  • 或任何其他方式来做到这一点?

任何建议将不胜感激。!

【问题讨论】:

    标签: c# mysql .net winforms reference


    【解决方案1】:

    像我创建的那样创建一个公共类 fnc_selectbtncCode(); frmProducts 表单中的数据并使用您的数据库连接将数据加载到此类中,因为它是公共的,您也可以访问其他类并且可以使用它的数据或只是用户单例方法就像 Zeeshan 建议的那样

    // 使用你已经使用过的相同构造函数

       InitializeComponent();
    
            while (sdr.Read())
            {
                txtProductCode1.Text = Convert.ToString(sdr["ProductCode"]);
            }            
    }  
    

    在表格2中你已经加载的数据是加载事件,在公共类中加载它

    private void txtProductCode1_KeyDown(object sender, KeyEventArgs e) { if (e.Control && e.KeyCode == Keys.Space) {

            if (e.Control && e.KeyCode == Keys.Space)
            {
                int a;
    
                frmProducts cs = new frmProducts();
                cs.ShowDialog();
    
                a = cs.fnc_selectbtncCode();
    
                txtProductCode1.Text = Convert.ToString(a);
            }
        }
        if (e.KeyCode == Keys.Enter)
        {
            txtQty.Focus();
        }          
    }
    

    frmProducts中函数“fnc_selectbtncCode”的代码为:

      public int fnc_selectbtncCode()
                {              
    
                    if (lvProducts.SelectedItems.Count == 0)
                    {
                        MessageBox.Show("Please Select Atleast one Column", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return 0;
                    }
    
                    else
                    {
                        ItemNo = Convert.ToInt32(lvProducts.SelectedItems[0].Text.ToString());
                        return ItemNo;
                    }
    
                }
    

    【讨论】:

    • 谢谢老兄,非常简单的解决方案...非常感谢:)
    【解决方案2】:

    只需遵循单例方法即可。

    或

    以发票形式创建一个像 loadData(parameter) 这样的公共函数。 然后在调用 loadData 时用新数据填写发票表格

    //第一次使用这个构造函数 公共 frmSaleInvoice(参考 SqlDataReader sdr) {

        InitializeComponent();
    
            while (sdr.Read())
            {
                txtProductCode1.Text = Convert.ToString(sdr["ProductCode"]);
            }            
    }  
    

    第二次或任何其他时间使用

        public void LoadData(ref SqlDataReader sdr)
            {
    
                    while (sdr.Read())
                    {
                        txtProductCode1.Text = Convert.ToString(sdr["ProductCode"]);
                    }            
                    Activate();
            }  
    

    【讨论】:

    • 你能提供一些例子吗兄弟?或者你可以更新我的代码,我会从中学习
    【解决方案3】:

    我认为您误解了如何将数据从子表单传播到其父表单的方式。 有很多方法可以实现您想要的功能(如果我理解正确的话)。我将概述其中两个: 首先,让我们定义 - 您的 frmSaleInvoice 是您的父表单,而在 Ctrl+Space 之后显示的表单是子表单。

    1) 对话方式: 更改以这种方式显示孩子的方法:

    private void txtProductCode1_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.Control && e.KeyCode == Keys.Space)
                {
                    frmProducts cs = new frmProducts();
                    if (cs.ShowDialog() == DialogResult.OK)
                    {
                        //it says you that user didn't cancel child form without selecting item
                        //parent form is frozen until user finishes his selecting on child form
                        //you have to create some public property in child form which you fill after
                        //user clicks on Select then you can do following:
                        this.MyTextbox.Text = cs.SomePropertyIveCreatedToHoldData;
                    }
    
                }
            }
    

    2) 事件: 第二种方法是使用事件,但我认为它比 ShowDialog 复杂得多。事件还允许您使此过程不冻结您的父表单(并非在所有情况下都有好处),它适用于您希望让用户在子表单处于活动状态时使用父表单进行操作的情况。虽然您对此有疑问,但您应该首先尝试 ShowDialog 方式,然后再决定是否要使用事件,因为实现此功能的方式确实非常复杂。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-21
      • 1970-01-01
      相关资源
      最近更新 更多