紧急请C#高手看看,一个错误.不知道怎么修改下!!!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsApplication9
{
public partial class Form1 : Form
{
//获取当前选定的节点值
public int CurrentVal = 0;
//获取控件树的深度
public int pubDeep = 0;
// 获取子树节点的个数
public int pubChildNum = 0;
//获取当前节点对象
public TreeNode CurrentNode = null;
public Form1()
{
InitializeComponent();
}
//private void toolStripLabel1_Click(object sender, EventArgs e)
//{
// /////
//}
//private void toolTip1_Popup(object sender, PopupEventArgs e)
//{
// /////
//}
private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
TreeNode tn = treeView1.GetNodeAt(e.X, e.Y);
if (tn != null)
{
this.treeView1.SelectedNode = tn;
}
}
if (e.Node != null)
{
//MessageBox.Show( "Node Selected ");
}
}
private void treeView1_AfterLabelEdit_1(object sender, NodeLabelEditEventArgs e)
{
if (e.Label != null)
{
TreeNode node = treeView1.SelectedNode;
if (node.GetType() == typeof(TreeNode))
{
//这里 e.Lable为要修改的节点的值
}
}
treeView1.LabelEdit = false;
}
private void treeView1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right )
{
Point ClickPoint = new Point(e.X, e.Y);
TreeNode CurrentNode = treeView1.GetNodeAt(ClickPoint);
if (CurrentNode == null)
return;
Point ScreenPoint = treeView1.PointToScreen(ClickPoint);
Point FormPoint = this.PointToClient(ScreenPoint);
ContextMenu menu = new ContextMenu();
menu.MenuItems.Clear();
menu.MenuItems.Add( "增加表字段 ");
menu.MenuItems[0].Click += new EventHandler(Option1_Click);
menu.MenuItems.Add( "删除表字段 ");
menu.MenuItems[1].Click += new EventHandler(Option2_Click);
menu.Show(this, FormPoint);
TreeNode node = treeView1.SelectedNode;
if (node.GetType() == typeof(TreeNode))
{
treeView1.LabelEdit=false ;
node.EndEdit(true) ;
}
}
}
private void parseNode(TreeNode tn)
{
StreamWriter sr = new StreamWriter( "f:\\0100.txt ", false, System.Text.Encoding.UTF8);
IEnumerator ie = tn.Nodes.GetEnumerator();
while (ie.MoveNext())
{
TreeNode ctn = (TreeNode) ie.Current;
//为最底层节点,即该节点为RSS节点
if (ctn.GetNodeCount(true) == 0)
{
//xmlLine = GetRSSText(ctn);
sr.WriteLine( "xmlLine ");
}
else//不是最底层节点,即该节点为目录节点
{
//xmlLine = GetDirectoryText(ctn);
sr.WriteLine( "xmlLine ");
}
//如果还有子节点则继续遍历
if (ctn.GetNodeCount(true) > 0)
{
parseNode(ctn);
}
}
sr.WriteLine( " </outline> ");
}
private void Option1_Click(object sender, EventArgs e) //提示文字,暂时。。。。
{
MessageBox.Show( "增加 ");
if (!(this.treeView1.Nodes.Count == 0))
{
TreeNode newnodes = new TreeNode( "add newnode ");
treeView1.SelectedNode.Nodes.Add(newnodes);
}
else
{
this.treeView1.Nodes.Add( "childnode "); //
TreeNode node = treeView1.SelectedNode; //是根结点时的处理代码
//node.Clone(); //复制该树节点;
}
}
private void Option2_Click(object sender, EventArgs e)
{
MessageBox.Show( "删除 "); // Remove 方法进行移除单个节点
this.treeView1.Nodes.Remove(this.treeView1 .SelectedNode );
}
private void treeView1_KeyDown(object sender, KeyEventArgs e)//鼠标按下的时候键盘触发
{
if (e.KeyCode == Keys.F2 ) //设置F2为修改的键;
{
TreeNode node = treeView1.SelectedNode;
if (node.GetType() == typeof(TreeNode))
{
treeView1.LabelEdit = true;
node.BeginEdit ();
treeView1.BeginUpdate();
}
}
}
private void treeView1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)//鼠标经过的时候
{
if (e.Button == MouseButtons.Right)
{
Point pointNode = new Point();
pointNode.X = e.X;
pointNode.Y = e.Y;
if (this.treeView1.GetNodeAt(pointNode) != null)
{
MessageBox.Show(this.treeView1.GetNodeAt(pointNode).Text);
}
}
}
//private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
//{
//CurrentVal = Int32.Parse(e.Node.Tag.ToString());
//CurrentNode = e.Node;
//pubChildNum = e.Node.GetNodeCount(true);
//TreeNode tr = new TreeNode();
//tr.Text = "textname ";
//tr.Tag = "0001 ";
//CurrentNode.Nodes.Add(tr);
//treeView1.BeginUpdate();
//}
protected void treeView1_AfterSelect (object sender, System.Windows.Forms.TreeViewEventArgs e)
{
// Determine by checking the Text property.
//MessageBox.Show(e.Node.Text); //显示选择的节点的信息,暂时;
}
private void tabPage2_Click(object sender, EventArgs e)
{
//System.IO.FileStream nnn = new System.IO.FileStream();
}
}
}
[解决办法]
private void parseNode(TreeNode tn)
中这句有问题:IEnumerator ie = tn.Nodes.GetEnumerator();
[解决办法]
添加一个引用
using System.Collections;
或者将这几句改为
private void parseNode(TreeNode tn)
{
StreamWriter sr = new StreamWriter( "f:\\0100.txt ", false, System.Text.Encoding.UTF8);
foreach(TreeNode ctn in tn.Nodes)
{
//为最底层节点,即该节点为RSS节点
if (ctn.GetNodeCount(true) == 0)
{
//xmlLine = GetRSSText(ctn);
sr.WriteLine( "xmlLine ");
}
else//不是最底层节点,即该节点为目录节点
{
//xmlLine = GetDirectoryText(ctn);
sr.WriteLine( "xmlLine ");
}
//如果还有子节点则继续遍历
if (ctn.GetNodeCount(true) > 0)
{
parseNode(ctn);
}
}
sr.WriteLine( " </outline> ");
}
[解决办法]
然后有个疑问就是
using System.Collections.Generic;
和
using System.Collections;
当然有区别,明显一个包含另一个呀
还有就是,比如Generic有个方法test()
要是引入using System.Collections.Generic;的话可以直接用
要是引入using System.Collections就成了 Generic.test()
这样说得很明白了吧
[解决办法]
using System.Collections.Generic.
和
using System.Collections;
是两个不名的名字空间
分别有using System.Collections.Generic.
IEnumerator和
using System.Collections.IEnumerator两个不同的类
在没加
using System.Collections;的时候是使用前都 但前者不是这么用的