I'm very new to C# language, so please take me easy. What I want to ask is quite simple, but being new, I don't know how to do it.
I have 2 forms: Form1 and Form2. Form1 is the "default" one, the one you have when you open the application. I have 2 textboxes in the second form and two buttons (ok and cancel). In the first form I have a button which opens the 2nd form when you click on it and a textbox. I tried to get the text from those 2 textboxes in form 2 and to put it in the textbox from form1, but I didn't managed to do it. I want when I click ok in the second form, the text from those 2 textboxes in form 2 to be put in the textbox from form1 and when I click cancel, to just simply close form2. Can you help me?
If I understand your question:
- Form2 has 2 TextBoxes (
textBox1
andtextBox2
) and 2 buttons (btnOK
andbtnCancel
)- If
btnOK
is pressed - concatinate values oftextBox1
andtextBox2
and pass them toForm1
- If
btnCancel
is pressed - do not pass any dataBrief description of my answer:
It can be easily achieved with event handlers, just hook up to
OnFormClosing
event and read data from predefined property ofForm2
Some code to illustrate my answer is below
Form1.cs
Form2.cs
You could create a public property in Form2 that is set by Form1 when you press your button.
On the Form Load event, you could then, set the value of your text box to that of the property.
Example of Form 2