Set value to another form

后端 未结 4 1695
清歌不尽
清歌不尽 2021-01-21 12:50

I have to form Form1 and Form2

Form1 source

namespace WindowsFormsApplication1
{
    public partial class Form1 :          


        
相关标签:
4条回答
  • 2021-01-21 13:13

    Use Constructor method , here is example

    In form2

    public Form2(string strTextBox)
    {
     InitializeComponent(); 
     label1.Text=strTextBox;
    }
    

    In form1 click event

    private void label1_Click(object sender, System.EventArgs e)
    {
    Form2 frm=new Form2(textBox1.Text);
    frm.Show();
    }
    

    For more information , reference Passing Data Between Forms !

    0 讨论(0)
  • 2021-01-21 13:29
    public partial class Form1 : Form
    {
    public Form1() {
    InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e){
    Form2 frm = new Form2(this);
    frm.Show();
    }
    }
    public partial class Form2 : Form
    {
    Form _frm;
    public Form2(Form frm)
    {
    _frm = frm;
    InitializeComponent();
    }`enter code here`
    
    private void button1_Click(object sender, EventArgs e)
    {
    Form1 formVariable = (Form1)_frm;
    formVariable.textBox.Text = "Hello";
    }
    }
    
    0 讨论(0)
  • 2021-01-21 13:30

    You need to pass a reference to your Form1 instance to the Form2 instance.

    You can do that like this:

    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void label1_Click(object sender, EventArgs e)
            {
                Form2 frm = new Form2(this); // <---- Pass a reference to this form to Form2
                frm.SetBtn = "teste test";
                frm.Show();
            }
    
            public string setLb
            {
                set
                {
                    label1.Text = value;
                }
            }
        }
    }
    

    And you would need to change the Form2 implementation a little aswell:

    namespace WindowsFormsApplication1
    {
        public partial class Form2 : Form
        {
            private Form1 other;
    
            //Empty constructor for the designer
            public Form2()
            {
                InitializeComponent();
            }
    
            public Form2(Form1 other)
            {
                this.other = other;
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                other.setLb = "test test";
            }
    
            public string SetBtn
            {
                set
                {
                    button1.Text = value;
                }
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-21 13:32

    In Form2 you have to add a parameter for the Form1.

    i.e.

    // Form1
    ...
    private void button1_Click(object sender, EventArgs e)
    {
        Form2 frm = new Form2(this);
        frm.SetBtn = "teste test";
        frm.Show();
    }
    ...
    
    // Form2
    public Form2(Form frm1)
    {
        InitializeComponent();
        frm1.setLb = "test test";
    }
    
    0 讨论(0)
提交回复
热议问题