如何调用listbox中某行的内容给label 和 button,同时是两边数值是同步的?c# winform

如何调用listbox中某行的内容给label 和 button,同时是两边数值是同步的?c# winform

问题描述:

listbox中调取某行(例如第三行)的内容给另一个窗体(非子窗体)的button 和label,同时修改了listbox那行内容,button和label同步改变?并且如果该值被删除了,button和label内容也被删除。

label form代码
public AddRenameWindow addrename { get; set; }
//connect with addrename form 's data

    public void AlterWindow_Load(object sender, EventArgs e)
    {
        //show  teams' name in addrenmae form on this form 
        label4.Text = AddRenameWindow.a[0];
        label5.Text = AddRenameWindow.a[1];
        label6.Text = AddRenameWindow.a[2];
        label7.Text = AddRenameWindow.a[3];
        label8.Text = AddRenameWindow.a[4];
        label9.Text = AddRenameWindow.a[5];
        label10.Text = AddRenameWindow.a[6];
        label11.Text = AddRenameWindow.a[7];
        label12.Text = AddRenameWindow.a[8];
        label13.Text = AddRenameWindow.a[9];
        label14.Text = AddRenameWindow.a[10];
        label15.Text = AddRenameWindow.a[11];
        label16.Text = AddRenameWindow.a[12];
        label17.Text = AddRenameWindow.a[13];
        label18.Text = AddRenameWindow.a[14];
        label19.Text = AddRenameWindow.a[15];
        label20.Text = AddRenameWindow.a[16];
        label21.Text = AddRenameWindow.a[17];
    }
listbox form代码
        public AlterWindow alter { get; set; }
         public static string[] a = new string[30];
        public void button4_Click(object sender, EventArgs e)
        {
            AlterWindow alter = new AlterWindow();

            for (int index = 0; index < listBox1.Items.Count; index++)
            {
                a[index] = Convert.ToString(listBox1.Items[index]);
                //MessageBox.Show(a[i].ToString());
            }
                this.Close();
            }
        }
自己用蠢办法做出来了,虽然蠢但是同步功能完美。
![图片说明](https://img-ask.csdn.net/upload/201906/26/1561481978_125478.png)![图片说明](https://img-ask.csdn.net/upload/201906/26/1561481990_664584.png)


``

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Q759361
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Button3_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem != null)
            {
                listBox1.Items.Remove(listBox1.SelectedItem);
                foreach (Form item in Application.OpenForms)
                {
                    if (item is InputBox) { item.Close(); break; }
                }
            }
        }

        private void Button2_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem == null)
                return;
            InputBox ib = Application.OpenForms.Cast<Form>().FirstOrDefault(x => x is InputBox) as InputBox;
            if (ib == null)
            {
                ib = new InputBox();
                ib.textBox1.TextChanged += TextBox1_TextChanged;
            }
            ib.Top = this.Top;
            ib.Left = this.Left + this.Width + 10;
            ib.textBox1.Text = listBox1.Text;
            if (!ib.Visible) ib.Show(this);
        }

        private void TextBox1_TextChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem != null)
                listBox1.Items[listBox1.SelectedIndex] = (sender as TextBox).Text;
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            int i = listBox1.Items.Add("");
            listBox1.SelectedIndex = i;
            button2.PerformClick();
        }
    }
}

图片说明