求含checkbox的treeview树型结构的简单例子(200分求),
解决方案 »
- sql两张表查询的问题
- ◆◇◆ 请大家推荐几个好的免费的计算机类电子书下载网站! ◆◇◆
- 请问: Microsoft OLE DB provider for ODBC Drivers 错误'80004005' [mirosoft]ODBC Microsoft Access Driver ] 行中有错误 /admin
- 急急急!键入aspnet_regiis -c出错
- 半夜求救!!!!关于DataList的问题
- 想做一个类似论坛短消息的功能,不知道如阿下手啊~!大家给点资助吧!!
- 谁能帮我解释这是什么意思,怎么解决
- 如何实现客户端数据下载然后存储成文件---急
- 水晶报表部署后不能使用网络打印机
- 在ASP.NET页面中如何修改数据库的连接?
- XML该怎么学?
- 在网页里怎么显示icon的返回类型?(调用文件的图标,怎么在网页里显示出来)
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;namespace ERM
{
/// <summary>
/// FrmPerID 的摘要说明。
/// </summary>
public class FrmPerID : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.StatusBar statusBar1;
private System.Windows.Forms.ToolBar toolBar1;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Panel panel4;
private System.Windows.Forms.Panel panel2;
private System.ComponentModel.IContainer components;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.TreeView tr2;
private System.Windows.Forms.Splitter splitter1;
private System.Windows.Forms.ToolBarButton toolBarButton1;
private System.Windows.Forms.ToolBarButton toolBarButton2;
private System.Windows.Forms.ToolBarButton toolBarButton3;
private System.Windows.Forms.ToolBarButton toolBarButton4;
private System.Windows.Forms.ToolBarButton toolBarButton5;
private System.Windows.Forms.ToolBarButton toolBarButton6;
private System.Windows.Forms.ToolBarButton toolBarButton7;
private System.Windows.Forms.ToolBarButton toolBarButton8;
private System.Windows.Forms.ToolBarButton toolBarButton9;
private System.Windows.Forms.ToolBarButton toolBarButton10;
private System.Data.DataSet myds;
private System.Data.OleDb.OleDbDataAdapter myda;
private System.Data.OleDb.OleDbCommandBuilder cmdb;
private System.Windows.Forms.BindingManagerBase mybind;
private System.Data.DataView dv;
private System.Windows.Forms.ImageList imageList1;
private string rybh;
public FrmPerID()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(FrmPerID));
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.statusBar1 = new System.Windows.Forms.StatusBar();
this.toolBar1 = new System.Windows.Forms.ToolBar();
this.toolBarButton1 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton2 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton3 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton4 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton5 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton6 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton7 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton8 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton9 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton10 = new System.Windows.Forms.ToolBarButton();
this.imageList1 = new System.Windows.Forms.ImageList(this.components);
this.panel1 = new System.Windows.Forms.Panel();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.panel4 = new System.Windows.Forms.Panel();
this.tr2 = new System.Windows.Forms.TreeView();
this.splitter1 = new System.Windows.Forms.Splitter();
this.listBox1 = new System.Windows.Forms.ListBox();
this.panel2 = new System.Windows.Forms.Panel();
this.panel1.SuspendLayout();
this.panel4.SuspendLayout();
this.panel2.SuspendLayout();
this.SuspendLayout();
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1,
this.menuItem2,
this.menuItem3});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.Text = "文件";
//
// menuItem2
//
this.menuItem2.Index = 1;
this.menuItem2.Text = "编辑";
//
// menuItem3
//
this.menuItem3.Index = 2;
this.menuItem3.Text = "浏览";
this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
//
// statusBar1
//
this.statusBar1.Location = new System.Drawing.Point(0, 311);
this.statusBar1.Name = "statusBar1";
this.statusBar1.Size = new System.Drawing.Size(528, 22);
this.statusBar1.TabIndex = 0;
this.statusBar1.Text = "statusBar1";
//
// toolBar1
//
this.toolBar1.Appearance = System.Windows.Forms.ToolBarAppearance.Flat;
this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
this.toolBarButton1,
this.toolBarButton2,
this.toolBarButton3,
this.toolBarButton4,
this.toolBarButton5,
this.toolBarButton6,
this.toolBarButton7,
this.toolBarButton8,
this.toolBarButton9,
this.toolBarButton10});
this.toolBar1.DropDownArrows = true;
this.toolBar1.ImageList = this.imageList1;
this.toolBar1.Location = new System.Drawing.Point(0, 0);
this.toolBar1.Name = "toolBar1";
this.toolBar1.ShowToolTips = true;
this.toolBar1.Size = new System.Drawing.Size(528, 41);
this.toolBar1.TabIndex = 1;
this.toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);
//
// toolBarButton1
//
this.toolBarButton1.Text = "保存";
//
// toolBarButton2
//
this.toolBarButton2.ImageIndex = 2;
this.toolBarButton2.Text = "取消";
//
// toolBarButton3
//
this.toolBarButton3.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
//
// toolBarButton4
//
this.toolBarButton4.ImageIndex = 0;
this.toolBarButton4.Text = "增加";
this.toolBarButton4.Visible = false;
//
// toolBarButton5
//
this.toolBarButton5.ImageIndex = 8;
this.toolBarButton5.Text = "注销";
this.toolBarButton5.Visible = false;
//
// toolBarButton6
//
this.toolBarButton6.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
this.toolBarButton6.Visible = false;
//
// toolBarButton7
//
this.toolBarButton7.ImageIndex = 10;
this.toolBarButton7.Text = "展开";
//
// toolBarButton8
//
this.toolBarButton8.ImageIndex = 9;
this.toolBarButton8.Text = "合并";
//
// toolBarButton9
//
this.toolBarButton9.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
//
// toolBarButton10
//
this.toolBarButton10.ImageIndex = 7;
this.toolBarButton10.Text = "退出";
//
// imageList1
//
this.imageList1.ColorDepth = System.Windows.Forms.ColorDepth.Depth16Bit;
this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
//
// panel1
//
this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.panel1.Controls.Add(this.label2);
this.panel1.Controls.Add(this.label1);
this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
this.panel1.Location = new System.Drawing.Point(0, 41);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(528, 38);
this.panel1.TabIndex = 2;
//
// label2
//
this.label2.Font = new System.Drawing.Font("华文行楷", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label2.Location = new System.Drawing.Point(168, 0);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(160, 32);
this.label2.TabIndex = 1;
this.label2.Text = "系统权限列表";
this.label2.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
//
// label1
//
this.label1.Dock = System.Windows.Forms.DockStyle.Left;
this.label1.Font = new System.Drawing.Font("华文行楷", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label1.Location = new System.Drawing.Point(0, 0);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(144, 36);
this.label1.TabIndex = 0;
this.label1.Text = "操作人员";
this.label1.TextAlign = System.Drawing.ContentAlignment.BottomLeft;
//
// panel4
//
this.panel4.Controls.Add(this.tr2);
this.panel4.Controls.Add(this.splitter1);
this.panel4.Controls.Add(this.listBox1);
this.panel4.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel4.Location = new System.Drawing.Point(0, 0);
this.panel4.Name = "panel4";
this.panel4.Size = new System.Drawing.Size(528, 232);
this.panel4.TabIndex = 1;
//
// tr2
//
this.tr2.CheckBoxes = true;
this.tr2.Dock = System.Windows.Forms.DockStyle.Fill;
this.tr2.ImageIndex = -1;
this.tr2.Location = new System.Drawing.Point(163, 0);
this.tr2.Name = "tr2";
this.tr2.SelectedImageIndex = -1;
this.tr2.Size = new System.Drawing.Size(365, 232);
this.tr2.TabIndex = 3;
this.tr2.AfterCheck += new System.Windows.Forms.TreeViewEventHandler(this.tr2_AfterCheck);
this.tr2.DoubleClick += new System.EventHandler(this.tr2_DoubleClick);
//
// splitter1
//
this.splitter1.Location = new System.Drawing.Point(160, 0);
this.splitter1.Name = "splitter1";
this.splitter1.Size = new System.Drawing.Size(3, 232);
this.splitter1.TabIndex = 2;
this.splitter1.TabStop = false;
//
// listBox1
//
this.listBox1.Dock = System.Windows.Forms.DockStyle.Left;
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(0, 0);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(160, 232);
this.listBox1.TabIndex = 1;
this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
//
// panel2
//
this.panel2.Controls.Add(this.panel4);
this.panel2.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel2.Location = new System.Drawing.Point(0, 79);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(528, 232);
this.panel2.TabIndex = 3;
//
// FrmPerID
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(528, 333);
this.Controls.Add(this.panel2);
this.Controls.Add(this.panel1);
this.Controls.Add(this.toolBar1);
this.Controls.Add(this.statusBar1);
this.Menu = this.mainMenu1;
this.Name = "FrmPerID";
this.Text = "分配用户权限";
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
this.Load += new System.EventHandler(this.FrmPerID_Load);
this.panel1.ResumeLayout(false);
this.panel4.ResumeLayout(false);
this.panel2.ResumeLayout(false);
this.ResumeLayout(false); }
#endregion private void FrmPerID_Load(object sender, System.EventArgs e)
{
InitTree();
InitListBox();
rybh=listBox1.Items[0].ToString().Substring(1,4);
InitPersonPrivileges(true);
}
private void InitListBox()
{
try
{
string str_sql="SELECT OperatorID, OperatorName FROM SYS_Operators";
System.Data.OleDb.OleDbCommand cmd = new OleDbCommand(str_sql,ERM.PubClass.AdoConnect());
if(cmd.Connection.State.ToString().ToUpper()!="OPEN")
{
cmd.Connection.Open();
}
System.Data.OleDb.OleDbDataReader rd = cmd.ExecuteReader();
if(rd.HasRows)
{
while(rd.Read())
{
listBox1.Items.Add("["+rd.GetString(0)+"]"+rd.GetString(1));
}
}
cmd.Connection.Close();
rd.Close(); }
catch(Exception ex)
{
MessageBox.Show(ex.Message);
} }
/// <summary>
/// 初始人员权限IorS false
/// 保存人员权限IorS true
/// </summary>
/// <param name="IorS"></param>
private void InitPersonPrivileges(bool IorS)
{
int i;
TreeNode tnode= new TreeNode();
for(i=0;i<tr2.Nodes.Count;i++)
{
tnode=tr2.Nodes[i];
if (IorS)
{
CheckNode(tnode,true,false);
}
else
{
CheckNode(tnode,false,true);
}
}
}
/// <summary>
/// 初始树结构第一层;
/// </summary>
private void InitTree()
{
try
{
TreeNode onenode;
this.myds=new System.Data.DataSet();
this.myda=new System.Data.OleDb.OleDbDataAdapter("select operatorid,modeid from sys_operatorprivileges",ERM.ClfClass.AdoConnect());
this.myda.Fill(this.myds,"sys_operatorprivileges");
this.dv=new System.Data.DataView(this.myds.Tables["sys_operatorprivileges"],"","operatorid,modeid",System.Data.DataViewRowState.CurrentRows);
this.cmdb=new System.Data.OleDb.OleDbCommandBuilder(this.myda);
this.mybind=this.BindingContext[this.myds,"sys_operatorprivileges"];
string Osql="select distinct subMainindex,submaintitle from SYS_Privileges";
System.Data.OleDb.OleDbCommand Ocmd=new OleDbCommand(Osql,ERM.PubClass.AdoConnect());
if (Ocmd.Connection.State.ToString().ToUpper()!="OPEN")
{
Ocmd.Connection.Open();
}
System.Data.OleDb.OleDbDataReader Omyrd = Ocmd.ExecuteReader();
if (Omyrd.HasRows)
{
while(Omyrd.Read())
{
onenode=new TreeNode();
onenode.Text=Omyrd.GetString(1);
this.tr2.Nodes.Add(onenode);
Initsectree(onenode,Omyrd.GetInt32(0).ToString());
}
}
Omyrd.Close();
Ocmd.Connection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Initsectree(TreeNode onenode,string subindex)
{
TreeNode secnode;
string Ssql="select distinct titleindex,titleCaption from SYS_Privileges where submainindex = "+subindex;
System.Data.OleDb.OleDbCommand Scmd=new OleDbCommand(Ssql,ERM.PubClass.AdoConnect());
if(Scmd.Connection.State.ToString().ToUpper()!="OPEN")
{
Scmd.Connection.Open();
}
System.Data.OleDb.OleDbDataReader Smyrd = Scmd.ExecuteReader();
if (Smyrd.HasRows)
{
while(Smyrd.Read())
{
secnode=new TreeNode();
secnode.Text=Smyrd.GetString(1);
onenode.Nodes.Add(secnode);
InitThrtree(secnode,subindex,Smyrd.GetInt32(0).ToString());
}
}
Scmd.Connection.Close();
Smyrd.Close();
}
private void InitThrtree(TreeNode onenode,string subindex,string cellindex)
{
TreeNode thrnode;
string Tsql="select distinct id ,cellcaption from SYS_Privileges where submainindex = "+subindex+" and titleindex= "+cellindex;
System.Data.OleDb.OleDbCommand Tcmd=new OleDbCommand(Tsql,ERM.PubClass.AdoConnect());
if(Tcmd.Connection.State.ToString().ToUpper()!="OPEN")
{
Tcmd.Connection.Open();
}
System.Data.OleDb.OleDbDataReader Tmyrd = Tcmd.ExecuteReader();
if (Tmyrd.HasRows)
{
while(Tmyrd.Read())
{
thrnode=new TreeNode();
thrnode.Text="["+Tmyrd.GetString(0).ToString()+"]"+Tmyrd.GetString(1).ToString();
onenode.Nodes.Add(thrnode);
}
}
Tcmd.Connection.Close();
Tmyrd.Close();
} private void menuItem3_Click(object sender, System.EventArgs e)
{
System.Windows.Forms.TreeNode Trnode;
Trnode=this.tr2.SelectedNode;
CheckNode(Trnode,true,false);
}
/// <summary>
/// ////选择结点的叶子结点选择状态;
/// </summary>
/// <param name="Pnode"></param>
private void CheckNode(TreeNode Pnode,bool Cb,bool Sb)
{
string sys_id;
int i;
TreeNode Cnode= new TreeNode();
Cnode = Pnode.FirstNode;
for(i=0;i<Pnode.Nodes.Count;i++)
{
Cnode.Expand();
if(Cnode.Nodes.Count>0)
{
CheckNode(Cnode,Cb,Sb);
}
if(Cnode.Text.ToString().Substring(0,1)=="[")
{
///初始人员权限权限
///
int tem = 0;
tem = Cnode.Text.ToString().IndexOf("]",0,Cnode.Text.Length);
sys_id=Cnode.Text.ToString().Substring(1,tem-1);
if(Cb)
{
Cnode.Checked =CheckPid(sys_id);
}
///人员权限保存
if(Sb && Cnode.Checked)
{
ExcuteSql("insert sys_operatorprivileges(operatorId,ModeId) values('"+rybh+"','"+sys_id+"')");
//save_pid(sys_id);
} }
Cnode=Cnode.NextNode;
} }
/// <summary>
/// 检查人员是否有该ModeID 的权限;
/// </summary>
/// <param name="sys_id"></param>
/// <returns></returns>
private bool CheckPid(string sys_id)
{ bool b_test=false;
System.Data.DataRowView[] dvrow;
dvrow=dv.FindRows(new System.Object[]{rybh,sys_id});
if (dvrow.Length> 0)
{
b_test=true;
}
return b_test; } private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
this.myda=new System.Data.OleDb.OleDbDataAdapter("select operatorid,modeid from sys_operatorprivileges",ERM.ClfClass.AdoConnect());
this.myda.Fill(this.myds,"sys_operatorprivileges");
this.dv=new System.Data.DataView(this.myds.Tables["sys_operatorprivileges"],"","operatorid,modeid",System.Data.DataViewRowState.CurrentRows);
this.cmdb=new System.Data.OleDb.OleDbCommandBuilder(this.myda);
rybh=listBox1.Items[listBox1.SelectedIndex].ToString().Substring(1,4);
InitPersonPrivileges(true);
} private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
switch( e.Button.Text)
{
case "保存":
ExcuteSql("delete sys_operatorprivileges where operatorId= '"+rybh+"'");
InitPersonPrivileges(false);
break;
case "取消":
break;
case "增加":
ERM.FrmUSER_MANAGE frm = new FrmUSER_MANAGE();
frm.ShowDialog();
break;
case "注销":
if (MessageBox.Show("确认要删除此用户吗?","提示",System.Windows.Forms.MessageBoxButtons.OKCancel,System.Windows.Forms.MessageBoxIcon.Question,System.Windows.Forms.MessageBoxDefaultButton.Button1)==System.Windows.Forms.DialogResult.OK)
{
ExcuteSql("delete sys_operatorprivileges where operatorId= '"+rybh+"'");
ExcuteSql("delete sys_operators where operatorid='"+rybh+"'");
listBox1.Items.Remove(listBox1.SelectedIndices);
}
break;
case "展开":
tr2.ExpandAll();
break;
case "合并":
tr2.CollapseAll();
break;
case "退出":
this.Close();
break;
}
}
private void Check1(TreeNode Mnode,bool bg)
{
int i;
TreeNode Nnode= new TreeNode();
Nnode =Mnode.FirstNode;
for(i=0;i<Mnode.Nodes.Count;i++)
{
Mnode.Expand();
if(Nnode.Nodes.Count>0)
{
Check1(Mnode,bg);
}
if(Nnode.Text.ToString().Substring(0,1)=="[")
{
Nnode.Checked =bg;
}
Nnode=Nnode.NextNode;
}
} private void tr2_DoubleClick(object sender, System.EventArgs e)
{
} private void ExcuteSql(string sql)
{
System.Data.OleDb.OleDbCommand mycmd = new OleDbCommand(sql,ERM.PubClass.AdoConnect());
mycmd.Connection.Open();
try
{
mycmd.ExecuteNonQuery();
mycmd.Connection.Close();
}
catch (Exception ex)
{
mycmd.Connection.Close();
MessageBox.Show(ex.Message);
}
} private void tr2_AfterCheck(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
}
}
}
1.将工具箱中的treeview控件拉到页面;
2.选中treeview控件,按F4键弹出treeview控件的属性对话框;
3.选中属性对话框的Nodes属性,这时出现一个按钮(图标是"...");
点击它,将出现一个对话框(名为TreeNodeEditor);
4.对话框TreeNodeEditor的左下方有一个Add Root按钮,点击它将新增一个节点;
点中该节点,在对话框TreeNodeEditor的右边找出CheckBox属性,将它的值设为True;
点击OK按钮就可以了.
用控件很容易实现
求含checkbox的treeview树型结构的简单例子,…… 是能和数据库结合的简单例子,可能偶表达不明确,不好意思