/* * 处理datagridview的点击事件 * - 第一列的checkbox * - 启动、挂起、关机按钮 */ private void dataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e) { //MessageBox.Show("您单击的是第["+e.RowIndex+"]行第["+e.ColumnIndex+"列],单元格的内容是["+this.dataGridView.Rows[e.RowIndex].Cells[2].Value+"]"); if (e.ColumnIndex == 0 && e.RowIndex != -1) //checkbox { DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)this.dataGridView.Rows[e.RowIndex].Cells["check"]; if (Convert.ToBoolean(checkCell.Value)) { checkCell.Value = false; } else { checkCell.Value = true; } } else if (e.ColumnIndex == 7 || e.ColumnIndex == 8 || e.ColumnIndex == 9) //启动、挂起、停止按钮 { Vm vm = new Vm(); vm.control(e.ColumnIndex - 7, this.dataGridView.Rows[e.RowIndex].Cells["path"].Value.ToString()); refreshAll_Click(sender, e); } else if (e.ColumnIndex == 10) { vmFile.vmHost host = new vmFile.vmHost(); host.id = this.dataGridView.Rows[e.RowIndex].Cells["id"].Value.ToString(); host.name = this.dataGridView.Rows[e.RowIndex].Cells["name"].Value.ToString(); host.path = this.dataGridView.Rows[e.RowIndex].Cells["path"].Value.ToString(); editForm(sender, e, false, host); refreshAll_Click(sender, e); } }
//“编辑窗口”的“确定”按钮事件 private void editYesButton_Click(object sender, EventArgs e) { Button btn = (Button)sender; Form fm = (Form)btn.Parent; Label idL = (Label)fm.Controls[fm.Controls.IndexOfKey("id")]; TextBox nameB = (TextBox)fm.Controls[fm.Controls.IndexOfKey("name")]; TextBox pathB = (TextBox)fm.Controls[fm.Controls.IndexOfKey("path")]; Label warnL = (Label)fm.Controls[fm.Controls.IndexOfKey("warn")]; //窗口类型:添加?编辑? bool flag = (idL.Text.Length == 0); string id = idL.Text.Trim(); string name = nameB.Text.Trim(); string path = pathB.Text.Trim(); vmFile vfile = new vmFile(); if (name.Length == 0) { warnL.Text = "虚拟机名称不能为空"; return; } if (path.Length == 0) { warnL.Text = "虚拟机位置不能为空"; return; } if (flag && vfile.isVmHostExist(id)) { warnL.Text = "虚拟机[" + name + "]已经存在"; return; } vmFile.vmHost host = new vmFile.vmHost(); host.id = id; host.name = name; host.path = path; if (flag) { host.id = System.Guid.NewGuid().ToString(); vfile.addVmHost(host); } else { vfile.editVmHost(host); } fm.Close(); }
//“导入窗口”的yes按钮事件 private void importYesButton_Click(object sender, EventArgs e) { Form iForm = (Form)((Button)sender).Parent; DataGridView iView = (DataGridView)((Panel)iForm.Controls["iPanal"]).Controls["iView"]; vmFile vfile = new vmFile(); for (int i = 0; i < iView.Rows.Count; i++) { vmFile.vmHost host = new vmFile.vmHost(); host.id = System.Guid.NewGuid().ToString(); host.name = iView.Rows[i].Cells["name"].Value.ToString(); host.path = iView.Rows[i].Cells["path"].Value.ToString(); vfile.addVmHost(host); iForm.Close(); refreshAll_Click(sender, e); } }
/* * 添加和编辑窗口共用: * - flag * - true:添加 * - false:编辑 * - host * - 要编辑的对象,添加时不需要 */ private void editForm(object sender, EventArgs e, bool flag, vmFile.vmHost host) { /* * 控件列表 * - Form:主窗体 * - Label:顶部警告框,默认为空 * - Label:id * - Lable+TextBox:name标签和输入框 * - Lable+TextBox:path标签和输入框 * - Button:底部确定按钮 * - Button:底部取消按钮 */ Form cForm = new Form(); Label warnL = new Label(); Label idL = new Label(); Label nameL = new Label(); TextBox nameB = new TextBox(); Label pathL = new Label(); TextBox pathB = new TextBox(); Button yesB = new Button(); Button noB = new Button(); //主窗体 if (flag) { cForm.Text = "添加虚拟机"; } else { cForm.Text = "编辑虚拟机[" + host.id + "]"; } cForm.StartPosition = FormStartPosition.CenterScreen; cForm.Width = 400; cForm.Height = 250; cForm.FormBorderStyle = FormBorderStyle.FixedSingle; cForm.MaximizeBox = false; //顶部警告框 warnL.Name = "warn"; warnL.Width = 380; warnL.ForeColor = System.Drawing.Color.Red; warnL.Location = new Point(10, 10); //id idL.Name = "id"; if (flag) { idL.Text = ""; } else { idL.Text = host.id; } idL.Location = new Point(50, 30); idL.Visible = false; /* * 名称和输入框 * _____________ * 名称 |____________| */ nameL.Text = "名称"; nameL.Width = 50; nameL.Location = new Point(50, 50); nameB.Name = "name"; nameB.Width = 250; nameB.Location = new Point(100, 50); if (!flag) { nameB.Text = host.name; //nameB.Enabled = false; } /* * 位置和输入框 * _____________ * 位置 |____________| */ pathL.Text = "位置"; pathL.Width = 50; pathL.Location = new Point(50, 100); pathB.Name = "path"; pathB.Width = 250; pathB.Location = new Point(100, 100); if (!flag) { pathB.Text = host.path; } //底部确定按钮 yesB.Text = "确定"; yesB.Width = 50; yesB.Location = new Point(125, 150); yesB.Click += editYesButton_Click; //底部取消按钮 noB.Text = "取消"; noB.Width = 50; noB.Location = new Point(225, 150); noB.Click += editNoButton_Click; //加载控件和窗口 cForm.Controls.Add(warnL); cForm.Controls.Add(idL); cForm.Controls.Add(nameL); cForm.Controls.Add(nameB); cForm.Controls.Add(pathL); cForm.Controls.Add(pathB); cForm.Controls.Add(yesB); cForm.Controls.Add(noB); cForm.ShowDialog(); }