Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#从组合框文本中拉出列_C# - Fatal编程技术网

C#从组合框文本中拉出列

C#从组合框文本中拉出列,c#,C#,我有四个组合框和两个文件。如果列与组合框匹配,我需要将其写入一个文件,但它必须与第二个组合框一起出现。比如说 Combobox1:Apple| Orange 组合框2:菠萝李子 我选了苹果李子 我需要搜索一个文本文件,找到Apple或Plum的任何列: 橙|梨|桃|萝卜|猴|苹果|葡萄|李子 然后我只需要将Apple | Plum列写入一个新的文本文件。任何帮助都会很棒 更好的例子 Combobox1所选项目:苹果 Combobox2所选项目:梅花 文本文件: 苹果|梨|李|橙 1 | 2 |

我有四个组合框和两个文件。如果列与组合框匹配,我需要将其写入一个文件,但它必须与第二个组合框一起出现。比如说

Combobox1:Apple| Orange
组合框2:菠萝李子

我选了苹果李子

我需要搜索一个文本文件,找到Apple或Plum的任何列:

橙|梨|桃|萝卜|猴|苹果|葡萄|李子 然后我只需要将Apple | Plum列写入一个新的文本文件。任何帮助都会很棒

更好的例子 Combobox1所选项目:苹果 Combobox2所选项目:梅花

文本文件:
苹果|梨|李|橙
1 | 2 | 3 | 4
215 | 3 | 45 | 98
125 | 498 | 76 | 4
4165 | 465 | 4 | 65

结果文件:
1|3
215 | 45
125 | 76
4165 | 4


感谢您的建议,我不需要添加到combobox或读取文件的帮助,只需要如何从具有多列的分隔文件创建文件。

您的答案涉及很多步骤,如果没有更多信息,我无法回答。但在genreal


将组合框添加到表单中。
填充组合框
将事件侦听器添加到组合框上的更改事件
myCombo.Change+=新事件处理程序(comboChanged)

根据更改组合框的选定值添加代码进行搜索

快速和肮脏:

            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]);
                    }
                }
            }
string[]数据=null;
使用(StreamReader sr=newstreamreader(“data.txt”))
{
data=sr.ReadToEnd().Split(新字符串[]{“\r\n”},StringSplitOptions.RemoveEmptyEntries);
}
if(data!=null&&data.Length>0)
{
int colIndex1=-1;
int colIndex2=-1;
字符串[]行=数据[0]。拆分(新字符[]{'|'});
for(int i=0;i
tbh我不知道你的问题是关于什么的;你指的是哪个专栏?你需要找哪一列?这些都只是例子。列是用户选择的列。