c#列表和树形控件

求一个c#用到列表和树形控件的代码,最好有注释。。
2024-12-21 12:56:57
推荐回答(4个)
回答1:

列表可以用listview控件,如果有多行的话可以用循环,这里只用一行进行说明:
ListViewItem lv = new ListViewItem(); //先实例化
lv.Text =“”; 一行的第一个元素,双引号里面填写元素
lv.SubItems.Add(); 一行的第二个元素,括号号里面填写元素
lv.SubItems.Add(); 一行的第二个元素,括号号里面填写元素,以此类推
listViewname.Items.Add(lv); 将实例化添加到listview里面, listViewname为所用控件的名字

回答2:

//点击TreeView控件时的事件
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
string message = string.Format("选中了{0}节点,深度{1}", treeView1.SelectedNode.Text, treeView1.SelectedNode.Level);
MessageBox.Show(message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);

}

///


/// 方法
///

public void file()
{
DataSet dataset = new DataSet();
SqlDataAdapter da;
StringBuilder sb = new StringBuilder();

sb.Append(" SELECT dbo.Grade.GradeName, dbo.Student.StudentName");
sb.Append(" FROM dbo.Grade INNER JOIN");
sb.Append(" dbo.Student ON dbo.Grade.GradeId = dbo.Student.GradeId");
da = new SqlDataAdapter(sb.ToString(), Class2.connection);
da.Fill(dataset, "student");
dataGridView1.DataSource = dataset.Tables["student"];
//创建dataview对象
DataView dv = new DataView();
//筛选条件
string rewfile = string.Empty;
//...获得选中的节点
//年纪的节点
if (treeView1.SelectedNode.Level == 1)
{
string gradeName = treeView1.SelectedNode.Text;
rewfile = string.Format("gradeName={0}", gradeName);
}
//性别级别的节点
if (treeView1.SelectedNode.Level == 2)
{
//性别的枚举值
gender gend = (gender)Enum.Parse(typeof(gender), treeView1.SelectedNode.Tag.ToString());
//性别编号
int genderID = (int)gend;
//筛选条件
rewfile = string.Format("gradeName = '{0}' and gender ={1}", treeView1.SelectedNode.Parent.Text, genderID);
}
dv.RowFilter = rewfile;//设置筛选条件
//dv.Sort = "studentname desc";//指定排序条件
dataGridView1.DataSource = dv; //绑定数据源
}

回答3:

menu 控件

回答4:

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace UserControls
{
///


/// TreeViewComboBox 的摘要说明。
///

public class TreeViewComboBox : UserControl
{
///
/// 必需的设计器变量。
///

private Container components = null;

public delegate void EventHandle(TreeNode n);
public event EventHandle AfterSelectedNode;

private bool _allowSelectParentNode = false;

public TreeViewComboBox()
{
this.InitializeComponent();

// Initializing Controls
this.pnlBack = new Panel();
this.pnlBack.BorderStyle = BorderStyle.Fixed3D;
this.pnlBack.BackColor = Color.White;
this.pnlBack.AutoScroll = false;

this.tbSelectedValue = new TextBox();
this.tbSelectedValue.BorderStyle = BorderStyle.None;
this.tbSelectedValue.ReadOnlyChanged += new EventHandler(tbSelectedValue_ReadOnlyChanged);

this.btnSelect = new ButtonEx();
this.btnSelect.Click += new EventHandler(ToggleTreeView);
this.btnSelect.FlatStyle = FlatStyle.Flat;

this.lblSizingGrip = new LabelEx();
this.lblSizingGrip.Size = new Size(9,9);
this.lblSizingGrip.BackColor = Color.Transparent;
this.lblSizingGrip.Cursor = Cursors.SizeNWSE;
this.lblSizingGrip.MouseMove += new MouseEventHandler(SizingGripMouseMove);
this.lblSizingGrip.MouseDown += new MouseEventHandler(SizingGripMouseDown);

this.tvTreeView = new TreeView();
this.tvTreeView.BorderStyle = BorderStyle.None;
this.tvTreeView.DoubleClick += new EventHandler(TreeViewNodeSelect);
this.tvTreeView.Location = new Point(0,0);
this.tvTreeView.LostFocus += new EventHandler(TreeViewLostFocus);
//this.tvTreeView.Scrollable = false;

this.frmTreeView = new Form();
this.frmTreeView.FormBorderStyle = FormBorderStyle.None;
this.frmTreeView.BringToFront();
this.frmTreeView.StartPosition = FormStartPosition.Manual;
this.frmTreeView.ShowInTaskbar = false;
this.frmTreeView.BackColor = SystemColors.Control;

this.pnlTree = new Panel();
this.pnlTree.BorderStyle = BorderStyle.FixedSingle;
this.pnlTree.BackColor = Color.White;

SetStyle(ControlStyles.DoubleBuffer,true);
SetStyle(ControlStyles.ResizeRedraw,true);

// Adding Controls to UserControl
this.pnlTree.Controls.Add(this.lblSizingGrip);
this.pnlTree.Controls.Add(this.tvTreeView);
this.frmTreeView.Controls.Add(this.pnlTree);
this.pnlBack.Controls.AddRange(new Control[]);
this.Controls.Add(this.pnlBack);
}

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

protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

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

private void InitializeComponent()
{
//
// ComboBoxTree
//
this.Name = "ComboBoxTree";
// this._absoluteChildrenSelectableOnly = true;
this.Layout += new LayoutEventHandler(this.ComboBoxTree_Layout);

}
#endregion

#region Private 属性
private Panel pnlBack;
private Panel pnlTree;
private TextBox tbSelectedValue;
private ButtonEx btnSelect;
private TreeView tvTreeView;
private LabelEx lblSizingGrip;
private Form frmTreeView;

// private string _branchSeparator;
// private bool _absoluteChildrenSelectableOnly;
private Point DragOffset;
#endregion

#region Public 属性

///
/// 是否允许选择非叶子节点
///

public bool AllowSelectParentNode
{
set
{
_allowSelectParentNode = value;
}
get
{
return _allowSelectParentNode;
}
}

public Color TextForeColor
{
set
}

public Color TextBackColor
{
set
}

///
/// 文本只读属性
///

public bool TextReadOnly
{
set
}

///
/// 树节点集合
///

public TreeNodeCollection Nodes
{
get
{
return this.tvTreeView.Nodes;
}
}

///
/// 选中的节点
///

public TreeNode SelectedNode
{
set
{
this.tvTreeView.SelectedNode = value;
}
get
{
return this.tvTreeView.SelectedNode;
}
}

///
/// ImageList
///

public ImageList Imagelist
{
get
set
}

///
/// 显示选中的值
///

public override string Text
{
get
set
}

// ///
// /// 显示完整树节点路径时,路经的分隔符(1位字符)
// ///

// public string BranchSeparator
// {
// get
// set
// {
// if(value.Length > 0)
// this._branchSeparator = value.Substring(0,1);
// }
// }
//
// ///
// /// 是否是叶子节点
// ///

// public bool AbsoluteChildrenSelectableOnly
// {
// get
// set
// }
#endregion

///
/// 拖动树背景,改变背景大小
///

private void RelocateGrip()
{
this.lblSizingGrip.Top = this.frmTreeView.Height - lblSizingGrip.Height - 1;
this.lblSizingGrip.Left = this.frmTreeView.Width - lblSizingGrip.Width - 1;
}

///
/// 点击三角按钮,显示树Form
///

///
///
private void ToggleTreeView(object sender, EventArgs e)
{
if(!this.frmTreeView.Visible)
{
Rectangle CBRect = this.RectangleToScreen(this.ClientRectangle);
this.frmTreeView.Location = new Point(CBRect.X, CBRect.Y + this.pnlBack.Height);
this.tvTreeView.Nodes[0].Expand();
this.frmTreeView.Show();
this.frmTreeView.BringToFront();

this.RelocateGrip();
//this.tbSelectedValue.Text = "";
}
else
{
this.frmTreeView.Hide();
}
}

///
/// 验证选中的是否是叶子节点
///

///
// public bool ValidateText()
// {
// string ValidatorText = this.Text;
// TreeNodeCollection TNC = this.tvTreeView.Nodes;
//
// for(int i = 0; i < ValidatorText.Split(this._branchSeparator.ToCharArray()[0]).Length; i++)
// {
// bool NodeFound = false;
// string NodeToFind = ValidatorText.Split(this._branchSeparator.ToCharArray()[0])[i];
// for(int j = 0; j < TNC.Count; j++)
// {
// if(TNC[j].Text == NodeToFind)
// {
// NodeFound = true;
// TNC = TNC[j].Nodes;
// break;
// }
// }
//
// if(!NodeFound)
// return false;
// }
//
// return true;
// }

#region 事件
///
/// 改变背景大小,在鼠标移动时发生
///

///
///
private void SizingGripMouseMove(object sender, MouseEventArgs e)
{
if(e.Button == MouseButtons.Left)
{
int TvWidth, TvHeight;
TvWidth = Cursor.Position.X - this.frmTreeView.Location.X;
TvWidth = TvWidth + this.DragOffset.X;
TvHeight = Cursor.Position.Y - this.frmTreeView.Location.Y;
TvHeight = TvHeight + this.DragOffset.Y;

if(TvWidth < 50)
TvWidth = 50;
if(TvHeight < 50)
TvHeight = 50;

this.frmTreeView.Size = new Size(TvWidth, TvHeight);
this.pnlTree.Size = this.frmTreeView.Size;
this.tvTreeView.Size = new Size(this.frmTreeView.Size.Width - this.lblSizingGrip.Width, this.frmTreeView.Size.Height - this.lblSizingGrip.Width);;
RelocateGrip();
}
}

///
/// 改变背景大小,在按下鼠标时发生
///

///
///
private void SizingGripMouseDown(object sender, MouseEventArgs e)
{
if(e.Button == MouseButtons.Left)
{
int OffsetX = Math.Abs(Cursor.Position.X - this.frmTreeView.RectangleToScreen(this.frmTreeView.ClientRectangle).Right);
int OffsetY = Math.Abs(Cursor.Position.Y - this.frmTreeView.RectangleToScreen(this.frmTreeView.ClientRectangle).Bottom);

this.DragOffset = new Point(OffsetX, OffsetY);
}
}

///
/// 树型控件失去焦点
///

///
///
private void TreeViewLostFocus(object sender, EventArgs e)
{
if(!this.btnSelect.RectangleToScreen(this.btnSelect.ClientRectangle).Contains(Cursor.Position))
this.frmTreeView.Hide();
}

///
/// 选中树型空间节点
///

///
///
private void TreeViewNodeSelect(object sender, EventArgs e)
{
if(this._allowSelectParentNode && this.tvTreeView.SelectedNode != this.tvTreeView.Nodes[0])
{
this.tbSelectedValue.Text = this.tvTreeView.SelectedNode.Text;

this.ToggleTreeView(sender,null);

AfterSelectedNode(this.tvTreeView.SelectedNode);
}
else
{
if(this.tvTreeView.SelectedNode.Nodes.Count == 0)
{
this.tbSelectedValue.Text = this.tvTreeView.SelectedNode.Text;

this.ToggleTreeView(sender,null);

AfterSelectedNode(this.tvTreeView.SelectedNode);
}
}
}

private void ComboBoxTree_Layout(object sender, LayoutEventArgs e)
{
this.Height = this.tbSelectedValue.Height + 8;
this.pnlBack.Size = new Size(this.Width, this.Height - 2);

this.btnSelect.Size = new Size(16, this.Height - 6);
this.btnSelect.Location = new Point(this.Width - this.btnSelect.Width - 4, 0);

this.tbSelectedValue.Location = new Point(2, 2);
this.tbSelectedValue.Width = this.Width - this.btnSelect.Width - 4;
this.frmTreeView.Size = new Size(this.Width + 60, 250);
this.pnlTree.Size = this.frmTreeView.Size;
this.tvTreeView.Width = this.frmTreeView.Width - this.lblSizingGrip.Width;
this.tvTreeView.Height = this.frmTreeView.Height - this.lblSizingGrip.Width;
this.RelocateGrip();
}

///
/// 文本只读事件
///

///
///
private void tbSelectedValue_ReadOnlyChanged(object sender, EventArgs e)
{
if(this.tbSelectedValue.ReadOnly)
{
this.tbSelectedValue.BackColor = Color.White;
}
}
#endregion

#region 树型背景Label
private class LabelEx : Label
{
///
///
///

public LabelEx()
{
this.SetStyle(ControlStyles.UserPaint,true);
this.SetStyle(ControlStyles.DoubleBuffer,true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint,true);
}

///
///
///

///
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
ControlPaint.DrawSizeGrip(e.Graphics,Color.Black, 1, 0, this.Size.Width, this.Size.Height);
}
}
#endregion

#region 三角形按钮
private class ButtonEx : Button
{
ButtonState state;

///
///
///

public ButtonEx()
{
this.SetStyle(ControlStyles.UserPaint,true);
this.SetStyle(ControlStyles.DoubleBuffer,true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint,true);

}
///
///
///

///
protected override void OnMouseDown(MouseEventArgs e)
{
state = ButtonState.Pushed;
base.OnMouseDown(e);
}

///
///
///

///
protected override void OnMouseUp(MouseEventArgs e)
{
state = ButtonState.Normal;
base.OnMouseUp(e);
}

///
///
///

///
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
ControlPaint.DrawComboButton(e.Graphics, 0, 0, this.Width, this.Height, state);
}
}
#endregion

}

}
另外,站长团上有产品团购,便宜有保证