开发者

C# pulling out columns from combobox text

开发者 https://www.devze.com 2022-12-30 06:47 出处:网络
What i have is four comboboxes and two files. If the column matches the combobox i need to wr开发者_开发问答ite it out to a file but it has to appened with the second combobox.So for example

What i have is four comboboxes and two files. If the column matches the combobox i need to wr开发者_开发问答ite 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.


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]);
                    }
                }
            }


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消