【问题标题】:C# pulling out columns from combobox textC# 从组合框文本中提取列
【发布时间】:2010-05-10 17:15:07
【问题描述】:

我有四个组合框和两个文件。如果该列与组合框匹配,我需要将其写入文件,但必须附加第二个组合框。比如

Combobox1:苹果 |橙色
Combobox2:菠萝 |李子

我选择了苹果李子

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

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

更好的例子 Combobox1 选中项:Apple 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

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

【问题讨论】:

  • tbh 我不知道你的问题是什么;你指的是哪一栏?你需要找到哪一列?
  • 这些只是例子。列是用户选择的任何一个。

标签: c#


【解决方案1】:

又快又脏:

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

【讨论】:

    【解决方案2】:

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

    创建发件人
    向表单添加组合框。
    填充组合框
    为组合框上的更改事件添加事件侦听器
    myCombo.Change+= new EventHandler(comboChanged)

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

    【讨论】:

      猜你喜欢
      • 2016-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多