How can I reuse forms for multiple purposes so that I don't have so many forms that perform much of the same functionality and have many of the same controls?
Every project of any size has numerous dialog type forms, used for data entry. Many times, the forms use a mix of similiar controls with some new ones for the various functionalities required in the form.
I have started using an Enum to enumerate the various types of functionality that I want the form to perform, and then, based on the functionality required, the same form is reused and takes on a different look and feel. Not only does this reduce the overall number of forms that I need, but it makes the work of the IDE easier and faster. As you know, VB.NET is constantly doing a "background compile", which is made slower each time you add new classes and forms to your project.
This process can be greatly improved by keeping the number of windows that you have open at any one time to a minimum. I believe this will reduce the number of windows that the IDE is concerned with having to worry about compiling. This compile is done so that IntelliSense is always up to date. If you are working in VB.NET, you may have noticed, that as the project size increases, the performance of the IDE, with respect to intellisense, begins to degrade. Again, I believe that you can help this situation by doing two things; first, reuse classes and forms, by adding additional functionality to them, and keep the number of open windows to a minimum.
In this article, I will show you a simple technique that I have adopted for creating "visual polymorphism". That's a fifty cent term for making a form take on a different look and feel, based on a parameter that I set when I instantiate it.
First, in the form itself, I will set up an Enum and a usage or feature property that will tell the form how I want it to look and behave when it is shown. The code for that is shown below.
| Public Enum FormUsage SelectList MemoFieldUpdate End Enum Public UseType As FormUsage = FormUsage.SelectList |
| Dim f As New frmTest f.UseType = frmTest.FormUsage.SelectList f.ShowDialog() |

| f.UseType = frmTest.FormUsage.MemoFieldUpdate f.ShowDialog() |

| Private Sub frmTest_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles MyBase.Load ' Determine the look of the form at load time ' based on the selected usage. With Me Select Case UseType Case FormUsage.SelectList .grpBox1.Visible = True .grpBox2.Visible = False .txtCode.Visible = False .lstItems.Visible = True .lblText.Text = "Select Item" .Text = "Select Item" Case FormUsage.MemoFieldUpdate .grpBox1.Visible = False .grpBox2.Visible = True .txtCode.Visible = True .lstItems.Visible = False .lblText.Text = "Enter Text" .Text = "Enter Memo Data" End Select End With End Sub |