c#如何使列表框中的选定项显示在标签中?

c#如何使列表框中的选定项显示在标签中?,c#,winforms,listbox,C#,Winforms,Listbox,所以我有一个问题,在Windows窗体中的什么地方放置代码,每次我在列表框中选择其他项时,它都会被刷新? 我试图放入listBox1,但它使我的整个程序崩溃,我试图将它放入public Form2()但它只显示label4中的第一个选定项,并且在我选择listBox1中的其他项后不会刷新。有什么解决办法吗 var selected = listBox1.SelectedItem.ToString(); label4.Text = selected; 完整代码如下 using S

所以我有一个问题,在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)
        {
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用System.IO`
日语名称空间
{
公共部分类表单2:表单
{
公共列表词=新列表();
公共字符串Wor;
公共int状态=0;
公共表格2()
{
初始化组件();
loadfile();
listBox1.DataSource=单词;
//var selected=listBox1.SelectedItem.ToString();
//label4.Text=已选择;
hidetext();
}
私有void hidetext()
{
textBox1.Hide();
textBox2.Hide();
textBox3.Hide();
label1.Hide();
label2.Hide();
label3.Hide();
}
私有void showtext()
{
textBox1.Show();
textBox2.Show();
textBox3.Show();
label1.Show();
label2.Show();
label3.Show();
}
公共作废保存()
{
常量字符串sPath=“Data.txt”;
System.IO.StreamWriter sw=新的System.IO.StreamWriter(sPath);
foreach(单词中的字符串项)
{
软件写入线(项目);
}
sw.Close();
}
私有void UpdateListBox(列出单词)
{
//字符串内容=null;
List itemAll=新列表();
foreach(字串str)
{
itemAll.Add(str);
}
}
/*公共作废保存(列出单词)
{
StringBuilder内容=新建StringBuilder();
foreach(单词中的字符串s)
{
内容。附录行;
}
System.IO.File.WriteAllText(@“Data.txt”,contents.ToString());
}*/
私有void加载文件()
{
弦线;
var file=new System.IO.StreamReader(“Data.txt”);
而((line=file.ReadLine())!=null)
{
添加(行);
}
}
/*私有无效删除()
{
字符串内容=null;
List itemAll=新列表();
foreach(字串str)
{
itemAll.Add(str);
}
foreach(listBox1.SelectedItems中的字符串lstitem)
{
itemAll.移除(lstitem);
//File.AppendAllText(strFile+“当前在.txt中”,strName+Environment.NewLine);
}
foreach(itemAll中的字符串s)
{contents+=s+Environment.NewLine;}
System.IO.File.WriteAllText(@“Data.txt”,contents);
}*/
私有无效列表框1\u SelectedIndexChanged(对象发送方,事件参数e)
{
var selected=listBox1.SelectedItem.ToString();
label4.Text=已选择;
}
私有无效按钮4_单击(对象发送者,事件参数e)
{
梅纽曼=新梅纽曼();
男人。秀();
this.Hide();
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
状态+=1;
开关(状态)
{
案例1:
showtext();
打破
案例2:
words.Add(textBox1.Text);
listBox1.DataSource=null;
listBox1.DataSource=单词;
Save();
状态=0;
textBox1.Text=“”;
hidetext();
打破
违约:
Console.WriteLine(“默认情况”);
打破
}
}
私有无效按钮2\u单击(对象发送者,事件参数e)
{
/*//已单击“删除”按钮。
int selectedIndex=列表框1.selectedIndex;
尝试
{
//删除列表中的项目。
words.RemoveAt(selectedIndex);
}
抓住
{
}
listBox1.DataSource=null;
listBox1.DataSource=单词;
删除()*/
//已单击删除按钮。
int selectedIndex=列表框1.selectedIndex;
尝试
{
//删除列表中的项目。
words.RemoveAt(selectedIndex);//从words中删除项
UpdateListBox(words);//在GUI上更新内容
Save();//在IO上保存
listBox1.DataSource=null;
listBox1.DataSource=单词;
}
抓住
{
}
}
私有void Form2\u FormClosing(对象发送方,FormClosingEventArgs e)
{
环境。退出(0);
}
私有无效按钮3\u单击(对象发送者,事件参数e)
{
}
私有无效标签4_单击(对象发送方,事件参数e)
{
}
}
}

您需要双击ListBox1控件的事件选项卡上的设计器,使其自动创建
SelectedIndexChanged
只需找到
SelectedIndexChanged
,然后双击它

在事件内部放置以下代码

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

将更改事件挂接到列表,然后在事件处理程序中设置文本@多比斯特c