Question User Control - Multicolumn Combobox

tslu

New member
Joined
Mar 13, 2009
Messages
4
Programming Experience
Beginner
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
 
There is no need to do this...
Friend Class customListView
Inherits System.Windows.Forms.ListView
end Class
...if you don't have any alterations/customizations to the class.

You could use a button that makes the lv appear/disappear and even increase it's height and width to make to appear to grow to a size you want an added effect. Making and deleting controls is a waste if it is not absolutely needed.

I am not sure exactly what you are wanting.
 
im trying to create a custom combobox that has a drop down with vertical multiple columns. The .net one only has a single column whereas the multicolumn scroll sideways
 
The easiest way to display column data in combobox is to use a fixed width font and composite formatting string with alignment, for example select Courier New font and see the effect of the code below, combobox need to be wide enough to display the 45 chars.
VB.NET:
Dim r As New Random, s As String = "sample data"
For i As Integer = 1 To 20
    Me.ComboBox1.Items.Add(String.Format("{0,-15}{0,-15}{0,-15}", s.Substring(0, r.Next(3, 11))))
Next
 
Back
Top