Returning Data From A Second Form
The other day I received a query about using second forms in Visual Basic. I found a link to some examples and passed it along but it left me unsettled. The teacher in me did not find those examples explanatory enough. Sample code along is ok as far as it goes but often beginners need more. In fact I tend to prefer more myself. So I set out this morning to create an example. While I was creating an example for Visual Basic I decided to create an example for C# as well because, well, why not? What this example does is to instantiate a second form which is a simple implementation of an input box. The input box includes a textbox for data entry, a cancel box (I keep wanting to label this “never mind”) and a Done button. It looks like this:
Exciting right? I believe that examples for beginners should be very basic. I’ll use this form for both code examples. The textbox is called txtInput. The buttons are btnCancel and btnDone which should all be self-explanatory.
If you want to load your own custom form in .Net the first thing you have to do is to create that form. I do that from the Project menu option and selecting Add Windows Form. Since I want to return data I need to create a property. This property (of course you can create several if you want) will allow the calling program to get data from the form. In this case I am using a simple string property like this:
' Create a string property using newTextValue to hold the data privately
Private newTextValue As String
Public Property newText() As String
Get
Return newTextValue
End Get
Set(ByVal value As String)
newTextValue = value
End Set
End Property
The C# version might look something like this:
// Create a string property using newTextValue to hold the data privately
private string newTextValue;
public string newText
{
get { return newTextValue; }
set { newTextValue = value; }
}
Now to use our form we have to declare an instance of it and ask it to show itself. Let’s say it our form/class is called MyInput.
' Declare a new object of type MyInput which is a form
Dim testCase As New MyInput
' Declare a string variable to save the return from the form
Dim MyData As String
' Show the second form as a dialoge box
testCase.ShowDialog()
' Get the information from the form we just called
MyData = testCase.newText
' Display what was returned
MsgBox(MyData)
Why do we have it display as a dialog box? That is to make sure that our main program don’t go off and keep doing things before we are done with the second form. Our code in C# might look like this BTW:
// Declare a new object of type MyInput which is a form
MyInput testCase = new MyInput();
// Declare a string variable to save the return from the form
string MyData = null;
// Show the second form as a dialoge box
testCase.ShowDialog();
// Get the information from the form we just called
MyData = testCase.newText;
// Display what was returned
MessageBox.Show(MyData);
I hope the comments are detailed enough. That is more comments than I might put in a real program but I think having them here helps explain what is going on a little better. One last thing, if you find this example more useful than what ever else you may have found please think about linking to it so that more people can find it. Thanks.