Hi,
I've attempted to create a user control which uses comboBox and ListView to
get a multicolumn comboBox. Everything seemed fine except I hope to
remove/dispose the lv control when it is no longer in used or lost focus.
Can someone correct me or give me some advice on whether Im going the right
direction with the code below:
Imports System.Drawing.Drawing2D
Imports System.Windows
Imports System.Runtime.InteropServices
Imports System.Windows.Forms
Imports System.Reflection
Imports System.Threading
Public Enum wndMsg
WM_LBUTTONDBLCLK = &H203
WM_LBUTTONDOWN = &H201
End Enum
Public Class customComboBox
Inherits System.Windows.Forms.ComboBox
Private mDropDownEnabled As Boolean = True
Private WithEvents lv As customListView
Private WithEvents frm As Form
Public Sub New()
Me.Text = "customComboBox"
End Sub
Protected Overrides Sub OnLostFocus(ByVal e As System.EventArgs)
If not mDropDownEnabled Then
mDropDownEnabled = True
If Not lv Is Nothing Then
lv.Hide()
End If
End If
MyBase.OnLostFocus(e)
End Sub
Protected Overrides Sub OnClick(ByVal e As System.EventArgs)
If not mDropDownEnabled Then
mDropDownEnabled = True
frm.Controls.Remove(lv)
End If
MyBase.OnClick(e)
End Sub
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = wndMsg.WM_LBUTTONDBLCLK OrElse m.Msg =
wndMsg.WM_LBUTTONDOWN Then
If mDropDownEnabled = True Then
If Not Me.Focused Then
Me.Focus()
End If
mDropDownEnabled = False
frm = Me.FindForm
lv = New customListView
lv.Left = Me.Left
lv.Top = Me.Top + Me.Height
lv.Width = Me.Width
lv.Show()
frm.Controls.Add(lv)
Else
mDropDownEnabled = True
frm.Controls.Remove(lv)
End If
Return
End If
MyBase.WndProc(m)
End Sub
Private Sub frm_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles frm.Click
If Not mDropDownEnabled Then
mDropDownEnabled = True
frm.Controls.Remove(lv)
End If
End Sub
Private Sub lv_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles lv.Click
Me.Text = lv.SelectedItems(0).SubItems(0).Text
If Not lv Is Nothing Then
frm.Controls.Remove(lv)
End If
End Sub
End Class
Friend Class customListView
Inherits System.Windows.Forms.ListView
end Class
I've attempted to create a user control which uses comboBox and ListView to
get a multicolumn comboBox. Everything seemed fine except I hope to
remove/dispose the lv control when it is no longer in used or lost focus.
Can someone correct me or give me some advice on whether Im going the right
direction with the code below:
Imports System.Drawing.Drawing2D
Imports System.Windows
Imports System.Runtime.InteropServices
Imports System.Windows.Forms
Imports System.Reflection
Imports System.Threading
Public Enum wndMsg
WM_LBUTTONDBLCLK = &H203
WM_LBUTTONDOWN = &H201
End Enum
Public Class customComboBox
Inherits System.Windows.Forms.ComboBox
Private mDropDownEnabled As Boolean = True
Private WithEvents lv As customListView
Private WithEvents frm As Form
Public Sub New()
Me.Text = "customComboBox"
End Sub
Protected Overrides Sub OnLostFocus(ByVal e As System.EventArgs)
If not mDropDownEnabled Then
mDropDownEnabled = True
If Not lv Is Nothing Then
lv.Hide()
End If
End If
MyBase.OnLostFocus(e)
End Sub
Protected Overrides Sub OnClick(ByVal e As System.EventArgs)
If not mDropDownEnabled Then
mDropDownEnabled = True
frm.Controls.Remove(lv)
End If
MyBase.OnClick(e)
End Sub
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = wndMsg.WM_LBUTTONDBLCLK OrElse m.Msg =
wndMsg.WM_LBUTTONDOWN Then
If mDropDownEnabled = True Then
If Not Me.Focused Then
Me.Focus()
End If
mDropDownEnabled = False
frm = Me.FindForm
lv = New customListView
lv.Left = Me.Left
lv.Top = Me.Top + Me.Height
lv.Width = Me.Width
lv.Show()
frm.Controls.Add(lv)
Else
mDropDownEnabled = True
frm.Controls.Remove(lv)
End If
Return
End If
MyBase.WndProc(m)
End Sub
Private Sub frm_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles frm.Click
If Not mDropDownEnabled Then
mDropDownEnabled = True
frm.Controls.Remove(lv)
End If
End Sub
Private Sub lv_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles lv.Click
Me.Text = lv.SelectedItems(0).SubItems(0).Text
If Not lv Is Nothing Then
frm.Controls.Remove(lv)
End If
End Sub
End Class
Friend Class customListView
Inherits System.Windows.Forms.ListView
end Class