用文本文件的第一行索引填充文本文件的列表框

时间:2018-12-06 17:28:52

标签: listbox text-files line display items

private void customersToolStripMenuItem1_Click(object sender, EventArgs e)
    {
        custList.Text = "";
        FormControl();
        CustlistGB.Visible = true;
        CustlistGB.BringToFront();
        Optionchc.Visible = false;
        mainFormHandler.Size = new System.Drawing.Size(281, 367);
        custList.Text = "";

        fileloc = System.AppDomain.CurrentDomain.BaseDirectory + @"data\" + Todaysdate + @"\Customers.txt";
        if (!Directory.Exists(System.AppDomain.CurrentDomain.BaseDirectory + @"data\" + Todaysdate))
        {
            Directory.CreateDirectory(System.AppDomain.CurrentDomain.BaseDirectory + @"data\" + Todaysdate);
        }
        using (var stream = new StreamWriter(@"data\" + Todaysdate + @"\Customers.txt", append: true))
        {
            stream.Close();
            int counter = 0;
            StreamReader read = new StreamReader(fileloc.ToString());
            List<string> users = new List<string>(File.ReadAllLines(fileloc));
            while (!read.EndOfStream)
            {
                string line = read.ReadLine();
                string[] tokens = line.Split(new char[] { ',' }, StringSplitOptions.None);
                foreach (string sx in tokens)
                {
                    custList.Text = tokens[0];
                    counter++;
                }
            }
        }
    }

我的代码无法正常工作。我正在尝试获得第一个“令牌” 在每行中显示在我的列表框中,但没有出现。当我点击 应用运行时使用列表框,但是当我单击列表框时,它会在标签中显示第一行的信息。有任何想法吗?

1 个答案:

答案 0 :(得分:0)

这有效:

listsup.Items.Clear();
        Supfile = System.AppDomain.CurrentDomain.BaseDirectory + "data\\Suppliers.txt";
        List<string> proName = new List<string>();
        using (StreamReader rdr = new StreamReader(Supfile))
        {
            string line;
            while ((line = rdr.ReadLine()) != null)
            {
                string[] val = line.Split(',');
                listsup.Items.Add(val[0]);
            }
        }