簡體   English   中英

在C#中將子節點添加到樹視圖

[英]Adding child nodes to a treeview in C#

我有一個包含“汽車制造商”及其各自模型的樹狀視圖。 我有一個添加新的Make(父節點)的按鈕。

private void btnMake_Click(object sender, EventArgs e)
{
    string inputMake;
    inputMake = Microsoft.VisualBasic.Interaction.InputBox("Enter Make: ", "Add Car Manufacturer");
    carMake.Add(inputMake); // arrayList to store car Makes
    carTree.Nodes.Add(new TreeNode(inputMake));
}

我遇到的問題是添加了模型(子節點)。 我有一個添加模型的按鈕,但我不知道如何區分適當的父節點。

我目前有以下代碼:

private void btnModel_Click(object sender, EventArgs e)
{
    string inputModel;
    int index = carTree.Nodes.IndexOf(carTree.SelectedNode);
    //MessageBox.Show(carMake[index].ToString());
    //inputModel = Microsoft.VisualBasic.Interaction.InputBox("asfdasdf", "asdfasdf");
    //carTree.Nodes[index].Nodes.Add(new TreeNode(inputModel));
}

由於測試,最后幾行已被注釋掉。 我將護理Makes(父節點)放入ArrayList中,但是在訪問arraylist時遇到了問題。 此行返回錯誤:

 //MessageBox.Show(carMake[index].ToString());

最終,我希望以最有效的方式將子節點添加到各自的父節點中獲得幫助。

嘗試這個 :

if(carTree.SelectedNode == null)
  MessageBox.Show("Please select a node first");    

carTree.SelectedNode.Nodes.Add(new TreeNode("Child"));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM