【问题标题】:Populate labels when selecting item in listbox在列表框中选择项目时填充标签
【发布时间】:2019-05-10 14:19:49
【问题描述】:

我有一个列表框,其中包含文本文件中每一行的第一个索引。 索引用“,”分隔。 我想在列表框中选择一个项目,并让它用文本文件中的其余行填充我已有的标签。

private void listsup_MouseClick(object sender, MouseEventArgs e)
    {
        Supfile = System.AppDomain.CurrentDomain.BaseDirectory + "data\\Suppliers.txt";
        StreamReader spl = new StreamReader(Supfile);
        string word = Convert.ToString(listsup.SelectedItem);
        List<string> values = new List<string>();
        foreach (string str in values)
        {
            if (str.Contains(word))
            {
                string[] tokens = str.Split(',');
                labelsupnm.Text = tokens[0];
                labelconpers.Text = tokens[1];
                labeldiscr1.Text = tokens[2];
                labeldiscr2.Text = tokens[3];
                labeldiscr3.Text = tokens[4];
                labeldiscr4.Text = tokens[5];
                labeldiscr5.Text = tokens[6];
            }
        }
    }

问题是,我的标签中没有显示任何内容,请帮助。

【问题讨论】:

    标签: listbox label populate selecteditem


    【解决方案1】:

    我稍微更改了代码,添加了一些用于填充列表框本身的代码,现在一切正常。

    private void listsup_MouseClick(object sender, MouseEventArgs e)
        {
            Supfile = System.AppDomain.CurrentDomain.BaseDirectory + "data\\Suppliers.txt";
            try
            {
                StreamReader supFile;
                supFile = File.OpenText(Supfile);
    
                string lines;
    
                while (!supFile.EndOfStream)
                {
                    lines = supFile.ReadLine();
                    string[] tokens = lines.Split(',');
                    string tr = listsup.SelectedItem.ToString();
                    if (tr.Equals(tokens[0]))
                    {
                        labelsupnm.Text = tokens[0];
                        labelconpers.Text = tokens[1];
                        labeldiscr1.Text = tokens[2];
                        labeldiscr2.Text = tokens[3];
                        labeldiscr3.Text = tokens[4];
                        labeldiscr4.Text = tokens[5];
                        labeldiscr5.Text = tokens[6];
                    }
    
                }
            }
    
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-06
      • 2013-01-10
      • 2016-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多