Flexible Custom Control ?

General Fear

Member
Joined
Dec 22, 2012
Messages
12
Programming Experience
10+
I am using VB.Net 2010.

At our firm we have several textboxes. We have the typical white background with black letters. Then if it is a mandatory textbox, it is a yellow background with black letters. Then there is letters only textbox, followed by numbers only, then there are combinations of the above like mandatory letters only textbox and so on and so on . . .

Instead of creating a textbox for each occasion, I was wondering if I can have one textbox. I want to pass a parameter to the textbox. Then I would like to have a case statement inside that basically says, case 0 basic textbox case 1 letters only textbox case 2 numbers only etc etc . . .

Can I do this with custom controls? If so, how can I implement this. Or is there a better way?
 
Hi,

Yes this can easily be done by creating one custom TextBox and adding a property to define the type of behaviour you want the TextBox to emulate. Here is a quick example for you. I have created an Enumeration to define the type of TextBox to be used. This can then be changed within the properties tab in the IDE or you can just as easily change this in code as you need. All you need to do is expand upon the code to add all the behaviour that you want for your different scenarios.

VB.NET:
Imports System.ComponentModel
 
Public Class MultiFunctionTextBox
  Inherits TextBox
 
  Public Enum TextBoxType
    NormalTextBoxBehaviour
    MandatoryTextBox
    LettersOnlyTextBox
    NumbersOnlyTextBox
  End Enum
  Private _TextBoxBehaviour As TextBoxType
 
  <Category("Behavior")> <Description("Set the Behaviour of the TextBox Control.")> _
  Public Property TextBoxBehaviour As TextBoxType
    Get
      Return _TextBoxBehaviour
    End Get
    Set(value As TextBoxType)
      _TextBoxBehaviour = value
      Select Case value
        Case TextBoxType.MandatoryTextBox
          MyBase.BackColor = Color.Yellow
        Case Else
          MyBase.BackColor = Color.White
      End Select
    End Set
  End Property
End Class

Hope that helps.

Cheers,

Ian
 
Back
Top