| | |
Multi column combo box
Please support our VB.NET advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2007
Posts: 4
Reputation:
Solved Threads: 0
Hello there: This is my issue. I do have one combo box in a form from which I want to select a person, by selecting the ID. In the combo box I want to show sorted name, last name, and date of birth. I wonder this should not be so difficult, but I have no idea about how to do so. Could please anybody out there give me a hand!?
Thanks
Luisofo
Thanks
Luisofo
•
•
Join Date: Feb 2007
Posts: 4
Reputation:
Solved Threads: 0
I think I probably was not clear enough. I want to have in the combo box 4 columns: Name, Last name, Born date, and the ID of the person. But normally combo boxes only allow you 2 columns, one for the value to show and other for the value to store. I want to find the way to show all the 4 columns and selecting the one that I do need.
Incorrect.
Combo box = one column
http://www.startvbdotnet.com/controls/combo.aspx
You prolly mean, listView or DataGridView. Can you post a picture to better explain what you mean?
Combo box = one column
http://www.startvbdotnet.com/controls/combo.aspx
You prolly mean, listView or DataGridView. Can you post a picture to better explain what you mean?
Last edited by iamthwee; Feb 27th, 2007 at 2:20 pm.
*Voted best profile in the world*
I'm not sure I understand exactly what you're doing, but I think I have an idea.
What would probably be best for you to use would be a DataGrid. ESPECIALLY if the data you're populating the fields with is db driven. It would be relatively easy to set up as well.
The article I posted below describes EXACTLY what you need to do to setup a datagrid.
I hope this was helpful.
Good Luck.
http://www.devcity.net/Articles/215/1/article.aspx
What would probably be best for you to use would be a DataGrid. ESPECIALLY if the data you're populating the fields with is db driven. It would be relatively easy to set up as well.
The article I posted below describes EXACTLY what you need to do to setup a datagrid.
I hope this was helpful.
Good Luck.
http://www.devcity.net/Articles/215/1/article.aspx
•
•
Join Date: Apr 2008
Posts: 1
Reputation:
Solved Threads: 0
•
•
•
•
Hello there: This is my issue. I do have one combo box in a form from which I want to select a person, by selecting the ID. In the combo box I want to show sorted name, last name, and date of birth. I wonder this should not be so difficult, but I have no idea about how to do so. Could please anybody out there give me a hand!?
Thanks
Luisofo
-----------------------
I have the same problem. Have you found the solution? Hope you have.
•
•
Join Date: Apr 2008
Posts: 3
Reputation:
Solved Threads: 0
class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none">'Courier New'">Imports System.Drawing.Drawing2D
Imports System.Windows
Public Class MultiColumnCombo
Inherits System.Windows.Forms.ComboBox
Private _ColumnWidths As String = "100"
Private _ColumnWidthsArray As String()
Private _DoNotReact As Boolean = False
Private _Textchanged As Boolean = False
Sub New()
MyBase.New()
MyBase.DrawMode = DrawMode.OwnerDrawFixed
'SetStyle(ControlStyles.UserPaint, True)
'SetStyle(ControlStyles.AllPaintingInWmPaint, True)
'SetStyle(ControlStyles.DoubleBuffer, True)
End Sub
Public Property ColumnWidths() As String
Get
Return _ColumnWidths
End Get
Set(ByVal Value As String)
_ColumnWidths = Value
_ColumnWidthsArray = Value.Split(CType(";", Char))
Dim w As Integer = 0
For Each str As String In _ColumnWidthsArray
w += CInt(str)
Next str
MyBase.DropDownWidth = w + 10
End Set
End Property
Protected Overrides Sub OnDrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs)
' Set the DrawMode property to draw fixed sized items.
'
'If _Textchanged Then
' _Textchanged = False
' Exit Sub
'End If
Dim myBrush As Brush
'If e.Bounds.Y = 0 Then
' ItemHeight = 32
'Else
' ItemHeight = 16
'End If
' e.DrawBackground()
Dim LightColor As Color = Color.Blue
Dim DarkColor As Color = Color.Black
Dim GradBrush As Brush = New LinearGradientBrush(e.Bounds, LightColor, DarkColor, LinearGradientMode.Vertical)
Select Case CInt((e.State And DrawItemState.Selected))
'Case DrawItemState.Selected + DrawItemState.NoAccelerator + DrawItemState.NoFocusRect, DrawItemState.Selected
Case DrawItemState.Selected
myBrush = New SolidBrush(BackColor)
e.Graphics.FillRectangle(GradBrush, e.Bounds)
Case Else
myBrush = New SolidBrush(ForeColor)
'myBrush = GradBrush
e.Graphics.FillRectangle(New SolidBrush(BackColor), e.Bounds)
End Select
Dim str As String
' Draw the current item text based on the current Font and the custom brush settings.
Dim row As DataRowView = (CType(MyBase.Items(e.Index), DataRowView))
Dim newpos As Integer = e.Bounds.X
Dim endpos As Integer = e.Bounds.X
For indx As Integer = 0 To UBound(_ColumnWidthsArray)
Dim ColLength As Integer = CType(_ColumnWidthsArray(indx), Integer)
endpos += ColLength
Dim Charaant As Integer = CInt(Math.Round(CDbl(ColLength) / 6.2))
Dim rawitem As String = row.Item(indx).ToString()
If ColLength <> 0 Then
If Charaant > rawitem.Length Then
str = rawitem
Else
str = rawitem.Substring(0, Charaant)
End If
Dim r As RectangleF = New RectangleF(newpos + 2, e.Bounds.Y, endpos - 1, e.Bounds.Height)
'e.Graphics.FillRectangle(myBrush, r)
e.Graphics.DrawString(str, e.Font, myBrush, r)
'ControlPaint.DrawBorder3D(e.Graphics, New Rectangle(endpos + 2, e.Bounds.Y, newpos, e.Bounds.Height))
'e.Graphics.DrawString("mycol", e.Font, myBrush, New RectangleF(newpos, 0, CInt(_ColumnWidthsArray(indx)), ItemHeight))
If indx <= UBound(_ColumnWidthsArray) Then
e.Graphics.DrawLine(New Pen(Color.Black), endpos, e.Bounds.Y, endpos, Me.ItemHeight * Me.MaxDropDownItems)
e.Graphics.DrawLine(New Pen(Color.LightGray), endpos + 1, e.Bounds.Y, endpos + 1, Me.ItemHeight * Me.MaxDropDownItems)
End If
End If
newpos = endpos
Next
' e.DrawFocusRectangle()
myBrush.Dispose()
GradBrush.Dispose()
End Sub
Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
Dim foundIndex As Integer, textlngth As Integer
If _DoNotReact = False Then
If Me.AccessibilityObject.Value Is Nothing Then
textlngth = 0
Exit Sub
Else
textlngth = Me.AccessibilityObject.Value.Length
End If
If textlngth = 0 Then
MyBase.OnTextChanged(e)
_DoNotReact = False
_Textchanged = False
Exit Sub
End If
If textlngth <> 0 Then
_DoNotReact = True
foundIndex = FindString(Me.AccessibilityObject.Value)
If foundIndex > -1 Then
Me.SelectedIndex = foundIndex
Me.Select(textlngth, Me.Text.Length - textlngth)
'Me.SelectionStart = textlngth
_Textchanged = True
Else
_Textchanged = False
End If
_DoNotReact = False
End If
MyBase.OnTextChanged(e)
End If
_DoNotReact = False
End Sub
Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)
Select Case e.KeyCode
Case Keys.Back, Keys.Enter, Keys.Delete
_DoNotReact = True
Case Keys.Down
Me.DroppedDown = True
Invalidate()
End Select
MyBase.OnKeyDown(e)
End Sub
End Class
Imports System.Windows
Public Class MultiColumnCombo
Inherits System.Windows.Forms.ComboBox
Private _ColumnWidths As String = "100"
Private _ColumnWidthsArray As String()
Private _DoNotReact As Boolean = False
Private _Textchanged As Boolean = False
Sub New()
MyBase.New()
MyBase.DrawMode = DrawMode.OwnerDrawFixed
'SetStyle(ControlStyles.UserPaint, True)
'SetStyle(ControlStyles.AllPaintingInWmPaint, True)
'SetStyle(ControlStyles.DoubleBuffer, True)
End Sub
Public Property ColumnWidths() As String
Get
Return _ColumnWidths
End Get
Set(ByVal Value As String)
_ColumnWidths = Value
_ColumnWidthsArray = Value.Split(CType(";", Char))
Dim w As Integer = 0
For Each str As String In _ColumnWidthsArray
w += CInt(str)
Next str
MyBase.DropDownWidth = w + 10
End Set
End Property
Protected Overrides Sub OnDrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs)
' Set the DrawMode property to draw fixed sized items.
'
'If _Textchanged Then
' _Textchanged = False
' Exit Sub
'End If
Dim myBrush As Brush
'If e.Bounds.Y = 0 Then
' ItemHeight = 32
'Else
' ItemHeight = 16
'End If
' e.DrawBackground()
Dim LightColor As Color = Color.Blue
Dim DarkColor As Color = Color.Black
Dim GradBrush As Brush = New LinearGradientBrush(e.Bounds, LightColor, DarkColor, LinearGradientMode.Vertical)
Select Case CInt((e.State And DrawItemState.Selected))
'Case DrawItemState.Selected + DrawItemState.NoAccelerator + DrawItemState.NoFocusRect, DrawItemState.Selected
Case DrawItemState.Selected
myBrush = New SolidBrush(BackColor)
e.Graphics.FillRectangle(GradBrush, e.Bounds)
Case Else
myBrush = New SolidBrush(ForeColor)
'myBrush = GradBrush
e.Graphics.FillRectangle(New SolidBrush(BackColor), e.Bounds)
End Select
Dim str As String
' Draw the current item text based on the current Font and the custom brush settings.
Dim row As DataRowView = (CType(MyBase.Items(e.Index), DataRowView))
Dim newpos As Integer = e.Bounds.X
Dim endpos As Integer = e.Bounds.X
For indx As Integer = 0 To UBound(_ColumnWidthsArray)
Dim ColLength As Integer = CType(_ColumnWidthsArray(indx), Integer)
endpos += ColLength
Dim Charaant As Integer = CInt(Math.Round(CDbl(ColLength) / 6.2))
Dim rawitem As String = row.Item(indx).ToString()
If ColLength <> 0 Then
If Charaant > rawitem.Length Then
str = rawitem
Else
str = rawitem.Substring(0, Charaant)
End If
Dim r As RectangleF = New RectangleF(newpos + 2, e.Bounds.Y, endpos - 1, e.Bounds.Height)
'e.Graphics.FillRectangle(myBrush, r)
e.Graphics.DrawString(str, e.Font, myBrush, r)
'ControlPaint.DrawBorder3D(e.Graphics, New Rectangle(endpos + 2, e.Bounds.Y, newpos, e.Bounds.Height))
'e.Graphics.DrawString("mycol", e.Font, myBrush, New RectangleF(newpos, 0, CInt(_ColumnWidthsArray(indx)), ItemHeight))
If indx <= UBound(_ColumnWidthsArray) Then
e.Graphics.DrawLine(New Pen(Color.Black), endpos, e.Bounds.Y, endpos, Me.ItemHeight * Me.MaxDropDownItems)
e.Graphics.DrawLine(New Pen(Color.LightGray), endpos + 1, e.Bounds.Y, endpos + 1, Me.ItemHeight * Me.MaxDropDownItems)
End If
End If
newpos = endpos
Next
' e.DrawFocusRectangle()
myBrush.Dispose()
GradBrush.Dispose()
End Sub
Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
Dim foundIndex As Integer, textlngth As Integer
If _DoNotReact = False Then
If Me.AccessibilityObject.Value Is Nothing Then
textlngth = 0
Exit Sub
Else
textlngth = Me.AccessibilityObject.Value.Length
End If
If textlngth = 0 Then
MyBase.OnTextChanged(e)
_DoNotReact = False
_Textchanged = False
Exit Sub
End If
If textlngth <> 0 Then
_DoNotReact = True
foundIndex = FindString(Me.AccessibilityObject.Value)
If foundIndex > -1 Then
Me.SelectedIndex = foundIndex
Me.Select(textlngth, Me.Text.Length - textlngth)
'Me.SelectionStart = textlngth
_Textchanged = True
Else
_Textchanged = False
End If
_DoNotReact = False
End If
MyBase.OnTextChanged(e)
End If
_DoNotReact = False
End Sub
Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)
Select Case e.KeyCode
Case Keys.Back, Keys.Enter, Keys.Delete
_DoNotReact = True
Case Keys.Down
Me.DroppedDown = True
Invalidate()
End Select
MyBase.OnKeyDown(e)
End Sub
End Class
![]() |
Similar Threads
- Multi column combobox (Visual Basic 4 / 5 / 6)
Other Threads in the VB.NET Forum
- Previous Thread: A Very very Simple Program
- Next Thread: set TextBoxes equal to zero
| Thread Tools | Search this Thread |
"crystal .net .net2005 30minutes 2005 2008 access account arithmetic array assignment basic binary bing button buttons center check code combobox component connectionstring convert crystalreport data database databasesearch datagrid datagridview design dissertation dissertations dissertationthesis dosconsolevb.net dropdownlist excel file-dialog firewall folder ftp google hardcopy image images insert isnumericfuntioncall listview login math memory mobile ms navigate net networking opacity output passingparameters peertopeervideostreaming picturebox picturebox1 port print problemwithinstallation project reports" save savedialog searchbox serial soap sorting string table tcp temp text textbox timer toolbox trim update updown upload useraccounts usercontrol vb vb.net vb.netcode vb.netformclosing()eventpictureboxmessagebox vb.nettoolboxvisualbasic2008sidebar vb2008 vbnet view visual visualbasic visualbasic.net visualstudio web wpf






