473,406 Members | 2,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How do I select an item in listBox so as to create new directories?

What I want to do is, once I double click or select an item in a listbox it should create new directory/folder in a specific path. Below is the code for creating the listBox but I do not know how to create a double click event.

Expand|Select|Wrap|Line Numbers
  1. private void button1_Click(object sender, EventArgs e)
  2.         {
  3.  
  4.             ListBox listBox1 = new ListBox();
  5.  
  6.             listBox1.Size = new System.Drawing.Size(200, 100);
  7.             listBox1.Location = new System.Drawing.Point(10, 10);
  8.  
  9.  
  10.             this.Controls.Add(listBox1);
  11.  
  12.             listBox1.MultiColumn = true;
  13.  
  14.             listBox1.SelectionMode = SelectionMode.MultiExtended;
  15.  
  16.             listBox1.BeginUpdate();
  17.  
  18.  
  19.             for (int x = 1; x <= 10; x++)
  20.             {
  21.                 listBox1.Items.Add("N-TYPE" + x.ToString());
  22.             }
  23.            the new items.
  24.             listBox1.EndUpdate();
  25.  
  26.  
  27.             listBox1.SetSelected(1, true);
  28.             listBox1.SetSelected(3, true);
  29.             listBox1.SetSelected(5, true);
  30.  
  31.  
  32.             System.Diagnostics.Debug.WriteLine(listBox1.SelectedItems[1].ToString());
  33.  
  34.  
  35.             System.Diagnostics.Debug.WriteLine(listBox1.SelectedIndices[0].ToString());
  36.  
  37.         }
  38.  
Basically I want the "N-TYPE1" also to be some folder is "E:\\New Files"

so that would make it "E:\\New Files\\N-TYPE1";
and then I also want to create new directories once N-TYPE1 or N-TYPE2 is selected.

So now it would be
"E:\\New Files\\N-TYPE1\\New Settings"

Is there any solution to this? Am I headed in wrong direction?
Thank You in advance.
Apr 1 '13 #1

✓ answered by vijay6

Hey user033088, If you get any error then post the error here then only others can understand. This is the code i wrote for your question. Try this code,

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Drawing;
  3. using System.IO;
  4. using System.Windows.Forms;
  5.  
  6. namespace WindowsFormsApplication1
  7. {
  8.     public partial class Form1 : Form
  9.     {
  10.  
  11.         private Button button1;
  12.         private ListBox listBox1;
  13.  
  14.         private string path;
  15.         private string selecteditem;
  16.  
  17.         public Form1()
  18.         {
  19.             InitializeComponent();
  20.         }
  21.  
  22.         private void Form1_Load(object sender, EventArgs e)
  23.         {
  24.             path = "C:\\Users\\Public";
  25.  
  26.             button1 = new Button();
  27.             button1.Text = "Create ListBox";
  28.             button1.AutoSize = true;
  29.             button1.Location = new Point(100, 150);
  30.             button1.Click +=button1_Click;
  31.             this.Controls.Add(button1);
  32.         }
  33.  
  34.         private void button1_Click(object sender, EventArgs e)
  35.         {
  36.             listBox1 = new ListBox();
  37.             listBox1.Size = new Size(200, 100);
  38.             listBox1.Location = new Point(12, 12);
  39.             listBox1.MultiColumn = true;
  40.             //listBox1.SelectionMode = SelectionMode.MultiExtended;
  41.             listBox1.BeginUpdate();
  42.             listBox1.DoubleClick += listBox1_DoubleClick;
  43.             this.Controls.Add(listBox1);
  44.  
  45.             for (int x = 1; x <= 11; x++)
  46.             {
  47.                 listBox1.Items.Add("N-TYPE" + x.ToString());
  48.             }
  49.         }
  50.  
  51.         private void listBox1_DoubleClick(object sender, EventArgs e)
  52.         {
  53.             selecteditem = String.Empty;
  54.  
  55.             if (listBox1.SelectedItems.Count > 0)
  56.             {
  57.                 selecteditem = listBox1.SelectedItem.ToString();
  58.  
  59.                 if(!Directory.Exists(path + "\\" + selecteditem))
  60.                 {
  61.                     Directory.CreateDirectory(path + "\\" + selecteditem);
  62.                 }
  63.                 else
  64.                 {
  65.                     //Do nothing
  66.                 }
  67.  
  68.                 listBox1.ClearSelected();
  69.             }
  70.         }
  71.     }
  72. }

8 1852
Rabbit
12,516 Expert Mod 8TB
I don't know what you mean by "how to create a double click event". Such an event already exists, you just need to move your code there.
Apr 1 '13 #2
I am very new to C# so pardon my ignorance.
So now with this code I have N-TYPE1......N-TYPE10 items. Now, if I double click on N-TYPE1 I would want to create a new folder.
I have no clue to do this.
Thank You for your help.
Apr 1 '13 #3
Rabbit
12,516 Expert Mod 8TB
I'm confused about what your question is. In your original post, you asked how to create a double click event. But it sounds like your question is really how to create a directory?
Apr 1 '13 #4
vijay6
158 100+
Hey user033088

I changed one property of listBox1 in your code (listBox1.SelectionMode property). So at a time you can select only one item in listBox1.

Expand|Select|Wrap|Line Numbers
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3.  
  4. listBox1 = new ListBox();
  5. listBox1.Size = new Size(200, 100);
  6. listBox1.Location = new Point(10, 10);
  7. listBox1.MultiColumn = true;
  8. //listBox1.SelectionMode = SelectionMode.MultiExtended;
  9. listBox1.BeginUpdate();
  10.  
  11. listBox1.DoubleClick += listBox1_DoubleClick;
  12.  
  13. this.Controls.Add(listBox1);
  14.  
  15. for (int x = 1; x <= 11; x++)
  16. {
  17. listBox1.Items.Add("N-TYPE" + x.ToString());
  18. }
  19.  
  20. }
  21.  
  22. void listBox1_DoubleClick(object sender, EventArgs e)
  23. {
  24.  
  25. string path = "E:\\New Files";
  26.  
  27. string selecteditem = String.Empty;
  28.  
  29. if (listBox1.SelectedItems.Count > 0)
  30. {
  31. selecteditem = listBox1.SelectedItem.ToString();
  32.  
  33. if(!Directory.Exists(path + "\\" + selecteditem))
  34. {
  35. Directory.CreateDirectory(path + "\\" + selecteditem);
  36. }
  37. else
  38. {
  39. //Do nothing
  40. }
  41.  
  42. listBox1.ClearSelected();
  43. }
Code with explanations

Expand|Select|Wrap|Line Numbers
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3.  
  4. listBox1 = new ListBox();
  5. listBox1.Size = new Size(200, 100);
  6. listBox1.Location = new Point(10, 10);
  7. listBox1.MultiColumn = true;
  8. //listBox1.SelectionMode = SelectionMode.MultiExtended;
  9. listBox1.BeginUpdate();
  10.  
  11. // Whenever you double click listBox1, the following line will call listBox1_DoubleClick Event. This is how you've to write an event for all controls. To know more about how to create events for controls refer the following link. http://www.c-sharpcorner.com/UploadF...eControls.aspx
  12.  
  13. listBox1.DoubleClick += listBox1_DoubleClick;
  14.  
  15. this.Controls.Add(listBox1);
  16.  
  17. for (int x = 1; x <= 11; x++)
  18. {
  19. listBox1.Items.Add("N-TYPE" + x.ToString());
  20. }
  21.  
  22. }
  23.  
  24. void listBox1_DoubleClick(object sender, EventArgs e)
  25. {
  26.  
  27. string path = "E:\\New Files"; // This variable stores the parent path.
  28.  
  29. string selecteditem = String.Empty; // This variable is used to store the selected item's value in listBox1.
  30.  
  31. if (listBox1.SelectedItems.Count > 0) // This statement is used to check whether atleast one listBox1 item is selected or not.
  32. {
  33. selecteditem = listBox1.SelectedItem.ToString(); // In this line, the listBox1's selected item's value is stored to 'selecteditem' variable.
  34.  
  35. // For example assume you selected (Double Clicks) 'N-TYPE1' item in listBox1.
  36.  
  37. if(!Directory.Exists(path + "\\" + selecteditem)) // This line check whether 'N-TYPE1' folder exists or not in parent directory. i.e, Checks whether 'E:\\New Files\\N-TYPE1' already directory exists or not. If directory not exists means body of if statement will execute.
  38. {
  39. Directory.CreateDirectory(path + "\\" + selecteditem); // This directory will create 'N-TYPE1' folder inside 'E:\\New Files' directory.
  40. }
  41. else // You don't need else statement. If you want you can remove else block.
  42. {
  43. //Do nothing
  44. }
  45.  
  46. listBox1.ClearSelected(); // This line will clear the selected item in listBox1.
  47. }
Hope this may help you.

Have a nice day (:
Apr 2 '13 #5
Thank you so much for replying. I tried the code but I m pretty sure I am doing something wrong. So once I click Start a new ListBox pops up which shows all the items (N-TYPE1.....N-TYPE11), which is correct. But The doubleClick part gives me a error for where ever I am calling listBox1. It goes away only after I add a new ListBox from the toolbox. But still it wont create the directory. What is it that I am doing wrong. Please help me. I really appreciate it. Thanks.!!
Apr 2 '13 #6
@Rabbit: I am not good at explaining in English but yes that is what I am looking for. I want to create new Directory but only once I click any item in the ListBox.
Apr 2 '13 #7
vijay6
158 100+
Hey user033088, If you get any error then post the error here then only others can understand. This is the code i wrote for your question. Try this code,

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Drawing;
  3. using System.IO;
  4. using System.Windows.Forms;
  5.  
  6. namespace WindowsFormsApplication1
  7. {
  8.     public partial class Form1 : Form
  9.     {
  10.  
  11.         private Button button1;
  12.         private ListBox listBox1;
  13.  
  14.         private string path;
  15.         private string selecteditem;
  16.  
  17.         public Form1()
  18.         {
  19.             InitializeComponent();
  20.         }
  21.  
  22.         private void Form1_Load(object sender, EventArgs e)
  23.         {
  24.             path = "C:\\Users\\Public";
  25.  
  26.             button1 = new Button();
  27.             button1.Text = "Create ListBox";
  28.             button1.AutoSize = true;
  29.             button1.Location = new Point(100, 150);
  30.             button1.Click +=button1_Click;
  31.             this.Controls.Add(button1);
  32.         }
  33.  
  34.         private void button1_Click(object sender, EventArgs e)
  35.         {
  36.             listBox1 = new ListBox();
  37.             listBox1.Size = new Size(200, 100);
  38.             listBox1.Location = new Point(12, 12);
  39.             listBox1.MultiColumn = true;
  40.             //listBox1.SelectionMode = SelectionMode.MultiExtended;
  41.             listBox1.BeginUpdate();
  42.             listBox1.DoubleClick += listBox1_DoubleClick;
  43.             this.Controls.Add(listBox1);
  44.  
  45.             for (int x = 1; x <= 11; x++)
  46.             {
  47.                 listBox1.Items.Add("N-TYPE" + x.ToString());
  48.             }
  49.         }
  50.  
  51.         private void listBox1_DoubleClick(object sender, EventArgs e)
  52.         {
  53.             selecteditem = String.Empty;
  54.  
  55.             if (listBox1.SelectedItems.Count > 0)
  56.             {
  57.                 selecteditem = listBox1.SelectedItem.ToString();
  58.  
  59.                 if(!Directory.Exists(path + "\\" + selecteditem))
  60.                 {
  61.                     Directory.CreateDirectory(path + "\\" + selecteditem);
  62.                 }
  63.                 else
  64.                 {
  65.                     //Do nothing
  66.                 }
  67.  
  68.                 listBox1.ClearSelected();
  69.             }
  70.         }
  71.     }
  72. }
Apr 3 '13 #8
Thank You sooo much. Its exactly what I was looking for!!!
Thanks.!!
Apr 3 '13 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: RC | last post by:
I have a form where the user enters the Model info. The user picks the Model from either a combobox or listbox which both are filled from the Models table. If the Model type isn't in the boxes,...
1
by: kerravon.geo | last post by:
I have a listbox whose rowsource is set to a memo field containing a semi-colon delimited value list. Upon initial opening of the form, I can't select an item from the list. However, if I add a...
1
by: BillyB | last post by:
I have a context menu tied to a list box. When I right-click on an item, I'd like that item to be selected before the context menu invokes, similar to how Outlook Express works when right-clicking...
1
by: Pawan Singh | last post by:
Hi, Is it possible to select a list box item when someone right clicks on it? I can find the snippets of code for VB6 but that code will not work in VB.Net. Thanks Pawan
5
by: mabond | last post by:
Hi My project has a form, two list boxes (lstArchive and lstSchedule) and two buttons (btnSendArchive and btnSendSchedule) Each list box represents the content of a directory. The command...
8
by: daddydfsu via AccessMonster.com | last post by:
I am trying to create a ListBox based on a search from a TextBox. I have a TextBox where I will enter in a MemberAlias. I want to click on a Search Button and that will do a select from a...
4
by: rn5a | last post by:
I have a ListBox which should list all the files & directories that exist in a particular directory. The problem is I can get the ListBox to list either all the files or all the directories but not...
3
by: Lohboy | last post by:
Using ASP.NET and IE7. (Sorry if I am posting in the wrong forum but my problem seemed to be more related to the JavaScript side than the ASP.NET side.) I have two DropDownList controls the...
2
by: 6afraidbecause789 | last post by:
Hi - Has anyone ever used toggle buttons to select items in a listbox? I'd like to put about 24 toggle buttons on an unbound form that select or deselect items in a multiple select listbox. I've...
1
by: gayat jain | last post by:
jsf <h:select item> how i can call bean method one click any select item
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.