我使用了搜索功能,但无法找到答案。我正在从事学校/实习项目。
我有拖放功能。 String
从ListBox
转到TreeView
。 TreeView
的结构未知。我只想知道孩子们。
我想这样做:
1)将所有电机和传感器放入ListBox
(来自电机和传感器列表)>这没问题
2)拖动f.e. TreeView
的传感器。传感器从ListBox
消失。 >这也没问题。
3)更新ListBox
。在拖放期间,可能会创建新的传感器或电机。我希望ListBox
中的所有传感器和演员都能使TreeView
中的所有传感器和演员都感知到。{1}}。这是一个问题。我无法找到TreeView
中的孩子。
我可以找到treeview.Nodes.Add("P5")
手动添加的孩子。但我找不到TreeView
中拖放的孩子。所以,我发现P5总线在拖放后无法找到FSA。
有谁知道拖放节点和手动节点之间的区别? 我怎么能找到拖拉的孩子。
编辑:我想我只是在treeview
的第一层搜索。而不是在那之后的层。还没有解决方案。
谢谢!
private void treeViewDrop_DragDrop(object sender, DragEventArgs e)
{
TreeNode nodeToDropIn = treeViewDrop.GetNodeAt(treeViewDrop.PointToClient(new Point(e.X, e.Y)));
if (nodeToDropIn == null) { return; }
object data = e.Data.GetData(typeof(String));
if (data == null) { return; }
nodeToDropIn.Nodes.Add(data.ToString());
// Data (string) to HLF of motor , textbox
foreach (Sensor mySensor in sensorlist.Where(sensor => sensor.TAG == data.ToString()))
{
string treeviewpath = nodeToDropIn.FullPath.ToString();
string treeviewpath1 = treeviewpath.Replace(@"\", ".");
mySensor.Higherlevelfunction = treeviewpath1 + "." + data.ToString();
}
// Data (string) to HLF of motor, textbox
foreach (Motor mymotor in motorlist.Where(motor => motor.TAG == data.ToString()))
{
string treeviewpath = nodeToDropIn.FullPath.ToString();
string treeviewpath1 = treeviewpath.Replace(@"\", ".");
mymotor.Higherlevelfunction = treeviewpath1 + "." + data.ToString();
}
// Refresh equipment
LoadPropsFlowSensor();
LoadPropsPressureSensor();
LoadPropsLevelSensor();
loadPropsMotor();
listBoxDrag.Items.Remove(data);
treeViewDrop.ExpandAll();
}
private void listBoxDrag_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void treeViewDrop_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void listBoxDrag_MouseDown(object sender, MouseEventArgs e)
{
listBoxDrag.DoDragDrop(this.listBoxDrag.SelectedItem, DragDropEffects.Move);
}
private void treeViewDrop_DoubleClick(object sender, EventArgs e)
{
{
// Pop up screen. Write here node name of group
string nodename = "";
nodename = Interaction.InputBox("Supply new node name", "New node", "", -1, -1);
if (nodename == "") { return; }
if (treeViewDrop.SelectedNode != null)
{
treeViewDrop.SelectedNode.Nodes.Add(nodename);
treeViewDrop.ExpandAll();
}
}
}
private void button3_Click(object sender, EventArgs e)
{
// Remove selected node
if (treeViewDrop.SelectedNode != null)
{
treeViewDrop.SelectedNode.Remove();
}
}
private void button4_Click(object sender, EventArgs e)
{
// Refresh listbox with all sensors and actors who are not in the treeview.
Refreshlistbox();
}
List<String> stringlist1 = new List<String>();
List<String> stringlist2 = new List<String>();
private void Refreshlistbox()
{
listBoxDrag.Items.Clear();
stringlist1.Clear();
foreach (Sensor mysensor in sensorlist)
{
stringlist1.Add(mysensor.TAG);
}
foreach (Motor mymotor in motorlist)
{
stringlist1.Add(mymotor.TAG);
}
stringlist2.Clear();
foreach (TreeNode node in treeViewDrop.Nodes)
{
if (node.Text.Equals("FSA"))
{
MessageBox.Show("Yes, found FSA");
}
}
treeViewDrop.Nodes.Add("P5");
foreach (TreeNode node in treeViewDrop.Nodes)
{
if (node.Text.Equals("P5"))
{
MessageBox.Show("Yes, found P5");
}
}
MessageBox.Show("Done");
var firstNotSecond = stringlist1.Except(stringlist2).ToList();
foreach (String mystring in firstNotSecond)
{
listBoxDrag.Items.Add(mystring);
}
}
答案 0 :(得分:0)
找到解决方案
private void PrintRecursive(TreeNode treeNode)
{
// Print the node.
System.Diagnostics.Debug.WriteLine(treeNode.Text);
stringlist2.Add(treeNode.Text);
// Print each node recursively.
foreach (TreeNode tn in treeNode.Nodes)
{
PrintRecursive(tn);
}
}
// Call the procedure using the TreeView.
private void CallRecursive(TreeView treeView)
{
// Print each node recursively.
TreeNodeCollection nodes = treeView.Nodes;
foreach (TreeNode n in nodes)
{
PrintRecursive(n);
}
}