How to have checklistbox in checklistbox

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 645 Views - Last Post: 22 September 2013 - 10:06 AM Rate Topic: -----

#1 reni90  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-September 13

How to have checklistbox in checklistbox

Posted 18 September 2013 - 01:34 PM

Maybe a little complicated topic name, but i will try to explain what I mean.
I have some file from which i get dates in formats like
2013/9/1
2013/9/2
2013/9/3
and so on...
Those dates are at the same files that the user of the program can select to download or not with the checklistbox.
I managed to add all the files in the checklistbox so i can check them but I want another option to have.
I want to have Year and month options. So if someone wants do download all files from Year 2013, or mont 2013/5 (5-month)
they check only the year or the month, and than all items that are in those year/month get checked. So I should have a checklistbox like this
2013
2013/9
2013/9/1
2013/9/2
2013/9/3
... abd so on.
Maybe a plus sign to, so i cann hid all items that are under 2013, and unhide it when i go with mnuse button on that plus sign near 2013 so that i can see all dates or monts.

I hope i could explain properly what i want to get. My English might not be well sorry

Is This A Good Question/Topic? 0
  • +

Replies To: How to have checklistbox in checklistbox

#2 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 355
  • View blog
  • Posts: 1,524
  • Joined: 08-April 09

Re: How to have checklistbox in checklistbox

Posted 18 September 2013 - 02:05 PM

Why don't you have 3 comboboxes.
1 for year, 1 for month and 1 for days so if you want to download everything from year 2013 just select year 2013 from 1 combobox and do the download, and if you wanna be more specific select year and month and based on the selection in your comboboxes you can populate your checkboxlist
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3566
  • View blog
  • Posts: 11,082
  • Joined: 05-May 12

Re: How to have checklistbox in checklistbox

Posted 18 September 2013 - 05:47 PM

To me, this is where a tree view with check boxes would be a good fit.

Sent from my MT7001 using Tapatalk 2
Was This Post Helpful? 0
  • +
  • -

#4 reni90  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-September 13

Re: How to have checklistbox in checklistbox

Posted 20 September 2013 - 12:38 PM

are there any tutorials how to use tree view with check boxes?, I couldnt find them
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3458
  • View blog
  • Posts: 11,707
  • Joined: 12-December 12

Re: How to have checklistbox in checklistbox

Posted 20 September 2013 - 12:50 PM

The documentation, and its example, should be kept close by:

TreeView.Checkboxes :MSDN

I wouldn't know about other tutorials for this (without searching).

One of the first things I would do, though, is to add a TreeView Control to a Form, change its CheckBoxes property to True and use the little arrow (on the Control) to add a few Nodes. (Perhaps you've already done this.)

This post has been edited by andrewsw: 20 September 2013 - 12:57 PM

Was This Post Helpful? 0
  • +
  • -

#6 reni90  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-September 13

Re: How to have checklistbox in checklistbox

Posted 21 September 2013 - 02:58 PM

ok i am near to make it finish. Now i would like to know how to make selected child nodes when parent node is selected?
i tired this code

private void node_AfterCheck(object sender, TreeViewEventArgs e)
        {
            // The code only executes if the user caused the checked state to change. 
            if (e.Action != TreeViewAction.Unknown)
            {
                if (e.Node.Nodes.Count > 0)
                {
                    /* Calls the CheckAllChildNodes method, passing in the current 
                    Checked value of the TreeNode whose checked state changed. */
                    this.CheckAllChildNodes(e.Node, e.Node.Checked);
                }
            }
        }

        private void CheckAllChildNodes(TreeNode treeNode, bool nodeChecked)
        {
            foreach (TreeNode node in treeNode.Nodes)
            {
                node.Checked = nodeChecked;
                if (node.Nodes.Count > 0)
                {
                    // If the current node has child nodes, call the CheckAllChildsNodes method recursively. 
                    this.CheckAllChildNodes(node, nodeChecked);
                }
            }
        }


But doesnt work
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3458
  • View blog
  • Posts: 11,707
  • Joined: 12-December 12

Re: How to have checklistbox in checklistbox

Posted 21 September 2013 - 03:02 PM

In what way doesn't it work? If you receive errors please post them.

There is a debugging tutorial linked in my signature that would enable you to step through your code.

This post has been edited by andrewsw: 21 September 2013 - 03:04 PM

Was This Post Helpful? 0
  • +
  • -

#8 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 355
  • View blog
  • Posts: 1,524
  • Joined: 08-April 09

Re: How to have checklistbox in checklistbox

Posted 21 September 2013 - 03:26 PM

Yes, andrewsw is right, there is a difference between "it doesn't work" and "it doesn't work the way i want" , your program will do what you tell it to do, not what you want.

This post has been edited by Michael26: 21 September 2013 - 03:26 PM

Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3458
  • View blog
  • Posts: 11,707
  • Joined: 12-December 12

Re: How to have checklistbox in checklistbox

Posted 21 September 2013 - 03:41 PM

The OP can should confirm if there is an error message, but the code looks like it should work (on a brief reading). I would throw a MessageBox into the AfterCheck event to make sure it is running at all.

I see that the code is taken directly from the previously linked MSDN-page. In which case, the TreeView would have to be named node.

This post has been edited by andrewsw: 21 September 2013 - 03:46 PM

Was This Post Helpful? 0
  • +
  • -

#10 reni90  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-September 13

Re: How to have checklistbox in checklistbox

Posted 22 September 2013 - 06:49 AM

I just figured out that i need to add this line somewhere
this.treeView1.AfterCheck += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterCheck);

But where and what does taht line do?
Was This Post Helpful? 0
  • +
  • -

#11 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3458
  • View blog
  • Posts: 11,707
  • Joined: 12-December 12

Re: How to have checklistbox in checklistbox

Posted 22 September 2013 - 07:03 AM

This attaches the AfterCheck event to your TreeView. As I mentioned, you need to change your event name from node_AfterCheck (which is from the MSDN example) to treeView1_AfterCheck.

I would also (as suggested..) put a MessageBox in this event to prove that the event is triggered.
Was This Post Helpful? 0
  • +
  • -

#12 reni90  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-September 13

Re: How to have checklistbox in checklistbox

Posted 22 September 2013 - 07:32 AM

OK i renamed it so its now like this
private void CheckAllChildNodes(TreeNode treeNode, bool nodeChecked)
        {
            foreach (TreeNode node in treeNode.Nodes)
            {
                node.Checked = nodeChecked;
                if (node.Nodes.Count > 0)
                {
                    // If the current node has child nodes, call the CheckAllChildsNodes method recursively. 
                    this.CheckAllChildNodes(node, nodeChecked);
                }
            }
        }


        private void treeView_AfterCheck(object sender, TreeViewEventArgs e)
        {
            MessageBox.Show("pritisno si neso");
            // The code only executes if the user caused the checked state to change. 
            if (e.Action != TreeViewAction.Unknown)
            {
                if (e.Node.Nodes.Count > 0)
                {
                    /* Calls the CheckAllChildNodes method, passing in the current 
                    Checked value of the TreeNode whose checked state changed. */
                    this.CheckAllChildNodes(e.Node, e.Node.Checked);
                }
            }
        }


I added messagebox which does not pop out wich means the event is not triggered,i haven't added
1	this.treeView1.AfterCheck += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterCheck);

Because i dont know where to add it and how to modified it so that it works for my code ?
]I added it in public Form1()
and got this : Delegate to an instance method cannot have null 'this
if I delete the this parts and make it look like this
treeView1.AfterCheck += new System.Windows.Forms.TreeViewEventHandler(treeView1_AfterCheck);

I get the same message delegate to an instance methode,....
Was This Post Helpful? 0
  • +
  • -

#13 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3458
  • View blog
  • Posts: 11,707
  • Joined: 12-December 12

Re: How to have checklistbox in checklistbox

Posted 22 September 2013 - 07:46 AM

Your event should be named treeView1_AfterCheck not treeView_AfterCheck (in your main code).

The last code - which attaches the event - can be in your public Form1() constructor, and you can include this.

This post has been edited by andrewsw: 22 September 2013 - 07:51 AM

Was This Post Helpful? 0
  • +
  • -

#14 reni90  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-September 13

Re: How to have checklistbox in checklistbox

Posted 22 September 2013 - 09:41 AM

public Form1()
        {
            InitializeComponent();
            this.treeView1.AfterCheck += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterCheck);
        }
         public void createFolder(string direktorij)
        {
            if (!Directory.Exists(direktorij))
            {
                Directory.CreateDirectory(direktorij);
            }

        }
        private void connect_to_server_Click(object sender, EventArgs e)
        {
            
            WebClient client = new WebClient();
            client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
            client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
           
        // Starts the download
            string url;
            url = "http://";
            url += list_url.Text;
            url += "/INFO.TXT";
            createFolder("C:\\downloads");
          
                client.DownloadFileAsync(new Uri(url), "C:\\downloads\\info.txt");
           
	         connect_to_server.Text = "Download In Process";
	         connect_to_server.Enabled = false;
        }
        

        void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                MessageBox.Show("Cannot connect to server");
            }
            else MessageBox.Show("Download Completed");

            connect_to_server.Text = "Start Download";
            connect_to_server.Enabled = true;
         
            

            

            using (StreamReader r = new StreamReader("C:\\downloads\\info.txt"))
            {
                // 3
                // Use while != null pattern for loop
                string line;
                treeView1.Nodes.Clear();
                  while ((line = r.ReadLine()) != null)
                {
                    // 4
                    // Insert logic here.
                    // ...
                    // "line" is a line in the file. Add it to our List.
                    lines.Add(line);
                    string g=GetYear(line);
                    string mj=GetMonth(line);
                    string dat=g;
                    dat+='/';
                    dat+=mj;
                    int pom=0;
                    
                    for (int i = 0; i < godina.Count; i++)
                    {
                        
                        if (godina[i]==g) pom=1;

                    }
                    if (pom==0) {
                        treeView1.Nodes.Add(g);
                        godina.Add(g);
                    }
                    pom=0;
                     for (int i = 0; i < mjesec.Count; i++)
                    {
                        if (mjesec[i]==dat) pom=1;

                    }   
                    if (pom==0) {
                        treeView1.Nodes[godina.Count-1].Nodes.Add(mj);
                        mjesec.Add(dat);
                    }

                    treeView1.Nodes[godina.Count-1].Nodes[mjesec.Count-1].Nodes.Add(line);
                }
            }
       
           
            foreach (string s in lines)
            {
                checkedList.Items.Add(s);
            }
            checkedList.Visible = true;
            label1.Visible = true; download.Visible = true;
            button1.Visible = true;
        }
     
        private void CheckAllChildNodes(TreeNode treeNode, bool nodeChecked)
        {
            foreach (TreeNode node in treeNode.Nodes)
            {
                node.Checked = nodeChecked;
                if (node.Nodes.Count > 0)
                {
                    // If the current node has child nodes, call the CheckAllChildsNodes method recursively. 
                    this.CheckAllChildNodes(node, nodeChecked);
                }
            }
        }


        private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
        {
            MessageBox.Show("pritisno si neso");
            // The code only executes if the user caused the checked state to change. 
            if (e.Action != TreeViewAction.Unknown)
            {
                if (e.Node.Nodes.Count > 0)
                {
                    /* Calls the CheckAllChildNodes method, passing in the current 
                    Checked value of the TreeNode whose checked state changed. */
                    this.CheckAllChildNodes(e.Node, e.Node.Checked);
                }
            }
        }

Now get this error
Error 3 Ambiguity between 'windows_aplikacija.Form1.treeView1_AfterCheck(object, System.Windows.Forms.TreeViewEventArgs)' and 'windows_aplikacija.Form1.treeView1_AfterCheck' C:\Users\nino\Desktop\IEEE IAS\ja radim kodovi\Monitoring\windows aplikacija\windows aplikacija\Form1.cs 26 93 windows aplikacija
Was This Post Helpful? 0
  • +
  • -

#15 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3458
  • View blog
  • Posts: 11,707
  • Joined: 12-December 12

Re: How to have checklistbox in checklistbox

Posted 22 September 2013 - 09:58 AM

'Ambiguity' suggests that you have two events of the same name in your code somewhere. You might use the Find feature, searching in all files for treeView1_AfterCheck. (The second copy might be in the Designer file.)

This post has been edited by andrewsw: 22 September 2013 - 09:59 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2