private void DelNode(int index1, int index2, int index3) { if (index1 == -1 || index2 == -1) { return; } int localIndex2 = index2; int localIndex3 = index3; if (index3 == -1) { socketCount--; } status.index1 = -1; status.index2 = -1; status.index3 = -1; IFPropertyNodeItem p = socketModels[index1]; if (((SocketModel)p).DelSocket(index2, index3)) { ((SocketModel)p).UpdateTreeView(p, false); // 更新选中情况 ReSelectItem(p, localIndex2, localIndex3); } else { RefreshTree(); UpdateView(); } }
// 自动选择、展开子节点 public virtual void UpdateSubTreeView(IFPropertyNodeItem item, bool selected = false) { item.Parent.Expanded = item.Expanded = true; if (selected && item.Children.Count == 1) { item.Children[0].Selected = true; } }
public void UpdateTreeView(IFPropertyNodeItem item, bool expend = false) { if (expend) { item.Expanded = true; item.Children.Last().Selected = true; } }
private void RefreshTree() { IFPropertyNodeItem item = treeViewServer.SelectedItem as IFPropertyNodeItem; treeViewServer.ItemsSource = null; treeViewServer.ItemsSource = socketModels; var lastItem = FocusManager.GetFocusedElement(this); treeViewServer.Focus(); lastItem.Focus(); }
private void GetIndex(RemoteSocketObject obj, out int index2, out int index3) { IFPropertyNodeItem p2 = obj.Parent; IFPropertyNodeItem p1 = p2.Parent; if (p2.Children != null) { index3 = p2.Children.IndexOf(obj); } else { index3 = -1; } index2 = p1.Children.IndexOf(p2); }
private int socketCount = 0; // 当前1级model的数量 //private List<SocketModel> socketModels = new List<SocketModel>(); private void Button_Create_Click(object sender, RoutedEventArgs e) { if (index1 == -1) { return; } bool success = false; IFPropertyNodeItem item = socketModels[index1]; if (item.Name == "TCPServer") { Window serverWin = new WinSocketCreate(); serverWin.Owner = this; serverWin.Left = this.Left + this.Width / 2 - serverWin.Width / 2; serverWin.Top = this.Top + this.Width / 5; if (serverWin.ShowDialog() == true) { // 创建TCP服务器,修改显示界面 if (((TCPServerSocket)item).CreateSocket(port) == 0) { // display success = true; } else { MessageBox.Show("端口" + port + "已被绑定!", "创建失败:"); } } } if (item.Name == "TCPClient") { Window win = new WinTCPClientCreate(); win.Owner = this; win.Left = this.Left + this.Width / 2 - win.Width / 2; win.Top = this.Top + this.Width / 5; if (win.ShowDialog() == true) { // 创建TCP客户端,修改显示界面 if (((TCPClientModel)item).CreateSocket(new IPEndPoint(ip, port)) == 0) { // display success = true; } else { MessageBox.Show("端口" + port + "已被绑定!", "创建失败:"); } } } if (item.Name == "UDPServer") { Window serverWin = new WinSocketCreate(); serverWin.Owner = this; serverWin.Left = this.Left + this.Width / 2 - serverWin.Width / 2; serverWin.Top = this.Top + this.Width / 5; ((WinSocketCreate)serverWin).portBox.Port = 50000; if (serverWin.ShowDialog() == true) { // 创建UDP服务器,修改显示界面 if (((UDPServerModel)item).CreateSocket(port) == 0) { // display success = true;; } else { MessageBox.Show("端口" + port + "已被绑定!", "创建失败:"); } } } if (item.Name == "UDPClient") { Window win = new WinUDPClientCrete(); win.Owner = this; win.Left = this.Left + this.Width / 2 - win.Width / 2; win.Top = this.Top + this.Width / 5; if (win.ShowDialog() == true) { // 创建UDP服务器,修改显示界面 if (((UDPClientModel)item).CreateSocket(new IPEndPoint(ip, port), port2) == 0) { // display success = true;; } else { MessageBox.Show("端口" + port2 + "已被绑定!", "创建失败:"); } } } if (item.Name == "UDPGroup") { Window groupWin = new WinUDPGroup(); groupWin.Owner = this; groupWin.Left = this.Left + this.Width / 2 - groupWin.Width / 2; groupWin.Top = this.Top + this.Width / 5; if (groupWin.ShowDialog() == true) { // 创建UDP组播 if (((UDPGroupSocket)item).CreateSocket(new IPEndPoint(ip, port), returnIndex1, returnIndex2) == 0) { // display success = true; //treeViewServer.ItemsSource = socketModels; } else { MessageBox.Show("端口" + port + "已被绑定!", "创建失败:"); } } } if (success) { ((SocketModel)item).UpdateTreeView(item, true); socketCount++; item.Expanded = true; UpdateView(); RefreshTree(); } }
private void GetIndex() { IFPropertyNodeItem item = treeViewServer.SelectedItem as IFPropertyNodeItem; if (item == null) { index1 = index2 = index3 = -1; return; } IFPropertyNodeItem p1 = null; IFPropertyNodeItem p2 = null; IFPropertyNodeItem p3 = null; while (true) { p1 = item; if (p1.Parent == null) { break; } p2 = p1; p1 = p2.Parent; if (p1.Parent == null) { break; } p3 = p2; p2 = p1; p1 = p2.Parent; break; } index1 = index2 = index3 = -1; int index = 0; foreach (var c in socketModels) { if (c == p1) { break; } index++; } index1 = index; if (p2 != null) { index = 0; foreach (var c in p1.Children) { if (c == p2) { break; } index++; } index2 = index; if (p3 != null) { index = 0; foreach (var c in p2.Children) { if (c == p3) { break; } index++; } index3 = index; } } }
private void ReSelectItem(IFPropertyNodeItem p1, int index2, int index3) { if (socketModels[this.index1] != p1) { RefreshTree(); return; } if (index3 == -1) { p1.Expanded = true; if (index2 > 0) { p1.Children.Last().Selected = true; } else if (index2 == 0 && p1.Children.Count != 0) { p1.Children.Last().Selected = true; } else { p1.Selected = true; } } else if (index3 == -2) //收包错误导致的删除 { if (p1.Children[index2].Children.Count != 0) { p1.Children[index2].Expanded = true; p1.Children[index2].Children[0].Selected = true; } else { p1.Children[index2].Selected = true; } } else if (index3 >= 0) { if (index2 == -1 || this.index2 != index2) { RefreshTree(); return; } IFPropertyNodeItem p2 = p1.Children[index2]; p2.Expanded = true; if (index3 > 0) { p2.Children.Last().Selected = true; } else if (index3 == 0 && p2.Children.Count != 0) { p2.Children.Last().Selected = true; } else { p2.Selected = true; } } RefreshTree(); }