我有Treeview与 节点 -Node1 -Node1Child -Node2 -Node2Child 和带数据的ComboBox Combo1 Combo2 如果Node1Child单击然后在ComboBox中显示Node1Child如何使其工作并且可以更改为Combo2值? 我曾尝试过: i搜索无处不在,无法找到C#
i have Treeview with Node -Node1 -Node1Child -Node2 -Node2Child and ComboBox with data Combo1 Combo2 how to make it work if Node1Child Clicked then in ComboBox Show Node1Child and can be changed woth Combo2 value? What I have tried: i search everywhere and cannot find solution for C#
推荐答案的解决方案 AfterSelect()事件仅触发新选择,如果已选中则不会触发。您可能希望捕获TreeView的Click事件。 The AfterSelect() event only fires for new selections and won't fire if already selected. You may want to capture the TreeView's Click event instead. private void treeView1_Click(object sender, EventArgs e) { TreeViewHitTestInfo info = treeView1.HitTest(treeView1.PointToClient(Cursor.Position)); if (info != null) MessageBox.Show(info.Node.Text); }
编辑: 以下是我认为你试图做的事情:
Here is what I think that you are attempting to do:
private void treeView1_Click(object sender, EventArgs e) { TreeViewHitTestInfo info = treeView1.HitTest(treeView1.PointToClient(Cursor.Position)); if (info != null) { comboBox1.DisplayMember = "Text"; comboBox1.DataSource = info.Node.Nodes; } } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { var node = comboBox1.SelectedItem as TreeNode; if (node == null) return; treeView1.SelectedNode = node; treeView1.Focus(); }