【问题标题】:Populating a listbox with a text file first index of every line in the file使用文件中每一行的文本文件第一个索引填充列表框
【发布时间】:2018-12-06 17:28:51
【问题描述】:
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++;
                }
            }
        }
    }

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

【问题讨论】:

    标签: listbox text-files line display items


    【解决方案1】:

    这行得通:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-23
      • 2023-03-20
      • 2022-10-07
      • 2014-12-13
      • 1970-01-01
      • 2013-11-01
      • 2011-12-17
      • 1970-01-01
      相关资源
      最近更新 更多