本文介绍了如何在c#应用程序中选择和取消选择树视图复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过示例我将逻辑驱动程序作为TreeView,我在我的应用程序中选择了两个按钮,SELECT ALL并取消选择所有这两个按钮。





当我最初执行应用程序时,处于已检查状态的驱动器,如果我想取消选中驱动器所有复选框都取消选中,那没关系!但是,子文件夹无法取消选中,仍然处于取消选中状态。



C#

我想要的代码......请帮帮我...

By the example i have took logical drivers as a TreeView, and i took two buttons in my application, SELECT ALL and Deselect all those two buttons.


When i execute the application initially, the drives in checked State, If I want to Deselect the drives all checkboxes are uncheck, thats ok! But, subfolders can't uncheck, Still it remains in uncheck state.

C#
Code I want... Please help me out...

推荐答案


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace DirectoryLister
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        public bool Checked { get; set; }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {

        }

        private void Form2_Load(object sender, EventArgs e)
        {
            treeView1.CheckBoxes = true;
            foreach (TreeNode node in treeView1.Nodes)
            {
                node.Checked = true;
            }

            string[] drives = Environment.GetLogicalDrives();

            foreach (string drive in drives)
            {
               // treeView1.Nodes[0].Nodes[1].Checked = true;
                DriveInfo di = new DriveInfo(drive);
                int driveImage;

                switch (di.DriveType)
                {
                    case DriveType.CDRom:
                        driveImage = 3;
                        break;
                    case DriveType.Network:
                        driveImage = 6;
                        break;
                    case DriveType.NoRootDirectory:
                        driveImage = 8;
                        break;
                    case DriveType.Unknown:
                        driveImage = 8;
                        break;
                    default:
                        driveImage = 2;
                        break;
                }

                TreeNode node = new TreeNode(drive.Substring(0, 1), driveImage, driveImage);
                node.Tag = drive;


                if (di.IsReady == true)
                    node.Nodes.Add("...");

                treeView1.Nodes.Add(node);


            }

            foreach (TreeNode node in treeView1.Nodes)
            {
                node.Checked = true;
            }
        }


        private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {


            if (e.Node.Nodes.Count > 0)
            {
                if (e.Node.Nodes[0].Text == "..." && e.Node.Nodes[0].Tag == null)
                {
                    e.Node.Nodes.Clear();


                    string[] dirs = Directory.GetDirectories(e.Node.Tag.ToString());

                    foreach (string dir in dirs)

                    {
                        DirectoryInfo di = new DirectoryInfo(dir);
                        TreeNode node = new TreeNode(di.Name, 0, 1);
                        node.Checked = true;

                        try
                        {
                            node.Tag = dir;


                            if (di.GetDirectories().Count() > 0)
                                node.Nodes.Add(null, "...", 0, 0);
                        }
                        catch (UnauthorizedAccessException)
                        {
                            node.ImageIndex = 12;
                            node.SelectedImageIndex = 12;
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "DirectoryLister", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                        }
                        finally
                        {
                            e.Node.Nodes.Add(node);
                        }
                    }

                }
            }

        }

        private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
        {
            foreach (TreeNode childNode in e.Node.Nodes)
            {
                childNode.Checked = e.Node.Checked;

            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            foreach (TreeNode node in treeView1.Nodes)
            {
                node.Checked = true;

            }

         }

        private void button2_Click(object sender, EventArgs e)
        {
            foreach (TreeNode node in treeView1.Nodes)
            {
                 node.Checked = false;
                 treeView1.SelectedNode = null;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Form3 f3 = new Form3();
            f3.ShowDialog();

        }


    }
}


这篇关于如何在c#应用程序中选择和取消选择树视图复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-17 16:47