Hello, Im new to c# and i have a little problem.

I have a folder with 5 text files (group1,group2, etc), each contains a list of names.

On my form, I have 2 combo boxes. I want the first one to allow me to select a group (a text file), and once the file is selected, to populate the second one with the names inside it. is that possible? and if it is, how to do it?

You have my gratitude in advance.A

Alex.

What you're describing can be easy depending on your background in any programming language.

Do you know about System.IO.Directory.GetFiles()?

You can use that inside your Form_Load() to get the names of the files in a directory.
You'll need to split the path off of the names for the content you will put in your combo box of group names.

Other than that, using File.ReadAllLines() should let you get the content from each of those files when necessary.

What you're describing can be easy depending on your background in any programming language.

Do you know about System.IO.Directory.GetFiles()?

You can use that inside your Form_Load() to get the names of the files in a directory.
You'll need to split the path off of the names for the content you will put in your combo box of group names.

Other than that, using File.ReadAllLines() should let you get the content from each of those files when necessary.

I dont have background experience in any language, im just starting.

But yeah, thanks for that lead, i googled it and this came up

 foreach (String file in System.IO.Directory.GetFiles(@"C:\blablabla\"))
        {

            comboBox1.Items.Add(new System.IO.FileInfo(file).Name);
        }

The combobox was populated with what i had inside that directory. Time to seach the next one.

in C# make an open file dialouge

set the Filter in the properties to Text files (*.txt)|*.txt|All files (*.*)|*.*

DialogResult result = openFileDialog1.ShowDialog();

 //Directory of the file that is selected
            string file = openFileDialog1.FileName;
			
			
            string[] split = File.ReadAllLines(file);

The string will have all the values in that file

Hi waleedqk. Welcome to DaniWeb.

There is no need to do a dialog with this if the task is to have the file names show in a combo box.

Something like this would do:

private void Form1_Load(object sender, EventArgs e)
      {
         comboBox1.DataSource =
            Directory.GetFiles(strDataDir, "group*.txt")
            .Select(s =>  (new FileInfo(s)).Name.Split('.')[0])
            .ToList();
      }

@Alientcp, what happens if you make a method for the combo box's SelectedIndexChanged and tell it to read the contents of the file (name) selected into the second combo box?

Hi waleedqk. Welcome to DaniWeb.

There is no need to do a dialog with this if the task is to have the file names show in a combo box.

Something like this would do:

private void Form1_Load(object sender, EventArgs e)
      {
         comboBox1.DataSource =
            Directory.GetFiles(strDataDir, "group*.txt")
            .Select(s =>  (new FileInfo(s)).Name.Split('.')[0])
            .ToList();
      }

Oh, yeah, this is in fact a proyect in programming class, forgot that i had to give a proof that i already have some work, here is it. (its on spanish, but very simple.)

screen 1 is a log in screen (working) : http://imageshack.us/photo/my-images/839/pantalla1i.png/

screen 2 is a menu screen with nothing but 3 options at the moment (working) (forgot to screen it)

screen 3 is first option, to create an inventory and saves it into a .txt file (working): http://imageshack.us/photo/my-images/215/pantalla2.png/

Screen 4 is the second op. to create a group list and saves it into a .txt file (working) : http://imageshack.us/photo/my-images/811/pantalla3f.png/

screen 5 is to ask requisition of material of that previously created inventory and groups (partially working, and where im asking for help): http://imageshack.us/photo/my-images/259/pantalla4.png/

Now, in this last screen the "group and category" combobox already shows me a list of the files that i already created from the other screens.

What it doesnt do yet is to populate the "encargado" section after loading "grupo" and the "articulo" after loading "categoria". Ill try the suggested options that you guys gave me and then update again

@Alientcp, what happens if you make a method for the combo box's SelectedIndexChanged and tell it to read the contents of the file (name) selected into the second combo box?

I have no idea, the teacher is rushing, we just finished cicles in structured programming, we havent touched arrays, classes or methods yet, and i dont know hot to do that. I seriously dont know what we are doing this at this stage yet. I went this far copying codes from different forums and youtube tutorials.

Based on those images, you're populating a listbox.
The technique will be the same.
If you read your data from the file into a List<string> OR convert it to a List with the .ToList() when you ReadAllLines(), you can set the DataSource of the listbox to the return value from ReadAllLines().ToList() and the listbox will be filled with the file contents.
Mock-up attached.

Based on those images, you're populating a listbox.
The technique will be the same.
If you read your data from the file into a List<string> OR convert it to a List with the .ToList() when you ReadAllLines(), you can set the DataSource of the listbox to the return value from ReadAllLines().ToList() and the listbox will be filled with the file contents.
Mock-up attached.

I found the solution. It was among the lines, but it was with an array. Sorry for the delayed response, this was my exam week and i had a lot of work.

When the form loads (under actions, that is on the little bolt on the properties section of the form), double click and use this code.

// This is to populate the comboBox1 with the .txt files on your specific folder

private void form_Load(object sender, EventArgs e)
        {

            string[] files = System.IO.Directory.GetFiles(@"c:\example");
             this.comboBox1.Items.AddRange(files);

        }

//then under the properties of the Combobox1, select the actions (the little bolt again), under selected index changed, double click and use //this code.

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox2.Items.Clear();
            string[] lineOfContents = File.ReadAllLines(comboBox1.Text);
            foreach (var line in lineOfContents)
            {
                string[] tokens = line.Split(',');
                comboBox2.Items.Add(tokens[0]);
            }

//and thats it, once you select the file in combobox1, the contents appear as a list in the combobox2 to manipulate at will.

Please mark this with the proper title as closed and solved. Thank you all for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.