|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Services
Chapters
Feature Zones
|
IntroductionWhether you are new to development, or coming from Visual Basic, C++, Java, or Delphi, managing forms in a WinForms can be initially confusing. This is an article targeted at those who are new to WinForms to demonstrate how to easily manage and work with forms in a WinForms application. Non ModalA non modal form is a form that is opened, but allows other windows to be focused while the window is open. This allows a user to work with more than one form at a time. PatternThe basic pattern to open a non modal form is as follows: C#private void button1_Click(object sender, System.EventArgs e) {
Form2 xForm = new Form2();
xForm.Show();
}
Visual BasicPrivate Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim xForm As Form2
xForm = New Form2
xForm.Show()
End Sub
The DisposalWhen a non modal form is closed, the ModalModal, or dialog style forms prevent the user from removing the focus from the form. The user must either open another form, or close the form. Forms opened before the modal form cannot be accessed by the user. PatternC#private void button2_Click(object sender, System.EventArgs e) {
using (Form2 xForm = new Form2()) {
xForm.ShowDialog(this);
}
}
Visual BasicPrivate Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
Dim xForm As Form2
xForm = New Form2
Try
xForm.ShowDialog(Me)
Finally
xForm.Dispose()
End Try
End Sub
The DisposalWinForms does not automatically call the Returning a ResultThe To see this in action, drop two buttons on a modal form. Now set the The pattern can then be modified as shown: C#private void button2_Click(object sender, System.EventArgs e) {
using (Form2 xForm = new Form2()) {
if (xForm.ShowDialog(this) == DialogResult.OK) {
// Take some action
}
}
}
Visual BasicPrivate Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
Dim xForm As Form2
xForm = New Form2
Try
If xForm.ShowDialog(Me) = DialogResult.OK Then
' Take some action
End If
Finally
xForm.Dispose()
End Try
End Sub
Passing DataPassing data into a form can easily be done by creating public methods, members, or properties and accessing them before the form is shown. As an example, imagine that the following is declared in the second form: C#public void SetValue(string aValue) {
button1.Text = aValue;
// Do some other things
}
Visual BasicPublic Sub SetValue(ByVal aValue As String)
button1.Text = aValue
' Do something else
End Sub
This method can then be called from the calling code: C#private void button1_Click(object sender, System.EventArgs e) {
Form2 xForm = new Form2();
xForm.SetValue("Test");
xForm.Show();
}
Visual BasicPrivate Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim xForm As Form2
xForm = New Form2
xForm.SetValue("Test")
xForm.Show()
End Sub
This example is shown using the This same method can be used to retrieve data from a modal form, by calling methods, or accessing members or properties after the An example might be as follows: C#private void button2_Click(object sender, System.EventArgs e) {
using (Form2 xForm = new Form2()) {
if (xForm.ShowDialog(this) == DialogResult.OK) {
_Value = xForm.GetValue();
}
}
}
Visual BasicPrivate Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
Dim xForm As Form2
xForm = New Form2
Try
If xForm.ShowDialog(Me) = DialogResult.OK Then
_Value = xForm.GetValue()
End If
Finally
xForm.Dispose()
End Try
End Sub
where Static ShowIf a form is always shown and certain arguments are required, the process can be encapsulated into a C#public static string Display(string aValue) {
using (Form2 xForm = new Form2()) {
xForm.SetValue("Test");
if (xForm.ShowDialog() == DialogResult.OK) {
return GetValue();
}
else {
return "";
}
}
}
Visual BasicPublic Shared Function Display(ByVal aValue As String) As String
Dim xForm As Form2
xForm = New Form2
Try
xForm.SetValue(aValue)
If xForm.ShowDialog() = xForm.DialogResult.OK Then
Return GetValue()
Else
Return ""
End If
Finally
xForm.Dispose()
End Try
End Function
The To show the form, the calling code appears as follows: C#private void button1_Click(object sender, System.EventArgs e) {
_Value = Form2.Display("Test");
}
Visual BasicPrivate Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
_Value = Form2.Display("Test")
End Sub
This of course can be done without returning a result as well, by simply changing the method type to C#public static void Display(string aValue) {
Form2 xForm = new Form2();
xForm.SetValue("Test");
xForm.Show();
}
Visual BasicPublic Shared Sub Display(ByVal aValue As String) As String
Dim xForm As Form2
xForm = New Form2
xForm.SetValue(aValue)
xForm.Show()
End Sub
Keeping a referenceOften it is useful to store a reference to a form for later use. Your application may need to update the form from another event, re-show the form, hide the form, etc. .NET does not support Globals, which many of us have formerly used for this purpose. However In C#public static Form2 Form2Ref;
Visual BasicPublic Shared Form2Ref As Form2
This variable can then be used to store a reference to an instance of the form. To reference it again later, for example to re-show the form or refocus it, the following code can be used: C#Form2.Form2Ref.Show();
Visual BasicForm2.Form2Ref.Show()
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||