void companyControl1_MouseDown(object sender, MouseEventArgs e) { panel1.Cursor = Cursors.NoMove2D; CompanyControl nv = (CompanyControl)sender; nv.BackColor = cl1; nv.txtCode.Text = "Code"; nv.txtName.Text = "Name"; panel1.Controls.Add(nv); nv.BringToFront(); }
void companyControl1_MouseUp(object sender, MouseEventArgs e) { panel1.Cursor = Cursors.Default; CompanyControl nv = new CompanyControl(); nv.Location = vitri; panelControl1.Controls.Add(nv); ControlMover.Init(nv); nv.MouseUp += companyControl1_MouseUp; nv.MouseDown += companyControl1_MouseDown; TimChaConMoi((CompanyControl)sender); }
List <CompanyControl> timchacon(CompanyControl cha) { foreach (CompanyControl con in dstemp) { if (con.Tag.ToString() == cha.Name.ToString()) { chacon.Add(con); timchacon(con);//Tim tiep cac con cua Control con } } return(chacon); }
void lbltemp_MouseUp(object sender, MouseEventArgs e) { panel1.Cursor = Cursors.Default; panel1.Paint += panel1_Paint;//Kich hoat lai su kien paint panel1.Refresh(); CompanyControl tempchacon = (CompanyControl)sender; CompanyControl trai = new CompanyControl(); CompanyControl phai = new CompanyControl(); CompanyControl kq = new CompanyControl(); //Tim cha gan nhat ben phai for (int i = tempchacon.Location.X + rong; i < tempchacon.Location.X + rong * 2; i++)//khoang cach tim kiem tinh theo ben phai { Control tk = panel1.GetChildAtPoint(new Point(i, tempchacon.Location.Y - caomotcap)); if (tk is CompanyControl) { phai = (CompanyControl)tk; break; } } //Tim cha gan nhat ben trai for (int i = tempchacon.Location.X; i > tempchacon.Location.X - rong; i--)//Khoang cach tim kiem theo ben trai { Control tk = panel1.GetChildAtPoint(new Point(i, tempchacon.Location.Y - caomotcap)); if (tk is CompanyControl) { trai = (CompanyControl)tk; break; } } //So sanh 2 cha xem cha nao gan hon if (Math.Abs(tempchacon.Location.X - trai.Location.X) < Math.Abs(tempchacon.Location.X - phai.Location.X))//Neu cha trai gan hon cha phai { kq = trai; } else { kq = phai; } if (kq.txtName.Text != "" && tempchacon.Tag.ToString() != kq.Name) { DialogResult r; r = MessageBox.Show(String.Format("'{0}' sẽ thuộc '{1}'.Bạn có muốn thay đổi?", tempchacon.txtName.Text, kq.txtName.Text), "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1); if (r == DialogResult.Yes)//Neu dong y thi luu xuong CSDL { string tentablecon = tempchacon.tentable; string macha = kq.tentable.Substring(3); string macon = tempchacon.tentable.Substring(3); DBEngine.exec(String.Format("Update {0} set {1}ID ={2} where {3}ID= {4}", tentablecon, macha, kq.ID, macon, tempchacon.ID)); LoadTree(); } } }
void TimChaConMoi(CompanyControl tempchacon) { if (tempchacon.Location.Y < caomotcap * LevelMax) { CompanyControl cha = new CompanyControl(); CompanyControl cungcap = new CompanyControl(); int kcchacony = 10000; int kchaconx = 10000; //Tim cha foreach (CompanyControl controlcu in dstemp) { if (controlcu.Location.Y < tempchacon.Location.Y) { int khoangcachchacony = Math.Abs(controlcu.Location.Y - tempchacon.Location.Y); int khoangcachchaconx = Math.Abs(controlcu.Location.X - tempchacon.Location.X); if (khoangcachchacony <= kcchacony && khoangcachchaconx <= kchaconx) { cha = controlcu; kcchacony = khoangcachchacony; kchaconx = khoangcachchaconx; } } } //Tim cung cap foreach (CompanyControl controlcu in dstemp) { if (controlcu.Tag.ToString() == cha.Name.ToString()) { cungcap = controlcu; break; } } DialogResult r; r = MessageBox.Show(String.Format("'{0}' sẽ thuộc '{1}'.Bạn có muốn cập nhật?", tempchacon.txtName.Text, cha.txtName.Text), "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1); if (r == DialogResult.Yes) { string tentable = cungcap.tentable; string matable = tentable.Substring(3); string matablecha = cha.tentable.Substring(3); if (matablecha != "Company") { DBEngine.exec(String.Format("Insert into {0} ({1}ID,{2}Code,{3}Name) values({4},'{5}','{6}')", tentable, matablecha, matable, matable, cha.ID, tempchacon.txtCode.Text, tempchacon.txtName.Text)); } else { DBEngine.exec(String.Format("Insert into {0} ({1}Code,{2}Name) values('{3}','{4}')", tentable, matable, matable, tempchacon.txtCode.Text, tempchacon.txtName.Text)); } LoadTree(); } } }
void lbltemp_MouseMove(object sender, MouseEventArgs e) { CompanyControl tempchacon = (CompanyControl)sender; CompanyControl trai = new CompanyControl(); CompanyControl phai = new CompanyControl(); CompanyControl kq = new CompanyControl(); //Tim cha gan nhat ben phai for (int i = tempchacon.Location.X + rong; i < tempchacon.Location.X + rong * 2; i++)//khoang cach tim kiem tinh theo ben phai { Control tk = panel1.GetChildAtPoint(new Point(i, tempchacon.Location.Y - caomotcap)); if (tk is CompanyControl) { phai = (CompanyControl)tk; break; } } //Tim cha gan nhat ben trai for (int i = tempchacon.Location.X; i > tempchacon.Location.X - rong; i--)//Khoang cach tim kiem theo ben trai { Control tk = panel1.GetChildAtPoint(new Point(i, tempchacon.Location.Y - caomotcap)); if (tk is CompanyControl) { trai = (CompanyControl)tk; break; } } //So sanh 2 cha xem cha nao gan hon if (Math.Abs(tempchacon.Location.X - trai.Location.X) < Math.Abs(tempchacon.Location.X - phai.Location.X))//Neu cha trai gan hon cha phai { kq = trai; } else { kq = phai; } if (kq.txtName.Text != "" && tempchacon.Tag.ToString() != kq.Name) { panel1.Cursor = Cursors.Cross; } else { panel1.Cursor = Cursors.NoMove2D; } }
void lbltemp_Click(object sender, EventArgs e) { if (lbltemp != null)//Tra lai mau cac Control click lan truoc { lbltemp.BackColor = cl1; foreach (CompanyControl con in chacon) { con.BackColor = cl1; } } chacon.Clear(); CompanyControl cha = (CompanyControl)((Control)sender).Parent; cha.BringToFront(); cha.BackColor = cl2; timchacon(cha); foreach (CompanyControl con in chacon)//To mau cac Control Click lan nay { con.BackColor = cl2; } lbltemp = cha;//Gan lai gia tri cho CompanyControltemp }
CompanyControl CreateControl(string ten, string tagname, string tenhienthi, string tablename, string codename, int id, int parentID) { //Add CompanyControl CompanyControl lbltemp = new CompanyControl(); lbltemp.Name = ten; lbltemp.Tag = tagname; lbltemp.Size = new Size(rong, cao); lbltemp.BackColor = cl1; lbltemp.Text = tenhienthi; lbltemp.tentable = tablename; lbltemp.txtcode = codename; lbltemp.txtname = tenhienthi; lbltemp.ID = id; lbltemp.ParentID = parentID; lbltemp.txtCode.GotFocus += lbltemp_Click; lbltemp.txtName.GotFocus += lbltemp_Click; lbltemp.EnabledChanged += lbltemp_EnabledChanged; lbltemp.MouseUp += lbltemp_MouseUp; lbltemp.MouseDown += lbltemp_MouseDown; lbltemp.MouseMove += lbltemp_MouseMove; ControlMover.Init(lbltemp, ControlMover.Direction.Horizontal); return(lbltemp); }
void LoadData(int ID, int Level) { //Name CompanyControl cha = Name+ID=tag CompanyControlcon //caomotcap*level ra vi tri y cua CompanyControl List <DataRow> dscon = (from DataRow dr in dstree.Rows where Convert.ToInt32(dr["ParentID"]) == ID && Convert.ToInt32(dr["ControlLevel"]) == Level + 1 select dr).ToList(); if (dscon.Count == 0)//Neu khong co con { DataRow dr1 = ((from DataRow dr in dstree.Rows where Convert.ToInt32(dr["ID"]) == ID && Convert.ToInt32(dr["ControlLevel"]) == Level select dr).ToList())[0]; string tenhienthi = Convert.ToString(dr1["Name"]); string ten = tenhienthi + ID.ToString(); string tagname = Convert.ToString(dr1["ParentName"]) + Convert.ToString(dr1["ParentID"]); string tablename = Convert.ToString(dr1["tablename"]); string codename = Convert.ToString(dr1["Code"]); int id = Convert.ToInt32(dr1["ID"]); int parentID = Convert.ToInt32(dr1["ParentID"]); CompanyControl lbltemp = CreateControl(ten, tagname, tenhienthi, tablename, codename, id, parentID); dstemp.Add(lbltemp); if (Level == LevelMax)//Neu la cap cao nhat { lbltemp.Location = new Point(a.Max(), caomotcap * Level); panel1.Controls.Add(lbltemp); a[Level] = lbltemp.Location.X + rong + khoangcach; } else { lbltemp.Location = new Point(a.Max(), caomotcap * Level);//vi tri x se bang vi tri cua CompanyControl cuoi cung cap sau no panel1.Controls.Add(lbltemp); a[Level] = lbltemp.Location.X + rong + khoangcach; } } else//Neu co con { foreach (DataRow dr in dscon) { LoadData(Convert.ToInt32(dr["ID"]), Convert.ToInt32(dr["ControlLevel"]));//Load danh sach con } DataRow dr1 = ((from DataRow dr in dstree.Rows where Convert.ToInt32(dr["ID"]) == ID && Convert.ToInt32(dr["ControlLevel"]) == Level select dr).ToList())[0]; string tenhienthi = Convert.ToString(dr1["Name"]); string ten = tenhienthi + ID.ToString(); string tablename = Convert.ToString(dr1["tablename"]); string codename = Convert.ToString(dr1["Code"]); int id = Convert.ToInt32(dr1["ID"]); int parentID = Convert.ToInt32(dr1["ParentID"]); //Add CompanyControl CompanyControl lbltemp; string targname; if (Level > 0) { targname = Convert.ToString(dr1["ParentName"]) + Convert.ToString(dr1["ParentID"]); } else { targname = ""; } lbltemp = CreateControl(ten, targname, tenhienthi, tablename, codename, id, parentID); List <CompanyControl> lbltemp1 = (from p in dstemp where p.Tag.ToString() == lbltemp.Name.ToString() select p).ToList(); if (lbltemp1.Count > 0) { int x = (lbltemp1[0].Location.X + lbltemp1.Last().Location.X) / 2; lbltemp.Location = new Point(x, caomotcap * Level); if (lbltemp.Location.X <= lbltemp1.Last().Location.X) { a[Level] = lbltemp1.Last().Location.X + rong + khoangcach; } } panel1.Controls.Add(lbltemp); dstemp.Add(lbltemp);//Add CompanyControl vao list } }