Return values from a form

Shaifali jain 420 Reputation points
2023-09-22T09:26:05.5166667+00:00

Hi ,

Which is the best technique to return values from a form and use that in another form . like using a variable or some other way . for example , i have a form with a textbox which returns a value . i want to use that value in the textbox of other form . can you please share how to do it in the an efficient manner .

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,884 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,822 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Jiale Xue - MSFT 44,231 Reputation points Microsoft Vendor
    2023-09-25T02:05:23.1033333+00:00

    Hi @Shaifali jain , Welcome to Microsoft Q&A,

    There are indeed multiple ways to get the value in a subform,

    For example, use public properties or use delegates.

    Here's how to use public properties:

    Form1

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 form2 = new Form2();
        form2.ShowDialog();
        MessageBox.Show("The return value is: " + form2.returnString);
    }
    

    Form2

    public partial class Form2 : Form
    {
        public string returnString;
        public Form2()
        {
            InitializeComponent();
    
        }
    
        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            returnString = textBox1.Text;
        }
    }
    

    enter image description here

    Here's how to use delegation: Add delegates and events to child windows. Set up the child window to call the above event and pass the string that needs to be passed to the event to the event. Add a method to display the returned information in the main window, the same as participating in the MyAccountDelegate delegation. Call the event in the main window.

    Form1:

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 form2 = new Form2();
        form2.MyAccountEvent += new Form2.MyAccountDelegate(DisplayMyAccount);
        form2.ShowDialog();
        MessageBox.Show("The return value is: " + returnString);
    }
    private string returnString;
    private void DisplayMyAccount(string account)
    {
        returnString = account;
    }
    

    Form2:

    public partial class Form2 : Form
    {
        public delegate void MyAccountDelegate(string account);
        public event MyAccountDelegate MyAccountEvent;
        public Form2()
        {
            InitializeComponent();
        }
    
        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (textBox1.Text.Length > 0)
            {
                MyAccountEvent(textBox1.Text);//Add account information
            }
        }
    }
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

  2. Johan Smarius 470 Reputation points MVP
    2023-09-22T11:02:25.9133333+00:00

    You can expose the values you want to exchange as properties of the form. Once the form closes, you still have a reference to the form in the parent/calling form and you can get the values from the properties.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.