M
metaperl
Hello, I'm trying to get the MSDN documentation example of a treeview
to work:
http://msdn2.microsoft.com/en-us/library/system.windows.forms.treeview.aspx
I made the function static and added a call to it in Main():
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1.InitializeTreeView();
Application.Run(new Form1());
}
but I'm getting the error "An object reference is required for the
nonstatic field, method, or property 'treenodeExample.Form1.treeView1'
Here is the Form1 class:
namespace treenodeExample
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be
disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region
// Windows Form Designer Code ommited
#endregion
private System.Windows.Forms.TreeView treeView1;
// Populates a TreeView control with example nodes.
public static void InitializeTreeView()
{
treeView1.BeginUpdate();
treeView1.Nodes.Add("Parent");
treeView1.Nodes[0].Nodes.Add("Child 1");
treeView1.Nodes[0].Nodes.Add("Child 2");
treeView1.Nodes[0].Nodes[1].Nodes.Add("Grandchild");
treeView1.Nodes[0].Nodes[1].Nodes[0].Nodes.Add("Great
Grandchild");
treeView1.EndUpdate();
}
}
}
to work:
http://msdn2.microsoft.com/en-us/library/system.windows.forms.treeview.aspx
I made the function static and added a call to it in Main():
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1.InitializeTreeView();
Application.Run(new Form1());
}
but I'm getting the error "An object reference is required for the
nonstatic field, method, or property 'treenodeExample.Form1.treeView1'
Here is the Form1 class:
namespace treenodeExample
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be
disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region
// Windows Form Designer Code ommited
#endregion
private System.Windows.Forms.TreeView treeView1;
// Populates a TreeView control with example nodes.
public static void InitializeTreeView()
{
treeView1.BeginUpdate();
treeView1.Nodes.Add("Parent");
treeView1.Nodes[0].Nodes.Add("Child 1");
treeView1.Nodes[0].Nodes.Add("Child 2");
treeView1.Nodes[0].Nodes[1].Nodes.Add("Grandchild");
treeView1.Nodes[0].Nodes[1].Nodes[0].Nodes.Add("Great
Grandchild");
treeView1.EndUpdate();
}
}
}