Try two methods:
treeView1.Nodes["o1"].Nodes["o152"].Text = Utilities.Strings.General;
or
treeView1.Nodes.Find("o152", true)[0].Text = Utilities.Strings.General;
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hi folks,
I am having difficulty trying to access child nodes when my form loads.
My treeview I have 17 root nodes. Each has root node has several children. Each node has a unique name which is set as design time. o1, o2, o3 ..... o250.
In my form_Load event I am calling a routine that specifies what text to give to each node (root and children).
private void Main_Load(object sender, EventArgs e)
{
SetUpText();
}
public void SetUpText()
{
// Root Nodes
treeView1.Nodes["o1"].Text = Utilities.Strings.DataMaintenance;
treeView1.Nodes["o32"].Text = Utilities.Strings.Invoicing;
treeView1.Nodes["o237"].Text = Utilities.Strings.PurchaseLedger;
treeView1.Nodes["o4"].Text = Utilities.Strings.ProductCostings;
treeView1.Nodes["o210"].Text = Utilities.Strings.Boats;
treeView1.Nodes["o42"].Text = Utilities.Strings.Finance;
treeView1.Nodes["o7"].Text = Utilities.Strings.SalesAnalysis;
treeView1.Nodes["o2"].Text = Utilities.Strings.Quotations;
treeView1.Nodes["o5"].Text = Utilities.Strings.SalesLedger;
treeView1.Nodes["o3"].Text = Utilities.Strings.Orders;
treeView1.Nodes["o157"].Text = Utilities.Strings.FishBook;
treeView1.Nodes["o10"].Text = Utilities.Strings.Utilities;
treeView1.Nodes["o150"].Text = Utilities.Strings.NominalLedger;
treeView1.Nodes["o8"].Text = Utilities.Strings.Intrastats;
treeView1.Nodes["o9"].Text = Utilities.Strings.CreditControl;
treeView1.Nodes["o220"].Text = Utilities.Strings.CustomerBoard;
treeView1.Nodes["o42"].Text = Utilities.Strings.Finance;
// Children of 1st Node
treeView1.Nodes["o152"].Text = Utilities.Strings.General; // Error here
treeView1.Nodes["o153"].Text = Utilities.Strings.NominalCodes;
treeView1.Nodes["o154"].Text = Utilities.Strings.Warehouses;
treeView1.Nodes["o155"].Text = Utilities.Strings.Banks;
The root nodes all update with the correct text. My problem is that I cant get access to the child nodes. I get error message "Object reference not set to an instance of an object".
Any ideas what might be causing this ?
John
Try two methods:
treeView1.Nodes["o1"].Nodes["o152"].Text = Utilities.Strings.General;
or
treeView1.Nodes.Find("o152", true)[0].Text = Utilities.Strings.General;
Hi,
The problem is that you are trying to directly access nodes from the wrong collections.
For example "o152" is not in the treeView1.Nodes, but in the o1.Nodes collection.
Anyway, simpler, more flexible and safer solution can be created with a simple recursive method and the dictionary.
Example:
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
SetUpText();
}
private void SetUpText() {
var nodeNameDictionary = BuildNameDictionary();
SetUpNodesText(treeView1.Nodes, nodeNameDictionary);
}
// You can build dictionary here or in the utilities.
private Dictionary<string, string> BuildNameDictionary() {
return new Dictionary<string, string> {
// { "o1", Utilities.Strings.DataMaintenance },
// { "o32", Utilities.Strings.Invoicing; },
// Test values:
{ "o1", "Data Maintenance" },
{ "o152", "General" },
{ "o153", "Nominal Codes" }
};
}
private void SetUpNodesText(TreeNodeCollection nodes, Dictionary<string, string> nodeNameDictionary) {
foreach (TreeNode node in nodes) {
var nodeName = node.Name;
if (nodeNameDictionary.ContainsKey(nodeName)) {
node.Text = nodeNameDictionary[nodeName];
}
SetUpNodesText(node.Nodes, nodeNameDictionary);
}
}
}
Result:
BR,
Adnan