//删除端口 private void button3_Click(object sender, EventArgs e) { if (dataGridView1.CurrentCell == null) { Show("请选择要删除的端口"); return; } string port = GetPort(dataGridView1.CurrentCell.RowIndex); if (Ask("确定删除端口" + port) != DialogResult.OK) { return; } XmPortBLL bll = new XmPortBLL(); if (bll.DeletePort(int.Parse(port))) { Initial(); Show("删除成功"); //关闭端口的tcp监听 } else { Show("删除端口" + port + "失败"); } }
//修改表格内容 private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { //获取当前列 int columnIndex = dataGridView1.CurrentCell.ColumnIndex; if (columnIndex != 2) { return; } //获取当前行 int rowIndex = dataGridView1.CurrentCell.RowIndex; string port = GetPort(rowIndex); string xmno = GetXmno(rowIndex); if (xmno == "") { Show("请输入项目编号"); return; } XmPortBLL bll = new XmPortBLL(); if (bll.GetXmno(int.Parse(port)) == int.Parse(xmno)) { return; } XmPortModel model = new XmPortModel() { port = int.Parse(port), xmno = int.Parse(xmno) }; if (bll.ChangeXmno(model)) { Show("修改成功"); } else { Show("修改失败"); } }
//检测端口的开启状态 void TestPort() { try { button1.Enabled = false; dataGridView1.Enabled = false; label1.Text = "正在获取端口开启状态"; XmPortBLL bll = new XmPortBLL(); TcpBLL tcpbll = new TcpBLL(); //获取端口绑定的ip地址 string ip = tcpbll.GetIp(); List <Task> taskList = new List <Task>(); TaskFactory factory = new TaskFactory(); for (int i = 0; i < dataGridView1.Rows.Count; i++) { int j = i; taskList.Add(factory.StartNew(() => { int port = int.Parse(GetPort(j)); //测试端口的连接 bool res = bll.TestPort(ip, port); //设置checkbox的选中状态 SetCheck(j, res); })); } ////用于等待其他几个检测端口的线程 taskList.Add(factory.ContinueWhenAll(taskList.ToArray(), arr => { // Show("刷新端口成功"); button1.Enabled = true; dataGridView1.Enabled = true; label1.Text = ""; })); } catch (Exception ex) { Show("获取端口状态失败"); FileOperation.WriteAppenFile("获取端口状态失败 " + ex.Message); } }
//添加端口 private void button1_Click(object sender, EventArgs e) { string port = textBox1.Text; string xmno = textBox2.Text; if (port == "" || xmno == "") { Show("请输入端口号和项目编号"); return; } if (isNumber(port) == false || isNumber(xmno) == false) { Show("请输入正确的端口号"); return; } XmPortBLL bll = new XmPortBLL(); if (bll.PortIsExist(int.Parse(port)) == true) { Show("该端口已存在"); return; } XmPortModel model = new XmPortModel() { port = int.Parse(port), xmno = int.Parse(xmno) }; if (bll.AddXmPort(model)) { Show("添加端口成功"); textBox1.Text = ""; textBox2.Text = ""; } else { Show("添加失败"); } }
//获取所有端口信息 public void GetPorts() { XmPortBLL bll = new XmPortBLL(); List <PortVM> models = new List <PortVM>(); foreach (var item in bll.GetPorts()) { PortVM model = new PortVM() { port = item.port.ToString(), xmno = item.xmno.ToString() }; models.Add(model); } dataGridView1.DataSource = models; dataGridView1.Columns[0].HeaderText = "端口状态"; dataGridView1.Columns[1].HeaderText = "端口号"; dataGridView1.Columns[2].HeaderText = "项目编号"; dataGridView1.Columns[1].ReadOnly = true; }