急需一段用C#编写的标准软件开发代码:是关于TreeView控件和TabControl的应用。 (不知道这些条件够不够)

2025-01-04 16:02:22
推荐回答(4个)
回答1:

我有一个TreeView控件的例子,但是不详细~~~~~
Treeview正在加载……效果演示

Form1.cs
using System;  
using System.Collections.Generic;  
using System.ComponentModel;  
using System.Data;  
using System.Drawing;  
using System.Text;  
using System.Windows.Forms;

namespace TreeviewDemo
{
public partial class Form1 : Form
{
/*
* 增加一个子节点就提示一次.
* 然后开线程请求真实的子节点,完毕后替换提示节点。 
*/
public class ThreadStartInfo
{
public TreeNode node = null;
//载入数据
public void LoadNode()
{
System.Threading.Thread.Sleep(5000);
string[] str = new string[]{"hgd","huih","kjhyiu","kuy","khio","jhgyut" };
this.AddNode(node,str);
}
delegate void newAddNode(TreeNode node,string[]str);  

void AddNode(TreeNode node, string[] str)
{
if (this.node.TreeView.FindForm().InvokeRequired)
{
this.node.TreeView.FindForm().Invoke(new newAddNode(AddNode),new object[]{node,str});   
}
else
{
node.Nodes.Clear();
foreach (string item in str)
{
node.Nodes.Add(item);
}
}
}
}
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
this.treeView1.Nodes.Add(new TreeNode("Root"));
this.treeView1.AfterSelect += new TreeViewEventHandler(treeView1_AfterSelect);

}

void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
if (e.Node.Tag==null || e.Node.Tag.ToString()=="")
{
ThreadStartInfo tsi = new ThreadStartInfo();
tsi.node = e.Node;
//加载过的就不要在加载了
tsi.node.Tag = 1;

new System.Threading.Thread(new System.Threading.ThreadStart(tsi.LoadNode)).Start();
tsi.node.Nodes.Add("正在载入……");

}
}
}
}

Form1.Designer.cs
namespace TreeviewDemo
{
partial class Form1
{
///


/// 必需的设计器变量。
///

private System.ComponentModel.IContainer components = null;

///
/// 清理所有正在使用的资源。
///

/// 如果应释放托管资源,为 true;否则为 false。
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows 窗体设计器生成的代码

///
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
///

private void InitializeComponent()
{
this.treeView1 = new System.Windows.Forms.TreeView();
this.SuspendLayout();
//
// treeView1
//
this.treeView1.Location = new System.Drawing.Point(12, 18);
this.treeView1.Name = "treeView1";
this.treeView1.Size = new System.Drawing.Size(243, 339);
this.treeView1.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 369);
this.Controls.Add(this.treeView1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.TreeView treeView1;
}
}

回答2:

给你个我在用的 是带子节点的数。

private void BindPriceTreeView()
{
IList list = priceTypeManager.GetAllPricesetTypeList();
TreeNode defaultnode = new TreeNode();
foreach (PricesetType item in list.Where(s => s.ParentId == null || s.ParentId == 0).ToList())
{
TreeNode node = new TreeNode();
node.Text = item.Name;
node.Value = item.TypeId.ToString();
node.ToolTip = string.Format("{0}", item.TypeId);
PriceCatalogTree.Nodes.Add(node);
this.GetPriceNode(node, list);
}
}

//添加子节点
protected void GetPriceNode(TreeNode node, IList list)
{
foreach (PricesetType item in list.Where(s => s.ParentId == int.Parse(node.Value)).ToList())
{
TreeNode leafNode = new TreeNode();
leafNode.Text = item.Name;
leafNode.Value = item.TypeId.ToString();
leafNode.ToolTip = string.Format("{0}", item.TypeId);
leafNode.Expanded = false;
node.ChildNodes.Add(leafNode);
//递归调用,添加子节点
this.GetPriceNode(leafNode, list);
}
}

回答3:










 




code
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindTreeView();
}
}

//给树控件绑定数据
private void BindTreeView()
{
TreeNode rootnode = new TreeNode();
TreeView1.Nodes.Add(rootnode);
rootnode.Text = "所有类别";
CreateTree(0, rootnode);
TreeView1.ShowLines = false;
rootnode.SelectAction = TreeNodeSelectAction.Expand;
//tn.Exand();
}

private void CreateTree(int belong, TreeNode rootnode)
{
string sql = "select ri_id,ri_name,ri_parentrightid from RightInfo where ri_parentrightid=" + belong;
DataSet ds = new DataSet();
ds = DataAccess.GetDataSet(sql);

foreach (DataRow dr in ds.Tables[0].Rows)
{
//实例化一个TreeNode对象
TreeNode treenode = new TreeNode();
treenode.Text = dr["ri_name"].ToString().Trim();
treenode.Value = dr["ri_id"].ToString().Trim();
//treenode.ImageUrl = "~/images/mebu-bgs1.gif";
treenode.Expanded = false;
treenode.SelectAction = TreeNodeSelectAction.Expand;
//给当前的节点添加子节点
rootnode.ChildNodes.Add(treenode);

//获取当前节点的ID
int id = Convert.ToInt32(dr["ri_id"]);
//递归调用CreateTree
CreateTree(id, treenode);
}
}

回答4:

只需要生成图上的效果吗