📄 editpc.cs
字号:
this.Text = "设置产品目录";
this.Load += new System.EventHandler(this.EditPC_Load);
this.groupBox1.ResumeLayout(false);
this.editArea.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private void EditPC_Load(object sender, System.EventArgs e) {
}
/// <summary>
/// 保存
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, System.EventArgs e) {
//名称不能为空
if(this.thisSign.Text == "" || this.thisName.Text.Trim() == ""){
MessageBox.Show("编号和产品名称不能为空!");
return;
}
if(!Public.SystemSignIsRight(this.thisSign.Text)){
MessageBox.Show("编号为空或格式不正确。" + Public.systemSignNotRight);
return;
}
if(this.edit){ //修改
try{
string sql = "Update ProductCatalog set signName='"+ this.thisName.Text +"',unitName='"+ this.unitName.Text +"',price="+ this.price.Text +",content='"+ this.memo.Text +"',modifyEr='"+ Public.userID +"',modifyDate='"+ DateTime.Now.ToString() +"',gg='"+ this.gg.Text +"' where treeID = '"+ this.thisSign.Text +"'";
OleDbCommand cmd = new OleDbCommand(sql,Public.conn);
Public.CloseConn();
Public.conn.Open();
cmd.ExecuteNonQuery();
Public.conn.Close();
this.LoadTree();
}catch(Exception ex){
MessageBox.Show(ex.Message.ToString());
return;
}
}else{ //新建,如果没有指定的autoID,这里则是将autoID默认为1,在下级建立档案
if(this.autoID == null){
autoID = "1";
this.addToLay = 1; //事先计算出来
}
//检查编号是否重复
try{
string sql = "select autoID productCatalog where treeID = '"+ this.thisSign.Text +"'";
OleDbDataAdapter dsa = new OleDbDataAdapter(sql,Public.conn);
DataSet _ds = new DataSet();
dsa.Fill(_ds,"t");
if(_ds == null && _ds.Tables[0].Rows.Count > 0){
MessageBox.Show("编号有重复,请确认后再保存!");
return;
}
_ds = null;
}catch{
//
}
//处理价格及数据类型
if(this.price.Text.Trim() == ""){
this.price.Text = "0.00";
}else{
try{
Decimal _price = Decimal.Parse(this.price.Text);
}catch{
MessageBox.Show("价格只能用数字(含小数点)表示!");
return;
}
}
//保存
try{
string sql = "Insert into ProductCatalog (treeID,parentID,depth,signName,unitName,price,content,addEr,addDate,gg)";
sql += " values ('"+ this.thisSign.Text +"',"+ this.autoID +","+ this.addToLay.ToString() +",'"+ this.thisName.Text +"','"+ this.unitName.Text +"',"+ this.price.Text +",'"+ this.memo.Text +"','"+ Public.userID +"','"+ DateTime.Now.ToString() +"','"+ this.gg.Text +"')";
OleDbCommand cmd = new OleDbCommand(sql,Public.conn);
Public.CloseConn();
Public.conn.Open();
cmd.ExecuteNonQuery();
Public.conn.Close();
this.LoadTree();
}catch(Exception ex){
MessageBox.Show(ex.Message.ToString());
return;
}
}
this.ClearEditArea();
}
/// <summary>
/// 初始化
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, System.EventArgs e) {
this.ClearEditArea();
this.button1.Enabled = false;
this.button2.Enabled = false;
}
/// <summary>
/// 递归添加树的节点
/// </summary>
/// <param name="parentID"></param>
/// <param name="pNode"></param>
public void CreateTree(int parentID,TreeNode pNode){
DataView dvTree = new DataView(this.ds.Tables[0]);
dvTree.RowFilter = "[PARENTID]="+parentID;
foreach(DataRowView row in dvTree){
if(pNode == null){ //根结点
TreeNode treeNode = this.treeView.Nodes.Add(row["signName"].ToString());
treeNode.Tag = row["treeID"].ToString();
treeNode.ExpandAll();
treeNode.ImageIndex = 0;
this.CreateTree(Int32.Parse(row["autoID"].ToString()),treeNode);
}else{
TreeNode treeNode = pNode.Nodes.Add(row["gg"].ToString() + " " + row["SignName"].ToString());
treeNode.Tag = row["treeID"].ToString();
treeNode.ExpandAll();
this.CreateTree(Int32.Parse(row["autoID"].ToString()),treeNode);
}
}
}
/// <summary>
/// 在树上点击右键时
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ClickInTreeView(object sender,MouseEventArgs e){
TreeNode tn = this.treeView.GetNodeAt(e.X,e.Y) as TreeNode;
if(tn!=null){
this.currNode = tn;
this.currSign = tn.Tag.ToString();
this.LoadNodeInfo(this.currSign);
this.menuItem1.Enabled = true;
this.menuItem2.Enabled = true;
this.menuItem5.Enabled = true;
this.button1.Enabled = false;
this.button2.Enabled = false;
if(tn.Parent == null){
this.menuItem1.Enabled = false;
this.ClearEditArea();
}
}else{
this.currNode = null;
this.currSign = null;
this.edit = false;
this.menuItem1.Enabled = false;
this.menuItem2.Enabled = false;
this.menuItem5.Enabled = false;
}
}
/// <summary>
/// 刷新目录结构
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void menuItem7_Click(object sender, System.EventArgs e) {
this.LoadTree();
}
/// <summary>
/// 新建本级档案
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void menuItem1_Click(object sender, System.EventArgs e) {
this.edit = false;
TreeNode pn = this.currNode.Parent;
this.addToLay = Int32.Parse(this.GetColVal(pn.Tag.ToString(),"depth")) + 1 ;
this.autoID = this.GetColVal(pn.Tag.ToString(),"autoID");
this.editArea.Text = "新建产品目录档案(上层节点:"+ pn.Text.ToString() +")";
this.thisSign.Enabled = true;
this.thisSign.Text = "";
this.thisName.Text = "";
this.unitName.Text = "";
this.price.Text = "0.00";
this.memo.Text = "";
this.gg.Text = "";
this.button1.Enabled = false;
this.button2.Enabled = false;
this.SetThisSign();
}
//新建下级档案
private void menuItem2_Click(object sender, System.EventArgs e) {
this.edit = false;
string dddd = this.GetColVal(this.currNode.Tag.ToString(),"depth");
if(dddd == null || dddd.Trim() == "") dddd = "0";
this.addToLay = Int32.Parse(dddd) + 1;
this.autoID = this.GetColVal(this.currNode.Tag.ToString(),"autoID");
this.editArea.Text = "新建产品目录档案(上层节点:"+ this.currNode.Text.ToString() +")";
this.thisSign.Enabled = true;
this.thisSign.Text = "";
this.thisName.Text = "";
this.unitName.Text = "";
this.price.Text = "0.00";
this.memo.Text = "";
this.gg.Text = "";
this.button1.Enabled = false;
this.button2.Enabled = false;
this.SetThisSign();
}
/// <summary>
/// 修改当前档案
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void menuItem4_Click(object sender, System.EventArgs e) {
//
}
/// <summary>
/// 删除当前档案
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void menuItem5_Click(object sender, System.EventArgs e) {
if(this.currNode != null && MessageBox.Show("删除后不可恢复,确定删除吗?","删除确认",MessageBoxButtons.OKCancel,MessageBoxIcon.Question) == DialogResult.OK){
try{
string autoID = this.GetColVal(this.currNode.Tag.ToString(),"autoID");
string sql = "delete from ProductCatalog where autoID = " + autoID;
OleDbCommand cmd = new OleDbCommand(sql,Public.conn);
Public.CloseConn();
Public.conn.Open();
cmd.ExecuteNonQuery();
Public.conn.Close();
this.LoadTree();
this.ClearEditArea();
this.currNode = null;
}catch(Exception ex){
MessageBox.Show(ex.Message.ToString());
Public.CloseConn();
return;
}
}
}
/// <summary>
/// 从ds中载入选中节点的信息
/// </summary>
private void LoadNodeInfo(string sign){
if(this.ds != null && sign != null){
for(int i=0;i<ds.Tables[0].Rows.Count;i++){
DataRow row = ds.Tables[0].Rows[i];
if(row["treeID"].ToString().Equals(sign) && row["depth"].ToString() != "0"){
this.thisSign.Text = row["treeID"].ToString();
this.thisName.Text = row["signName"].ToString();
this.unitName.Text = row["unitName"].ToString();
this.price.Text = row["price"].ToString();
this.memo.Text = row["content"].ToString();
this.currSign = this.thisSign.Text;
this.autoID = row["autoID"].ToString();
this.edit = true;
this.gg.Text = row["gg"].ToString();
this.editArea.Text = "编辑" + this.thisName.Text;
this.thisSign.Enabled = false;
}
}
}
}
/// <summary>
/// 返回指定字段值
/// </summary>
/// <param name="sign"></param>
/// <param name="colName"></param>
/// <returns></returns>
private string GetColVal(string sign,string colName){
if(this.ds != null && sign != null){
for(int i=0;i<ds.Tables[0].Rows.Count;i++){
DataRow row = ds.Tables[0].Rows[i];
if(row["treeID"].ToString().Equals(sign)){
return row[colName].ToString();
}
}
}
return null;
}
/// <summary>
/// 值变时
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void thisSign_TextChanged(object sender, System.EventArgs e) {
this.button1.Enabled = true;
this.button2.Enabled = true;
}
private void thisName_TextChanged(object sender, System.EventArgs e) {
this.button1.Enabled = true;
this.button2.Enabled = true;
}
private void unitName_TextChanged(object sender, System.EventArgs e) {
this.button1.Enabled = true;
this.button2.Enabled = true;
}
private void price_TextChanged(object sender, System.EventArgs e) {
this.button1.Enabled = true;
this.button2.Enabled = true;
}
private void memo_TextChanged(object sender, System.EventArgs e) {
this.button1.Enabled = true;
this.button2.Enabled = true;
}
/// <summary>
/// 取消编辑区域
/// </summary>
private void ClearEditArea(){
this.thisSign.Text = "";
this.thisName.Text = "";
this.unitName.Text = "";
this.price.Text = "0.00";
this.memo.Text = "";
this.gg.Text = "";
this.button1.Enabled = false;
this.button2.Enabled = false;
this.editArea.Text = "编辑";
this.edit = false;
//this.currNode = null;
this.currSign = null;
this.autoID = "1";
this.addToLay = 1;
this.thisSign.Enabled = true;
this.SetThisSign();
}
/// <summary>
/// 设置编号输入框
/// </summary>
private void SetThisSign(){
this.thisSign.Text = Public.GetNewInfoID("ProductCatalog","treeID");
//this.thisSign.Enabled = false;
}
private void editArea_Enter(object sender, System.EventArgs e) {
}
private void gg_TextChanged(object sender, System.EventArgs e) {
this.button1.Enabled = true;
this.button2.Enabled = true;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -