//“编辑窗口”的“确定”按钮事件 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); } }