public partial class SysPropertyManager : Form
    {
        public SysPropertyManager()
        {
            InitializeComponent();
        }        private void SysPropertyManager_Load(object sender, EventArgs e)
        {
            IProperties ip = (IProperties)ServiceManager.Services.GetService(typeof(IProperties));            //初始化控件
              List<SelectItemGroup> groupList = GetSelectItemGroupList();
            InitTreeView(this.ShowTree, groupList);
            SetTreeviewFocus(ShowTree);
        }        /// <summary>
        /// 初始化时选中第一个叶子节点
         /// </summary>
        /// <param name="node">第一个根节点</param>
        private void SetTreeviewFocus(TreeView treeview)
        {
            TreeNodeCollection tnc = treeview.Nodes;
            List<TreeNode> leafNodes = new List<TreeNode>();
            AllOverTree(tnc,ref leafNodes);
            treeview.Focus();
            treeview.SelectedNode = leafNodes[0];
            leafNodes[0].Checked = true;
        }        /// <summary>
        /// 遍历树,取得叶子节点集合
         /// </summary>
        /// <param name="tnc"></param>
        private List<TreeNode> AllOverTree(TreeNodeCollection tnc, ref List<TreeNode> leafNodes)
        {
            foreach (TreeNode node in tnc)
            {
                if (node.Nodes.Count != 0)
                {
                    AllOverTree(node.Nodes,ref leafNodes);
                }
                else
                {
                    leafNodes.Add(node);
                }
            }
            return leafNodes;
        }
     }
如以上代码,我想在SysPropertyManager启动时初始化treeview,并且将该树的第一个叶子节点置为选中状态。可是一直都没有得出我要的效果。
注:我的treeview是放在TabControl里的。每次运行的结果都是Tabcontrol被选中。帮忙看看什么原因~

解决方案 »

  1.   


            private void InitializeComponent()
            {
                this.TabManager = new System.Windows.Forms.TabControl();
                this.PropertyPage = new System.Windows.Forms.TabPage();
                this.splitContainer1 = new System.Windows.Forms.SplitContainer();
                this.ShowTree = new System.Windows.Forms.TreeView();
                this.ManagerPage = new System.Windows.Forms.TabPage();
                this.splitContainer2 = new System.Windows.Forms.SplitContainer();
                this.SetTree = new System.Windows.Forms.TreeView();
                this.CancelBTN = new System.Windows.Forms.Button();
                this.OKBTN = new System.Windows.Forms.Button();
                this.TabManager.SuspendLayout();
                this.PropertyPage.SuspendLayout();
                this.splitContainer1.Panel1.SuspendLayout();
                this.splitContainer1.SuspendLayout();
                this.ManagerPage.SuspendLayout();
                this.splitContainer2.Panel1.SuspendLayout();
                this.splitContainer2.SuspendLayout();
                this.SuspendLayout();
                // 
                // TabManager
                // 
                this.TabManager.Controls.Add(this.PropertyPage);
                this.TabManager.Controls.Add(this.ManagerPage);
                this.TabManager.Location = new System.Drawing.Point(-3, -2);
                this.TabManager.Name = "TabManager";
                this.TabManager.SelectedIndex = 0;
                this.TabManager.Size = new System.Drawing.Size(620, 370);
                this.TabManager.SizeMode = System.Windows.Forms.TabSizeMode.Fixed;
                this.TabManager.TabIndex = 0;
                // 
                // PropertyPage
                // 
                this.PropertyPage.Controls.Add(this.splitContainer1);
                this.PropertyPage.Location = new System.Drawing.Point(4, 21);
                this.PropertyPage.Name = "PropertyPage";
                this.PropertyPage.Padding = new System.Windows.Forms.Padding(3);
                this.PropertyPage.Size = new System.Drawing.Size(612, 345);
                this.PropertyPage.TabIndex = 0;
                this.PropertyPage.Text = "设置";
                this.PropertyPage.UseVisualStyleBackColor = true;
                // 
                // splitContainer1
                // 
                this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
                this.splitContainer1.Location = new System.Drawing.Point(3, 3);
                this.splitContainer1.Name = "splitContainer1";
                // 
                // splitContainer1.Panel1
                // 
                this.splitContainer1.Panel1.Controls.Add(this.ShowTree);
                this.splitContainer1.Size = new System.Drawing.Size(606, 339);
                this.splitContainer1.SplitterDistance = 220;
                this.splitContainer1.TabIndex = 0;
                // 
                // ShowTree
                // 
                this.ShowTree.Dock = System.Windows.Forms.DockStyle.Fill;
                this.ShowTree.LineColor = System.Drawing.Color.Silver;
                this.ShowTree.Location = new System.Drawing.Point(0, 0);
                this.ShowTree.Name = "ShowTree";
                this.ShowTree.Size = new System.Drawing.Size(220, 339);
                this.ShowTree.TabIndex = 0;
                // 
                // ManagerPage
                // 
                this.ManagerPage.Controls.Add(this.splitContainer2);
                this.ManagerPage.Location = new System.Drawing.Point(4, 21);
                this.ManagerPage.Name = "ManagerPage";
                this.ManagerPage.Padding = new System.Windows.Forms.Padding(3);
                this.ManagerPage.Size = new System.Drawing.Size(612, 345);
                this.ManagerPage.TabIndex = 1;
                this.ManagerPage.Text = "管理";
                this.ManagerPage.UseVisualStyleBackColor = true;
                // 
                // splitContainer2
                // 
                this.splitContainer2.Dock = System.Windows.Forms.DockStyle.Fill;
                this.splitContainer2.Location = new System.Drawing.Point(3, 3);
                this.splitContainer2.Name = "splitContainer2";
                // 
                // splitContainer2.Panel1
                // 
                this.splitContainer2.Panel1.Controls.Add(this.SetTree);
                this.splitContainer2.Size = new System.Drawing.Size(606, 339);
                this.splitContainer2.SplitterDistance = 220;
                this.splitContainer2.TabIndex = 1;
                // 
                // SetTree
                // 
                this.SetTree.Dock = System.Windows.Forms.DockStyle.Fill;
                this.SetTree.LineColor = System.Drawing.Color.Silver;
                this.SetTree.Location = new System.Drawing.Point(0, 0);
                this.SetTree.Name = "SetTree";
                this.SetTree.Size = new System.Drawing.Size(220, 339);
                this.SetTree.TabIndex = 2;
                // 
                // CancelBTN
                // 
                this.CancelBTN.DialogResult = System.Windows.Forms.DialogResult.Cancel;
                this.CancelBTN.Location = new System.Drawing.Point(508, 390);
                this.CancelBTN.Name = "CancelBTN";
                this.CancelBTN.Size = new System.Drawing.Size(75, 23);
                this.CancelBTN.TabIndex = 4;
                this.CancelBTN.Text = "取消";
                this.CancelBTN.UseVisualStyleBackColor = true;
                // 
                // OKBTN
                // 
                this.OKBTN.Location = new System.Drawing.Point(387, 391);
                this.OKBTN.Name = "OKBTN";
                this.OKBTN.Size = new System.Drawing.Size(75, 23);
                this.OKBTN.TabIndex = 3;
                this.OKBTN.Text = "确认";
                this.OKBTN.UseVisualStyleBackColor = true;
                this.OKBTN.Click += new System.EventHandler(this.OKBTN_Click);
                // 
                // SysPropertyManager
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(614, 426);
                this.Controls.Add(this.CancelBTN);
                this.Controls.Add(this.OKBTN);
                this.Controls.Add(this.TabManager);
                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
                this.Name = "SysPropertyManager";
                this.Text = "选项";
                this.Load += new System.EventHandler(this.SysPropertyManager_Load);
                this.TabManager.ResumeLayout(false);
                this.PropertyPage.ResumeLayout(false);
                this.splitContainer1.Panel1.ResumeLayout(false);
                this.splitContainer1.ResumeLayout(false);
                this.ManagerPage.ResumeLayout(false);
                this.splitContainer2.Panel1.ResumeLayout(false);
                this.splitContainer2.ResumeLayout(false);
                this.ResumeLayout(false);
            }
    这个是自动生成的代码;我尝试将this.PropertyPage.TabIndex = 0;重新设置也是一样的
    请诸位帮忙看看
      

  2.   

    你这个是CS的
    可以加个ImageList 控件 添加几个图标设置顺序,写Node节点的时候分配下Node的ImageIndex 还有他的selectImageindex 
    在treeview_after  事件里写上 node节点选定的imageindex 是哪个就可以了