Imports System.ComponentModel
' Purpose: Display the value of a ComboBox without the dimmed appearance when disabled
' and without having to draw the ComboBox manually
' Control consists of a TexBox and a ComboBox
' TextBox and ComboBox are both set to the composite control width on loading
' TextBox is in front of the ComboBox
' TextBox.Text is set to the ComboBox.Text
' In Edit Mode the textBox is not visible allowing the ComboBox to be viewed and changed
' When NOT in Edit Mode the TextBox hides the ComboBox so it cannot be changed
' The ComboBox is always visible so its value will be changed when the binding source value changes
Public Class myComboTextBox
' Control Edit Mode
Private EditMode As Integer
' This Control Width
Private ThisControlWidth As Integer
' Original Combo Selected Index
Private OrgSelectedIndex As Integer = -1
' cbo Width Has Changed Flag
Private ComboWidthChanged As Boolean
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub
' Set Initial Control Settings
Private Sub myComboTextBox_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
ThisControlWidth = Me.Width
ComboWidthChanged = False
' Change Backcolor Due To ReadOnly BackColor Is Not White
txtBox.BackColor = myClrWhite
txtBox.Width = ThisControlWidth
cboBox.Width = ThisControlWidth
End Sub
' Set View/Edit Mode
Public Property PropComboTextCurrentMode() As Integer
Get
Return EditMode
End Get
Set(ByVal Value As Integer)
EditMode = Value
txtBox.Visible = CBool(IIf(EditMode = glbViewMode, True, False))
cboBox.Enabled = CBool(IIf(EditMode = glbViewMode, False, True))
End Set
End Property
' Set Combo SelectedIndex
Public Property PropSelectedIndex() As Integer
Get
Return cboBox.SelectedIndex
End Get
Set(ByVal Value As Integer)
cboBox.SelectedIndex = Value
End Set
End Property
' Force Combo to Uppercase & Increase Width To Make AutoComplete DropDown Wider
Private Sub cbo_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) Handles cboBox.KeyPress
If Not ComboWidthChanged = True Then
cboBox.Width = cboBox.Width + cboAddWidth
ComboWidthChanged = True
End If
If Char.IsLetter(e.KeyChar) Then
e.KeyChar = Char.ToUpper(e.KeyChar)
End If
End Sub
' Restore Combo Width
Private Sub cbo_LostLeave(ByVal sender As Object, ByVal e As EventArgs) Handles cboBox.LostFocus, cboBox.Leave, cboBox.MouseLeave
If ComboWidthChanged = True Then
cboBox.Width = ThisControlWidth
ComboWidthChanged = False
End If
End Sub
' Save Original Combo Selected Index
Private Sub cboBox_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboBox.GotFocus
OrgSelectedIndex = cboBox.SelectedIndex
End Sub
' Check Combo For Invalid Selection, Restore Original SelectedIndex
Private Sub cboSelection_Validating(ByVal sender As Object, ByVal e As CancelEventArgs) Handles cboBox.Validating
' Invalid Selection or No Selection Made
If cboBox.SelectedIndex = -1 Then
' Invalid Selection but Text is Present
If cboBox.Text.Trim.Length > 0 Then
MessageBox.Show("Invalid selection", _
"Attention", _
MessageBoxButtons.OK, _
MessageBoxIcon.Exclamation, _
MessageBoxDefaultButton.Button1)
e.Cancel = True
If OrgSelectedIndex = -1 Then
cboBox.Text = ""
Else
cboBox.SelectedIndex = OrgSelectedIndex
End If
End If
End If
End Sub
' Save Original Combo Selected Index
Private Sub cboBox_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboBox.Validated
OrgSelectedIndex = cboBox.SelectedIndex
End Sub
' Set Textbox Text To Combo Text
Private Sub cboBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboBox.SelectedIndexChanged
txtBox.Text = cboBox.Text
End Sub
End Class