weixin_44097963
2019-04-30 03:10
采纳率: 66.7%
浏览 1.2k

C# 一个窗口的label如何读取另一个窗口中listbox里面某个值,并可以同步?

有两个窗体 form1 ,form2(平级关系,上面还有个总form),form2中label.text要读取form1中listbox中某一行(例如第三行)的值。而且当listbox第三行数值被修改(删除),label能自动同步修改值(删除)。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • weixin_44097963 2019-06-26 01:01
    已采纳

    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();
            }
        }
    

    自己用蠢办法做出来了,虽然蠢但是同步功能完美。
    图片说明
    图片说明

    ``

    打赏 评论
  • threenewbee 2019-04-30 10:58
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Windows.Forms;
    
    namespace Q759361
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                this.Show();
                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.Visible = true;
                }
    
            }
    
            [StructLayout(LayoutKind.Sequential)]
            public struct RECT
            {
                public int Left;        // x position of upper-left corner
                public int Top;         // y position of upper-left corner
                public int Right;       // x position of lower-right corner
                public int Bottom;      // y position of lower-right corner
            }
    
            [DllImport("user32.dll", CharSet = CharSet.Auto)]
            public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, out RECT lParam);
    
            public const int LB_GETITEMRECT = 408;
    
            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; }
                        if (item is InputBox) { (item as InputBox).label2.Text = ""; }
                    }
                }
            }
    
            //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();
            }
    
            private int modidx;
    
            private void ListBox1_DoubleClick(object sender, EventArgs e)
            {
                RECT rect;
                if (listBox1.SelectedItem != null)
                {
                    SendMessage(listBox1.Handle, LB_GETITEMRECT, listBox1.SelectedIndex, out rect);
                    textBox1.BorderStyle = BorderStyle.FixedSingle;
                    textBox1.Left = listBox1.Left + rect.Left + 2;
                    textBox1.Width = rect.Right - rect.Left - 4;
                    textBox1.Top = listBox1.Top + rect.Top + 2;
                    textBox1.Height = rect.Bottom - rect.Top - 4;
                    textBox1.Text = listBox1.SelectedItem.ToString();
                    textBox1.SelectAll();
                    textBox1.Visible = true;
                    textBox1.Focus();
                    modidx = listBox1.SelectedIndex;
                }
            }
    
            private void TextBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
                if (e.KeyChar == '\r')
                {
                    e.Handled = false;
                    listBox1.Items[modidx] = textBox1.Text;
                    textBox1.Visible = false;
                }
            }
    
            private void TextBox1_Validated(object sender, EventArgs e)
            {
                listBox1.Items[modidx] = textBox1.Text;
                textBox1.Visible = false;
            }
    
            private void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                InputBox ib = Application.OpenForms.Cast<Form>().FirstOrDefault(x => x is InputBox) as InputBox;
                if (ib != null)
                {
                    ib.label2.Text = listBox1.Text;
                }
            }
    
        }
    }
    
    
    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 InputBox : Form
        {
            public InputBox()
            {
                InitializeComponent();
            }
    
            private void Button1_Click(object sender, EventArgs e)
            {
                Close();
            }
    
            private void Label2_TextChanged(object sender, EventArgs e)
            {
                label2.Visible = (label2.Text != "");
                button1.Visible = label2.Visible;
            }
        }
    }
    
    

    图片说明

    打赏 评论
  • mURPHEY STUDIo 2019-04-30 11:17

    说下思路吧
    方法一:在form1中先查找获取当前的form2,然后在form1的listbox修改/删除事件中添加对该form2上的label的删改操作。
    方法二:在主窗口定义一个公共变量,form1修改,form2定时读取
    能想到的就这些了

    打赏 评论
  • hhhljlishen 2019-05-01 07:26

    定义一个事件,form1的listbox改变的时候触发该事件。form2定义一个函数修改label的值。主窗口负责将form1的事件,连接到form2的函数上

    打赏 评论
  • 飞鸟与青蛙 2019-05-05 12:45

    给你个思路,自定义事件(委托)去监听listbox的变化

    打赏 评论

相关推荐 更多相似问题