简体   繁体   English

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

[英]c# How to make that selected item from listbox will be shown in label?

So I have a question, where to put that code in Windows Forms that it would be refreshed every time I select other item in listbox? 因此,我有一个问题,每次我在列表框中选择其他项时,都会在刷新该代码的Windows窗体中放置该代码? I tried to put in listBox1, but it crash my whole program, I tried to put it in public Form2() but it show just first selected item in label4, and do not refresh after I select other item in listBox1. 我试图放入listBox1,但是它使我的整个程序崩溃,我试图将其放入public Form2()但它仅显示label4中的第一个选定项,并且在listBox1中选择其他项后不刷新。 Any solutions? 有什么办法吗?

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

full code below 下面的完整代码

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)
        {
        }
    }
}

you need to double click in the designer on the events tab for the ListBox1 control to have it automatically create the SelectedIndexChanged just find that SelectedIndexChanged and double click it 您需要双击设计器中的ListBox1控件的事件选项卡,以使其自动创建SelectedIndexChanged只需找到SelectedIndexChanged并双击即可

inside the event put the following code 在事件内放以下代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM