添加到列表框拆分字符串列

时间:2016-03-11 19:56:51

标签: c# split listbox

我有一个包含产品ID名称数量价格日期的列表框

我需要在其他列表框中添加Id Name Quantity Price。 我不知道怎么做,我已经尝试了很多。在此之前,我需要检索只有日期,我这样做

char[] cc = new char[500]; int i, nr;
string[] lines = new string[250];

public void btnRead_Click(object sender, EventArgs e)
{
        string FileName = textBox1.Text;
        FileStream r_stream = new FileStream(FileName, FileMode.Open, FileAccess.ReadWrite);
        StreamReader reads_string_from_r_stream = new StreamReader(r_stream);

        i = 0;
        listBox1.Items.Clear();
        listBox2.Items.Clear();

        for (; ; i++)
        {
            textBox1.Text = reads_string_from_r_stream.ReadLine();
            lines[i] = textBox1.Text;
            listBox1.Items.Add(lines[i]);
            listBox2.Items.Add(lines[i]);

            if (reads_string_from_r_stream.EndOfStream.Equals(true)) goto nn;       
        }
nn:
        textBox2.Text = reads_string_from_r_stream.EndOfStream.ToString();
        r_stream.Close();

        nr = listBox1.Items.Count;
        textBox6.Text = nr.ToString();
}

private void SelectPath_Click(object sender, EventArgs e)
{
    OpenFileDialog openFile1 = new OpenFileDialog();
    openFile1.Filter = "Text Files|*.txt";
    if (openFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    textBox1.Text = openFile1.FileName;
}

public void button1_Click(object sender, EventArgs e)
{
    int n = Convert.ToInt16(textBox6.Text);
    ListB.Items.Clear();
    for (i = 0; i < n; i++)
    {
        ListB.Items.Add(lines[i]);
    }
}

private void btnDat_Click(object sender, EventArgs e)
{
    int n = Convert.ToInt16(textBox6.Text);
    for (i = 0; i < n; i++)
    {
        if(! listDate.Items.Contains(lines[i].Split('\t')[lines[i].Split('\t').Length - 1]))
        {
            listDate.Items.Add(lines[i].Split('\t')[lines[i].Split('\t').Length - 1]);
        }
    }
    listData.Items.RemoveAt(0);
}

private void btnDataToTb_Click(object sender, EventArgs e)
{
    if (listData.SelectedItems.Count > 0)
    {
        string data = listData.SelectedItem.ToString();
        string[] date = data.Split('-');
        int day = Convert.ToInt32(date[0]);
        int month = Convert.ToInt32(date[1]);
        int year = Convert.ToInt32(date[2]);

        DateTime a = new DateTime(year, month, day);

        tbData.Text = a.ToString("dd.MM.yy");


        for (i = 0; i < listBox2.Items.Count; i++)
        {
            // listBox3.Items.Add(lines[i].Length - lines[i].Split('\t')[lines[i].Split('\t').Length - 1);
            //listBox3.Items.Add(lines[i].Split('\t')[lines[i].Split('\t').Length - lines[i].Split('\t').Length - 1]);
            // listBox3.Items.Add(lines[i]..Substring(0, lines[i].Split('\t').Length -1 ));
            //   listBox3.Items.Add(lines[i].Split('\t')[lines[i].Substring]);]
            listBox3.Items.Add(lines[i].Split('\t')[lines[i].Split('\t').Length - 1]);

        }
    }
    else
    {
        MessageBox.Show("Select a date");
    }
}

1 个答案:

答案 0 :(得分:0)

只要知道字段的索引,就可以创建一个简单的辅助方法,如下所示:

static string GetParts(string source, Func<int, bool> columnSelector)
{
    return string.Join("\t", source.Split('\t').Where((s, i) => columnSelector(i)));
}

因此,如果您希望您的字段为

Field:    ProductId Name Quantity Price Date
Column:           0    1        2     3    4

获取您将使用的Date

var date = GetParts(lines[i], c => c == 4);

并获得ID Name Quantity Price

var other = GetParts(lines[i], c => c < 4);