
所以,添加TreeView上的节点,必须把你的TreeNode添到TreeView中,而不是单纯的TreeNode.Add方法。你不执行TreeView.Add(TreeNode)方法,就永远不会把你的TreeNode添加到树中。
至于代码,1l已经说的很清楚了。
To: wonder_d至少在我现在的知识层面上,我认为
如果要想把节点加上,在下次打开的时候就出现,只能用静态的方法。
其实,可能我没有完全理解你的意思。但是
我认为无论你静态添加,还是动态添加,都是可以,
因为软件总有一个启动过程我指的是软件,不是在vs中的写的代码,如
果写在vs中,那么只能用静态了,因为vs的代码,要ctrl+f5才能运行,只有
运行了才能去Add()。
----------------------------------------------
无论你保存在文件中,还是保存的数据库中
动态添加时你总要打开文件或是数据库
所以在运行前肯定没有了,以上我指的都是在vs中的
代码,如果你做成了软件,那么无论你怎么添加都肯定会有
----------------------------------------------
其实有也是刚刚学习的vs,希望我们可以共同进步,我只是把我的
理解写了出来,共勉吧!
扩展的树视图控件创建新的 Windows 控件库项目 ︰Visual Studio.NET 或 Microsoft Visual Studio 2005年启动。
在文件菜单上单击新建,然后单击项目。
在项目类型列表中,单击Visual C# 项目。
注意:在 Visual Studio 2005 中,单击C#下的项目类型。
在模板列表中,单击Windows 控件库。
在名称文本框中,键入TreeViewEX,,然后单击确定。
在项目资源管理器中重命名默认的类模块从UserControl1.cs到TreeViewEX.cs。
在用户控件的属性窗口更改控件的名称从UserControl1到TreeViewEX。
从树视图控件继承。默认情况下,用户控件项目继承System.Windows.Forms.UserControl。更改类声明,它将继承System.Windows.Forms.TreeView,如下 ︰
注意︰ 不要修改组件设计器生成的代码部分。
public class TreeViewEX : System.Windows.Forms.TreeView
接下来,创建一个新的树节点类继承自System.Windows.Forms.TreeNode。你可以在相同的TreeViewEX.cs类模块中,或您可以创建一个单独的类模块。此类还实现IDictionaryEnumerator接口,并添加用于枚举一个集合中的节点的支持。在此示例中,现有的TreeViewEX.cs类模块用于此新类。要创建类别,请向TreeViewEX.cs添加下面的类定义 ︰
public class TreeNode : System.Windows.Forms.TreeNode, IDictionaryEnumerator
{
private DictionaryEntry nodeEntry
private IEnumerator enumerator
public TreeNode()
{
enumerator = base.Nodes.GetEnumerator()
}
public string NodeKey
{
get
{
return nodeEntry.Key.ToString()
}
set
{
nodeEntry.Key = value
}
}
public object NodeValue
{
get
{
return nodeEntry.Value
}
set
{
nodeEntry.Value = value
}
}
public DictionaryEntry Entry
{
get
{
return nodeEntry
}
}
public bool MoveNext()
{
bool Success
Success = enumerator.MoveNext()
return Success
}
public object Current
{
get
{
return enumerator.Current
}
}
public object Key
{
get
{
return nodeEntry.Key
}
}
public object Value
{
get
{
return nodeEntry.Value
}
}
public void Reset()
{
enumerator.Reset()
}
}
这将完成扩展的树视图控件。现在验证项目通过单击生成菜单中的生成解决方案编译没有错误。
返回页首
创建客户端应用程序
在文件菜单上单击新建,然后单击项目。
在项目类型列表中,单击Visual C# 项目。
注意:在 Visual Studio 2005 中,单击C#下的项目类型。
在模板列表中,单击Windows 应用程序。
将TreeViewEX控件的一个实例添加到 Windows 应用程序项目的默认窗体 ︰
在工具菜单上单击自定义工具箱。
单击.NET Framework 组件选项卡。
单击浏览,然后找到刚刚创建的 TreeViewEX.dll 文件。
单击确定。
TreeViewEX控件现在位于工具箱。向表单中添加该控件的实例。
向表单中添加两个命令按钮。
在窗体的Load事件中粘贴以下代码 ︰
this.treeViewEX1.Left = 10
this.treeViewEX1.Top = 10
this.treeViewEX1.Width = this.Width - 30
this.treeViewEX1.Height = (int)(this.Height * 0.6)
this.treeViewEX1.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right
this.button1.Top = treeViewEX1.Height + 20
this.button1.Left = 10
this.button1.Width = 200
this.button1.Height = 50
this.button1.Text = "Add Items to TreeViewEx"
this.button2.Top = button1.Top + button1.Height + 10
this.button2.Left = 10
this.button2.Width = 200
this.button2.Height = 50
this.button2.Text = "Locate Item in TreeView using the Key Value"
this.Height = 400
将以下代码粘贴 Button1 的Click事件中 ︰
TreeViewEX.TreeNode tn
string myData
myData = "Extra Information pertaining to Node"
tn = new TreeViewEX.TreeNode()
tn.Text = "This is node 1"
tn.NodeKey = "node1"
tn.NodeValue = myData + " 1"
treeViewEX1.Nodes.Add(tn)
tn = new TreeViewEX.TreeNode()
tn.Text = "This is node 2"
tn.NodeKey = "node2"
tn.NodeValue = myData + " 2"
treeViewEX1.Nodes.Add(tn)
tn = new TreeViewEX.TreeNode()
tn.Text = "This is node 3"
tn.NodeKey = "node3"
tn.NodeValue = myData + " 3"
treeViewEX1.Nodes.Add(tn)
tn = new TreeViewEX.TreeNode()
tn.Text = "This is node 4"
tn.NodeKey = "node4"
tn.NodeValue = myData + " 4"
treeViewEX1.Nodes.Add(tn)
tn = new TreeViewEX.TreeNode()
tn.Text = "This is node 5"
tn.NodeKey = "node5"
tn.NodeValue = myData + "5"
treeViewEX1.Nodes.Add(tn)
tn = new TreeViewEX.TreeNode()
tn.Text = "This is node 6"
tn.NodeKey = "node6"
tn.NodeValue = myData + " 6"
treeViewEX1.Nodes.Add(tn)
注意:应在 Visual Studio 2005年中更改代码。创建一个 Windows 窗体项目时,Visual C# 一个窗体向项目中添加默认情况下和名称 Form1 的窗体。表示窗体的两个文件称为 Form1.cs 和 Form1.designer.cs。在 Form1.cs 中编写代码。Designer.cs 文件是 Windows 窗体设计器将实现的所有 *** 作的代码,执行通过从工具箱中添加控件。有关 Windows 窗体设计器在 Visual C# 2005年中的详细信息,请访问下面的 Microsoft 网站 ︰
http://msdn2.microsoft.com/en-us/library/ms173077.aspx
将以下代码粘贴 Button2 的Click事件中 ︰
// Locate the third node using the NodeKey.
string myData
string nodeInfo
foreach (TreeViewEX.TreeNode tn in treeViewEX1.Nodes)
{
if (tn.NodeKey == "node6")
{
nodeInfo = "Name :" + tn.Text
myData = (string)tn.NodeValue
nodeInfo += "Data: " + myData
MessageBox.Show(nodeInfo, "Node Specific Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
break
}
}
请单击将项添加到 TreeViewEX。这将示例节点添加到树视图,定义为键和数据成员的值。
单击找到的项目。单击此按钮后,消息框将出现,并显示节点 6 基于使用foreach循环中的键属性的相关信息。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)