private void DataView_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (DataView.SelectedItem == null) { return; } TrainStationClass itemInfo = DataView.SelectedItem as TrainStationClass; if (itemInfo == null || !(itemInfo is TrainStationClass)) { MessageBox.Show("获取选中项出现问题"); return; } Text_arrivetime_Copy.Text = itemInfo.arrivetime; Text_leavetime_Copy.Text = itemInfo.leavetime; Text_remainseat_Copy.Text = itemInfo.remainseat.ToString(); }
private void Button_modify_Click(object sender, RoutedEventArgs e) { if (DataView.SelectedItem == null) { return; } TrainStationClass itemInfo = DataView.SelectedItem as TrainStationClass; if (itemInfo == null || !(itemInfo is TrainStationClass)) { MessageBox.Show("获取选中项出现问题"); return; } TcpClient tcpClient = null; NetworkStream networkStream = null; try { tcpClient = new TcpClient(); tcpClient.Connect(ip_address, port); //建立与服务器的连接 networkStream = tcpClient.GetStream(); if (networkStream.CanWrite) { int remainseat = 0; if (!getStandard(out remainseat, Text_remainseat_Copy.Text)) { MessageBox.Show("查询信息填写错误"); return; } var package = new TTS_Core.TrainStationOperationPackage(user, ip_address + ":" + listen_port.ToString(), "server", TTS_Core.Enum_OP.K_MODIFY, itemInfo.trainid, itemInfo.stationid, Text_arrivetime_Copy.Text, Text_leavetime_Copy.Text, remainseat); byte[] sendBytes = package.DataPackageToBytes(); //注册数据包转化为字节数组 networkStream.Write(sendBytes, 0, sendBytes.Length); var newClient = tcp_listener.AcceptTcpClient(); var bytes = ReadFromTcpClient(newClient); //获取数据 var package_rec = new TTS_Core.DataSetPackage(bytes); if (package_rec.forbid != 0 && package_rec.forbid != 1) { MessageBox.Show("出大问题"); } if (package_rec.forbid == 1) { MessageBox.Show("修改失败,请检查完整性约束或者是服务器故障"); } else { itemInfo.arrivetime = Text_arrivetime_Copy.Text; itemInfo.leavetime = Text_leavetime_Copy.Text; itemInfo.remainseat = remainseat; DataView.Items.Insert(DataView.SelectedIndex, itemInfo); DataView.Items.Remove(DataView.SelectedItem); DataView.Items.Refresh(); } } } catch { MessageBox.Show("无法连接到服务器!"); return; } finally { if (networkStream != null) { networkStream.Close(); } tcpClient.Close(); } }
private void Button_delete_Click(object sender, RoutedEventArgs e) { if (DataView.SelectedItem == null) { return; } int selected_trainid; int selected_stationid; TrainStationClass itemInfo = DataView.SelectedItem as TrainStationClass; if (itemInfo != null && itemInfo is TrainStationClass) { selected_trainid = itemInfo.trainid; selected_stationid = itemInfo.stationid; } else { MessageBox.Show("获取选中项出现问题"); return; } TcpClient tcpClient = null; NetworkStream networkStream = null; try { tcpClient = new TcpClient(); tcpClient.Connect(ip_address, port); //建立与服务器的连接 networkStream = tcpClient.GetStream(); if (networkStream.CanWrite) { var package = new TTS_Core.TrainStationOperationPackage(user, ip_address + ":" + listen_port.ToString(), "server", TTS_Core.Enum_OP.K_DELETE, selected_trainid, selected_stationid, "", "", 0); byte[] sendBytes = package.DataPackageToBytes(); networkStream.Write(sendBytes, 0, sendBytes.Length); var newClient = tcp_listener.AcceptTcpClient(); var bytes = ReadFromTcpClient(newClient); //获取数据 var package_rec = new TTS_Core.DataSetPackage(bytes); if (package_rec.forbid != 0 && package_rec.forbid != 1) { MessageBox.Show("出大问题"); } if (package_rec.forbid == 1) { MessageBox.Show("删除失败,请检查完整性约束或者是服务器故障"); } else { DataView.Items.Remove(DataView.SelectedItem); DataView.Items.Refresh(); } } } catch { MessageBox.Show("无法连接到服务器!"); return; } finally { if (networkStream != null) { networkStream.Close(); } tcpClient.Close(); } }