我在开发库存软件。在本软件中,当用户按下Ctrl+Space按钮时,系统将打开一个显示所有产品列表的表单(frmProducts.cs)。在此表单上,用户可以按名称搜索产品,从此表单中选择产品,然后按下“选择”按钮。选定的产品wll将与产品代码一起显示在“销售发票”产品名称文本框中。
现在,当用户选择产品程序时,打开一个新的销售发票表单,而不将数据加载到旧的表单中,就会出现问题。程序应该显示已经打开的表单,并填写数据。
我为打开产品信息而编写的代码是:
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);这样的数据
请帮帮我,我真的被困在里面了。
如有任何建议,将不胜感激。
发布于 2014-02-01 11:18:08
创建一个公共类,就像我已经创建了fnc_selectbtncCode();frmProducts形式的数据,并使用db连接将数据加载到该类中,仅仅因为它是公共的,您也可以访问其他类,也可以像Zeeshan建议的那样使用它的数据或用户Singleton方法
//使用已使用的构造函数
InitializeComponent();
while (sdr.Read())
{
txtProductCode1.Text = Convert.ToString(sdr["ProductCode"]);
}
} 在表单2中,加载它的load事件的数据,将其加载到公共类中
私有空txtProductCode1_KeyDown(对象发送方,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();
}
}函数"fnc_selectbtncCode“在frmProducts中的代码是:
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;
}
}发布于 2014-01-30 17:31:08
只需遵循单例的方法。
或
在发票形式中创建一个像loadData(参数)这样的公共函数。然后,当调用loadData时,用新的数据填充发票表
//首次使用此构造函数公共frmSaleInvoice(ref 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();
} 发布于 2014-01-31 08:47:17
我认为您误解了如何将数据从子窗体传播到其父窗体。实现功能的方法有很多种(如果我对您的理解是正确的)。我将概述其中的两个:首先,让我们定义-您的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方式,然后如果您想要使用事件,就应该做出决定,因为实现这种功能的方法非常复杂。
https://stackoverflow.com/questions/21463239
复制相似问题