You can use Custom Properties and Enums in Window Forms to enforce standards, speed coding, and make your code more readable. This is the second article in a series of articles on tips for Rapid Application Development in Visual Studio .NET, whether you are developing in VB.NET or C#. See my first article on Visual Inheritance.
Windows Forms are created with certain base properties. In .NET (C# and VB.NET), you can add your own properties to your forms. Using Attributes can cause them to be visible in the Forms Properties box, just like any intrinsic property. In this article, I will show three custom properties that you might want to add to your forms to standardize event handling functionality in your forms. The examples that I am using for this article will address three problems that I encounter in almost every Windows Form that has any appreciable logic in it. I will create the properties and then discuss the reason for them and their usage.
First, I will create three Enums to provide Intellisense, which makes your code much more readable. By the way, you can place the Enums above the Class definition line and save yourself one level of Intellisense.
| Public Enum State Loading Loaded Closing End Enum Public Enum Modified Clean Dirty End Enum Public Enum HandleEvents [Default] Busy Noise End Enum |
| Private _FormState As State Private _FormChanged As Modified Private _FormEventHandling As HandleEvents |
| <Browsable(True), _ Description("Tells the Load State of the form")> _ Public Property FormState() As State Get Return _FormState End Get Set(ByVal Value As State) _FormState = Value End Set End Property <Browsable(True), _ Description("Tells if control contents have been modified")> _ Public Property FormChanged() As Modified Get Return _FormChanged End Get Set(ByVal Value As Modified) _FormChanged = Value End Set End Property <Browsable(True), _ Description("Tells whether to ignore or process events")> _ Public Property FormEventHandling() As HandleEvents Get Return _FormEventHandling End Get Set(ByVal Value As HandleEvents) _FormEventHandling = Value End Set End Property |
| Private formLoading As Boolean |
| Private Sub frmDialogTemplate_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles MyBase.Load formLoading = True End Sub |
| Private Sub frmDialogTemplate_Activated(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles MyBase.Activated If formLoading Then ' this code will only execute once ' perform initialization code here, ' connect to database, load grids, etc. ' then turn off formLoading so we dont execute again formLoading = False End If End Sub |
| Private Sub frmDialogTemplate_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles MyBase.Load FormState = State.Loading End Sub Private Sub frmDialogTemplate_Activated(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles MyBase.Activated If FormState = State.Loading Then ' this code will only execute once ' perform initialization code here, ' connect to database, load grids, etc. ' then turn off formLoading so we dont execute again FormState = State.Loaded End If End Sub |

| Private Sub TextBox1_TextChanged(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles TextBox1.TextChanged FormChanged = Modified.Dirty End Sub |
| Private Sub frmMyNewDialog_Closing(ByVal sender As Object, _ ByVal e As System.ComponentModel.CancelEventArgs) _ Handles MyBase.Closing If FormChanged = Modified.Dirty Then ' implement save code or prompting here End If End Sub |
| Private Sub TextBox1_TextChanged(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles TextBox1.TextChanged FormChanged = Modified.Dirty ' if the Noise flag is set, ignore the interrupt If FormEventHandling = HandleEvents.Noise Then Exit Sub ' process the event here... End Sub |
| ' I am going to change the text of TextBox1, which ' will cause the change event to fire, so tell the ' handler to ignore the noise while I change the text FormEventHandling = HandleEvents.Noise TextBox1.Text = "New Text" FormEventHandling = HandleEvents.Default |