简体   繁体   中英

C# pulling out columns from combobox text

What i have is four comboboxes and two files. If the column matches the combobox i need to write it out to a file but it has to appened with the second combobox. So for example

Combobox1: Apple | Orange
Combobox2: Pineapple | Plum

I have selected Apple Plum

I need to search threw a text file and find whatever columns is Apple or Plum:

Orange|Pear|Peach|Turnip|Monkey|Apple|Grape|Plum and then i need to write out just the columns Apple|Plum to a new text file. Any help would be awesome!

Better example Combobox1 selected item:Apple Combobox2 selected item:Plum

Text FILE:
APPLE|Pear|Plum|Orange
1|2|3|4
215|3|45|98
125|498|76|4
4165|465|4|65

Resulting File:
1|3
215|45
125|76
4165|4

Thanks for the advice, i dont need help on adding to combobox or reading the files, just how to create a file from a delimited file having multiple columns.

Your answer involves a lot of steps that I can't answer without more information. But In genreal

Create a From
Add combo boxes to the form.
populate the combo boxes
add an event listener to the change event on the combo box
myCombo.Change+= new EventHandler(comboChanged)

Add code to do search based on the selected values of the change combo box.

Quick & dirty:

            string[] data = null;
            using (StreamReader sr = new StreamReader("data.txt"))
            {
                data = sr.ReadToEnd().Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            }
            if (data != null && data.Length > 0)
            {
                int colIndex1 = -1;
                int colIndex2 = -1;
                string[] line = data[0].Split(new char[] { '|' });
                for (int i = 0; i < line.Length; i++)
                {
                    if (String.Compare(line[i], comboBox1.Text, true) == 0)
                    {
                        colIndex1 = i;
                    }
                    if (String.Compare(line[i], comboBox2.Text, true) == 0)
                    {
                        colIndex2 = i;
                    }
                }
                using (StreamWriter sw = new StreamWriter("output.txt"))
                {
                    sw.WriteLine(comboBox1.Text + "|" + comboBox2.Text);
                    for (int i = 1; i < data.Length; i++)
                    {
                        line = data[i].Split(new char[] { '|' });
                        sw.WriteLine(line[colIndex1] + "|" + line[colIndex2]);
                    }
                }
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM