Send values from one form to another form

后端 未结 19 2514
眼角桃花
眼角桃花 2020-11-21 11:45

I want to pass values between two Forms (c#). How can I do it?

I have two forms: Form1 and Form2.

Form1 contains one button. When I click on that button, Fo

相关标签:
19条回答
  • 2020-11-21 12:16

    In this code, you pass a text to Form2. Form2 shows that text in textBox1. User types new text into textBox1 and presses the submit button. Form1 grabs that text and shows it in a textbox on Form1.

    public class Form2 : Form
    {
        private string oldText;
    
        public Form2(string newText):this()
        {
            oldText = newText;
            btnSubmit.DialogResult = DialogResult.OK;
        }
    
        private void Form2_Load(object sender, EventArgs e)
        {
            textBox1.Text = oldText;
        }
    
        public string getText()
        {
            return textBox1.Text;
        }
    
        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                DialogResult = System.Windows.Forms.DialogResult.OK;
            }
        }
    }
    

    And this is Form1 code:

    public class Form1:Form
    {
        using (Form2 dialogForm = new Form2("old text to show in Form2"))
        {
            DialogResult dr = dialogForm.ShowDialog(this);
            if (dr == DialogResult.OK)
            {
                tbSubmittedText = dialogForm.getText();
            }
            dialogForm.Close();
        }
    }
    
    0 讨论(0)
  • 2020-11-21 12:16

    This is very simple. suppose you have 2 window form Form1 and Form2 and you want to send record of textbox1 from Form1 to Form2 and display this record in label1 of Form2; then in Form2 create a label which name is label1 and go to the property of label1 and set 'Modifiers'=public and in Form one create a textBox with id textBox1 and a button of name submit then write the following code on button click event

    button1_Click(object sender, EventArgs e)
    {
      Form2 obj=new Form2();
      obj.label1.text=textBox1.text.ToString();
      obj.show();
    }
    

    thats it... for this way you can bind dataset record to another form's datagridview......

    0 讨论(0)
  • 2020-11-21 12:17

    Ok so Form1 has a textbox, first of all you have to set this Form1 textbox to public in textbox property.

    Code Form1:

    Public button1_click()
    {
        Form2 secondForm = new Form2(this);
        secondForm.Show();
    }
    

    Pass Form1 as this in the constructor.

    Code Form2:

    Private Form1 _firstForm;
    
    Public Form2(Form1 firstForm)
    {
        _firstForm = firstForm:
    }
    
    Public button_click()
    {
        _firstForm.textBox.text=label1.text;
        This.Close();
    }
    
    0 讨论(0)
  • 2020-11-21 12:21

    There are several solutions to this but this is the pattern I tend to use.

    // Form 1
    // inside the button click event
    using(Form2 form2 = new Form2()) 
    {
        if(form2.ShowDialog() == DialogResult.OK) 
        {
            someControlOnForm1.Text = form2.TheValue;
        }
    }
    

    And...

    // Inside Form2
    // Create a public property to serve the value
    public string TheValue 
    {
        get { return someTextBoxOnForm2.Text; }
    }
    
    0 讨论(0)
  • 2020-11-21 12:21

    you can pass as parameter the textbox of the Form1, like this:

    On Form 1 buttom handler:

    private void button2_Click(object sender, EventArgs e)
    {
    Form2 newWindow = new Form2(textBoxForReturnValue);
    newWindow.Show(); 
    }
    

    On the Form 2

    public static TextBox textBox2; // class atribute
    
    public Form2(TextBox textBoxForReturnValue)
    {
        textBox2= textBoxForReturnValue;
    }
    
    private void btnClose_Click(object sender, EventArgs e)
    {
    
        textBox2.Text = dataGridView1.CurrentCell.Value.ToString().Trim();
        this.Close();
    }
    
    0 讨论(0)
  • 2020-11-21 12:21

    if you change Modifiers Property of a control in a Form to Public, another Forms can access to that control. f.e. :

        Form2 frm;
        private void Form1_Load(object sender, EventArgs e)
        {
            frm = new Form2();
            frm.Show();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(frm.txtUserName.Text); 
            //txtUserName is a TextBox with Modifiers=Public        
        }
    
    0 讨论(0)
提交回复
热议问题