【问题标题】:c# How to make that selected item from listbox will be shown in label?c#如何使从列表框中选择的项目将显示在标签中?
【发布时间】:2015-08-19 21:02:52
【问题描述】:

所以我有一个问题,该代码在 Windows 窗体中的哪个位置放置,每次我在列表框中选择其他项目时都会刷新它? 我试图放入listBox1,但它使我的整个程序崩溃,我试图将它放入public Form2(),但它只显示label4中的第一个选定项目,并且在我选择listBox1中的其他项目后不刷新。有什么解决办法吗?

    var selected = listBox1.SelectedItem.ToString();
    label4.Text = selected;

完整代码如下

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;`

namespace VocaFlash_Japanese
{
    public partial class Form2 : Form
    {
        public List<string> words = new List<string>();
        public string Wor;
        public int State = 0;


        public Form2()
        {
            InitializeComponent();
            loadfile();
            listBox1.DataSource = words;
            //var selected = listBox1.SelectedItem.ToString();
          //  label4.Text = selected;

            hidetext();

        }
        private void hidetext()
        {
            textBox1.Hide();
            textBox2.Hide();
            textBox3.Hide();
            label1.Hide();
            label2.Hide();
            label3.Hide();
        }
        private void showtext()
        {
            textBox1.Show();
            textBox2.Show();
            textBox3.Show();
            label1.Show();
            label2.Show();
            label3.Show();
        }
        public void Save()
        {
            const string sPath = "Data.txt";
            System.IO.StreamWriter sw = new System.IO.StreamWriter(sPath);
            foreach (string item in words)
            {
                sw.WriteLine(item);
            }

            sw.Close();
        }
        private void UpdateListBox(List<string> words)
        {
            //string contents = null;
            List<string> itemAll = new List<string>();
            foreach (string str in words)
            {
                itemAll.Add(str);
            }
        }
        /*public void Save(List<string> words)
        {
            StringBuilder contents = new StringBuilder();
            foreach (string s in words)
            {
                contents.AppendLine(s);
            }
            System.IO.File.WriteAllText(@"Data.txt", contents.ToString());
        }*/
        private void loadfile()
        {
            string line;
            var file = new System.IO.StreamReader("Data.txt");
            while ((line = file.ReadLine()) != null)
            {
                words.Add(line);
            }
        }
       /* private void Remove()
        {
            string contents = null;
            List<string> itemAll = new List<string>();
            foreach (string str in words)
            {
                itemAll.Add(str);
            }
            foreach (string lstitem in listBox1.SelectedItems)
            {
                itemAll.Remove(lstitem);
                //File.AppendAllText(strFile + "Currently In.txt", strName + Environment.NewLine);
            }
            foreach (string s in itemAll)
            { contents += s + Environment.NewLine; }
            System.IO.File.WriteAllText(@"Data.txt", contents);

        }*/
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
           var selected = listBox1.SelectedItem.ToString();
            label4.Text = selected;
        }


        private void button4_Click(object sender, EventArgs e)
        {
            Meniu men = new Meniu();
            men.Show();
            this.Hide();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            State += 1;
            switch (State)
            {
                case 1:
                    showtext();
                    break;
                case 2:
                    words.Add(textBox1.Text);

                    listBox1.DataSource = null;
                    listBox1.DataSource = words;

                    Save();

                    State = 0;
                    textBox1.Text = "";

                    hidetext();
                    break;
                default:
                    Console.WriteLine("Default case");
                    break;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
           /* // The Remove button was clicked.
            int selectedIndex = listBox1.SelectedIndex;

            try
            {
                // Remove the item in the List.
                words.RemoveAt(selectedIndex);
            }
            catch
            {
            }

            listBox1.DataSource = null;
            listBox1.DataSource = words;
            Remove();*/
            // The Remove button was clicked.
            int selectedIndex = listBox1.SelectedIndex;

            try
            {
                // Remove the item in the List.
                words.RemoveAt(selectedIndex); //Remove item from words
                UpdateListBox(words);//Update contents on GUI
                Save(); //Save on IO
                listBox1.DataSource = null;
                listBox1.DataSource = words;

            }
            catch
            {
            }


        }

        private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
            Environment.Exit(0);
        }

        private void button3_Click(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {
        }
    }
}

【问题讨论】:

  • 挂钩一个更改事件到列表,然后在事件处理程序中设置文本。 Example on MSDN.
  • @DobisLT 你能解释一下将来哪个表单有 ListBox 控件,如果你能显示所有相关代码真的很有帮助,因为你无法确定你要更新哪个表单......如果它崩溃了你整个程序然后你需要了解如何实例化一个表单对象,这意味着如何创建一个Form2 的实例,例如请编辑问题并粘贴所有相关代码,这样其他人就不必猜测了
  • 只是好奇文件中的数据是什么样子的,我会用一行代码替换 while 循环,用1 代码行一次读取文件中的所有文本..@ 987654327@
  • 在隐藏或显示private void hidetext() 的方法中,我将创建一个利用Controls 类的方法并执行foreach(Control ctrl in Controls) then check it the type is TextBox`,然后在下一个条件检查中使可见为真检查是否控制是 Label 对 showtext 方法做同样的事情..

标签: c# winforms listbox


【解决方案1】:

您需要在设计器中双击 ListBox1 控件的事件选项卡,让它自动创建 SelectedIndexChanged 只需找到SelectedIndexChanged 并双击它

在事件里面放下面的代码

private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
    var selected = listBox1.GetItemText(listBox1.SelectedValue);
    label4.Text = selected; 
}

【讨论】:

  • 但是如果我添加这个代码,我的程序会崩溃并且不再能够添加新单词,并且蓝色标记卡住
  • 如果我想添加新项目就卡住了
  • 您需要显示与表单相关的完整代码以及您如何尝试根据更改的 selectedindex 分配标签,此代码不应导致您的程序崩溃,除非您尝试访问它从错误的形式。您还说您正在尝试添加新项目..但是在您发布的代码中没有任何地方表明您将项目添加到列表框控件..我们不介意读者..请显示所有相关代码也开始使用调试器不只是编码然后去..
  • 超级简单!但 'listBox1.SelectedValue' 为您提供数字索引值。使用 'listBox1.SelectedItem' 从选择中获取实际文本值。
猜你喜欢
  • 2010-11-14
  • 1970-01-01
  • 1970-01-01
  • 2013-09-21
  • 1970-01-01
  • 2019-05-10
  • 2019-05-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多