📄 frmproduct.cs
字号:
// tipNewProduct
//
this.tipNewProduct.AutomaticDelay = 150;
//
// frmProduct
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(566, 367);
this.Controls.Add(this.txtProdId);
this.Controls.Add(this.txtUnit);
this.Controls.Add(this.txtDesc);
this.Controls.Add(this.txtQty);
this.Controls.Add(this.txtProdNo);
this.Controls.Add(this.txtProdName);
this.Controls.Add(this.lblProdId);
this.Controls.Add(this.cboPrdCat);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.btnOK);
this.Controls.Add(this.lblUnit);
this.Controls.Add(this.lblProdCat);
this.Controls.Add(this.lblDesc);
this.Controls.Add(this.lblQty);
this.Controls.Add(this.lblPrdNo);
this.Controls.Add(this.lblPrdName);
this.Name = "frmProduct";
this.Text = "添加新产品";
this.Load += new System.EventHandler(this.frmProduct_Load);
this.ResumeLayout(false);
}
#endregion
private void frmProduct_Load(object sender, System.EventArgs e)
{
try
{
DataConnection.commnd.CommandText = "Select catCode from productcategories";
OleDbDataReader reader = DataConnection.commnd. ExecuteReader ();
while(reader.Read() )
{
cboPrdCat.Items.Add (reader.GetString (0));
}
reader.Close();
}
catch(Exception excp)
{
MessageBox.Show ("发生错误 "+excp.Message.ToString ());
}
}
private void btnCancel_Click(object sender, System.EventArgs e)
{
this.errProduct.SetError(this.cboPrdCat,"");
this.errProduct.SetError(this.txtQty,"");
this.errProduct.SetError(this.txtDesc,"");
this.errProduct.SetError(this.txtProdName,"");
this.errProduct.SetError(this.txtUnit,"");
this.Close();
}
public void ClearDetails()
{
this.txtProdName.Text = "";
this.txtDesc.Text = "";
this.cboPrdCat.Text = "";
this.txtUnit.Text = "";
this.txtProdNo.Text = "";
this.txtQty.Text = "";
this.txtProdId.Text = "";
}
public string GenerateProductId(string catCode, string flvlCatCode)
{
try
{
int srNoInt; int index=0,num;
string srNo=""; char[] srNoVal = new char[5];
string[] srNoStr=new string[100];
string subId, prodId;
char val;
string str = "Select productid from products where catcode = '"+catCode+"'";
DataConnection.commnd.CommandText = str;
OleDbDataReader reader=DataConnection.commnd.ExecuteReader();
srNoStr = new string[100];
num = 0;
if (reader.HasRows)
{
while (reader.Read())
{
srNo="";
subId = (reader.GetString(0)).Substring (4,5);
index = 0;
while(index < subId.Length)
{
val = Convert.ToChar(subId.Substring(index,1));
//Get the relevant characters containing product id
if (Char.IsNumber (val)) //check if each char is a number
{
if (Char.GetNumericValue(val)!=0) //check if it is non-zero
{
srNo+= val;
}
}
index++;
}//end of inner while loop
//now we have with us one prod id
srNoStr[num] = srNo;
num++;
}//end of outer while loop
}
if (num != 0) //if the table was initially empty num would be 0
{
Array.Sort(srNoStr);
int srNoLimit = srNoStr.Length -1;
srNoInt = int.Parse(srNoStr[srNoLimit])+1;
}
else
srNoInt = 1;
prodId = catCode + flvlCatCode;
reader.Close();
str = "Select count(*) from products";
DataConnection.commnd.CommandText =str;
reader = DataConnection.commnd.ExecuteReader();
int value;
if (reader.HasRows)
{
reader.Read();
value = reader.GetInt32(0)+1;
}
else
value = 1;
this.txtProdNo.Text = value.ToString ();
if (srNoInt <10)
prodId = prodId +"0000"+srNoInt.ToString ();
else if ((srNoInt >10)&& (srNoInt <100))
prodId=prodId +"000"+srNoInt.ToString ();
else if (srNoInt >100 && srNoInt <1000)
prodId=prodId +"00"+srNoInt.ToString ();
else
prodId=prodId +"0"+srNoInt.ToString ();
reader.Close ();
return prodId;
}
catch(Exception excp)
{
MessageBox.Show ("发生错误 "+excp.Message.ToString ());
return "";
}
}
private void cboPrdCat_Leave(object sender, System.EventArgs e)
{
try
{
bool validFlag = false;
foreach(string item in cboPrdCat.Items)
{
if (item.Equals(cboPrdCat.Text))
validFlag = true;
}
if (validFlag == false)
{
errorMsg = ("请输入有效的产品类别");
this.errProduct.SetError(this.cboPrdCat, errorMsg);
allValidated =false;
}
else
{
this.catCode = this.cboPrdCat.Text;
DataConnection.commnd.CommandText = "Select flvlcatCode from productcategories where catCode='"+this.catCode+"'";
OleDbDataReader reader = DataConnection.commnd. ExecuteReader();
if (reader.Read())
this.flvlCatCode = reader.GetString(0);
reader.Close ();
//autogenerate product id
this.txtProdId.Text= GenerateProductId(this.catCode,this.flvlCatCode);
}
}
catch(Exception excp)
{
MessageBox.Show ("发生错误 "+excp.Message.ToString ());
}
}
private bool chkNum(string str)
{
for(int i=0; i<str.Length;i++)
{
if(! Char.IsNumber(str,i))
{
return false;
}
}
return true;
}
private void txtProdName_Leave(object sender, System.EventArgs e)
{
if (this.txtProdName.Text.Trim() == "")
{
errorMsg = ("请输入有效的产品名称");
this.errProduct.SetError(this.txtProdName, errorMsg);
allValidated = false;
this.txtProdName.Focus();
}
else
{
DataConnection.commnd.CommandText = "Select * from products where productname='"+this.txtProdName.Text.Trim()+"'";
OleDbDataReader reader = DataConnection.commnd. ExecuteReader ();
if(reader.Read())
{
errorMsg = ("产品存在");
this.errProduct.SetError(this.txtProdName, errorMsg);
allValidated = false;
this.txtProdName.Focus();
}
else
{
errProduct.SetError(this.txtProdName,"");
allValidated= true;
}
reader.Close();
}
}
private void txtQty_Leave(object sender, System.EventArgs e)
{
if(this.txtQty.Text.Trim().Length == 0 || this.txtQty.Text == "0")
{
errorMsg = "必须输入数量";
// Set the ErrorProvider error with the text to display.
this.errProduct.SetError(this.txtQty, errorMsg);
this.txtQty.Focus();
}
else if (! this.chkNum(this.txtQty.Text))
{
errorMsg = "数量必须为数字";
this.txtQty.Focus();
// Set the ErrorProvider error with the text to display.
this.errProduct.SetError(this.txtQty, errorMsg);
}
else
{
errProduct.SetError(txtQty,"");
allValidated= true;
}
}
private void txtDesc_Leave(object sender, System.EventArgs e)
{
if (this.txtDesc.Text.Trim() == "")
{
errorMsg = ("请输入有效的产品说明");
this.errProduct.SetError(this.txtDesc, errorMsg);
allValidated = false;
this.txtDesc.Focus();
}
else
{
errProduct.SetError(this.txtDesc,"");
allValidated= true;
}
}
private void txtUnit_Leave(object sender, System.EventArgs e)
{
if (this.txtUnit.Text.Trim() == "")
{
errorMsg = ("请输入有效的单位");
this.errProduct.SetError(this.txtUnit, errorMsg);
allValidated =false;
this.txtUnit.Focus();
}
else
{
errProduct.SetError(this.txtUnit,"");
allValidated= true;
}
}// end of method
private void btnOK_Click(object sender, System.EventArgs e)
{
frmProductDetails.flag =true;
try
{
//check if all textboxes are filled
foreach (Control ctrl in this.Controls)
{
if (ctrl.GetType().ToString() != "System.Windows.Forms.NumericUpDown"&& ctrl.GetType().ToString() != "System.Windows.Forms.Label" && ctrl.GetType().ToString() != "System.Windows.Forms.Button" )
{
if (ctrl.Text =="")
allValidated = false;
}
}
if (allValidated)
{
this.catCode = cboPrdCat.Text;
DataConnection.commnd.CommandText = "Select FlvlCatCode from ProductCategories where catcode='"+catCode+"'";
OleDbDataReader reader = DataConnection.commnd.ExecuteReader();
reader.Read();
this.flvlCatCode = reader.GetString(0);
reader.Close ();
DataConnection.commnd.CommandText = "Select * from Products";
OleDbDataAdapter adapter = new OleDbDataAdapter (DataConnection.commnd.CommandText,DataConnection.oleconn);
DataSet ds = new DataSet();
adapter.Fill (ds);
DataRow drPrd = ds.Tables[0].NewRow();
drPrd["ProductId"] = this.txtProdId.Text;
drPrd["ProductName"] = txtProdName.Text;
drPrd["ProductNo"] = txtProdNo.Text;
drPrd["ProductDesc"] = txtDesc.Text;
drPrd["QOH"] = txtQty.Text;
drPrd["Units"] = txtUnit.Text;
drPrd["CatCode"] = catCode;
StockManagement stkMgmt = new StockManagement();
stkMgmt.AddProduct(drPrd);
MessageBox.Show ("Record successfully added");
this.Close();
}
else
MessageBox.Show ("未完成详细信息。不能添加");
}
catch(Exception excp)
{
MessageBox.Show ("发生错误 "+excp.Message.ToString ());
}
}
} //end of class
} //end of namespace
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -