|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
Contents
IntroductionOnce I had to develop a program using VBA with Access, and I had the great pleasure to use the FeaturesHere is a brief description of the main features of our control:
Public Properties
Using the codeFlat BorderTo reach the Flat Border look, we have to repaint the whole border and the arrow box, so we manage the Protected Overrides Sub WndProc(ByRef m As Message)
MyBase.WndProc(m)
If Me.BorderStyle = TipiBordi.FlatXP Then
Select Case m.Msg
Case &HF, &H133
'WM_PAINT
'We have to find if the Mouse is Over the combo
Dim mouseIsOver As Boolean
Dim mousePosition As Point = Control.MousePosition
mousePosition = PointToClient(mousePosition)
mouseIsOver = ClientRectangle.Contains(mousePosition)
If Me.HighlightBorderOnMouseEvents AndAlso (
mouseIsOver OrElse Me.Focused) Then
Dim g As Graphics = Graphics.FromHwnd(Me.Handle)
DrawBorder(g, Me.HighlightBorderColor)
DrawHighlightedArrow(g, False)
Else
Dim g As Graphics = Graphics.FromHwnd(Me.Handle)
DrawBorder(g, m_NormalBorderColor)
DrawNormalArrow(g, True)
End If
Case &H2A3
'WM_MOUSELEAVE
If Me.Focused Then Exit Sub
If currentColor.Equals(m_HighlightBorderColor) Then
Dim mouseIsOver As Boolean
Dim mousePosition As Point = Control.MousePosition
mousePosition = PointToClient(mousePosition)
mouseIsOver = ClientRectangle.Contains(mousePosition)
If Not mouseIsOver Then
Dim g As Graphics = Graphics.FromHwnd(Me.Handle)
DrawBorder(g, m_NormalBorderColor)
DrawNormalArrow(g, True)
g.Dispose()
End If
End If
Case &H200
'WM_MOUSEMOVE
If Me.HighlightBorderOnMouseEvents = True AndAlso Not Highlighted Then
currentColor = Me.HighlightBorderColor
Dim g As Graphics = Graphics.FromHwnd(Me.Handle)
DrawBorder(g, currentColor)
DrawHighlightedArrow(g, False)
g.Dispose()
End If
Case &H46
'WM_WINDOWPOSCHANGING
If Me.BorderStyle = TipiBordi.FlatXP Then
'Repaint the arrow when pressed
If Me.HighlightBorderOnMouseEvents Then
Dim g As Graphics = Graphics.FromHwnd(Me.Handle)
Dim pressedColorBrush As Brush = New SolidBrush(m_DropDownBackColor)
Dim Larghezza As Integer =
SystemInformation.VerticalScrollBarWidth - arrowWidth
g.FillRectangle(pressedColorBrush, New Rectangle((Left - Larghezza),
Top - 1, SystemInformation.VerticalScrollBarWidth + 1, Height + 2))
Dim p As Pen = New Pen(HighlightBorderColor)
g.DrawRectangle(p, (Left - Larghezza) - 1, Top - 2,
SystemInformation.VerticalScrollBarWidth + 2, Height + 4)
DrawArrow(g, False)
g.Dispose()
Me.Invalidate()
End If
End If
Case Else
Exit Select
End Select
End If
End Sub
First thing to notice, this part of code is used only if the The
We draw the combobox in two steps: first the border ( 'Calculate the location of the Arrow Box
Private Sub ArrowBoxPosition(ByRef left As Integer,
ByRef top As Integer, ByRef width As Integer, ByRef height As Integer)
Dim rc As Rectangle = ClientRectangle
width = arrowWidth
left = rc.Right - width - 2
top = rc.Top + 2
height = rc.Height - 4
End Sub
'Draw the Flat Arrow Box when not highlighted
Private Sub DrawNormalArrow(ByRef g As Graphics, ByVal disable As Boolean)
If Me.BorderStyle = TipiBordi.FlatXP Then
Dim left, top, arrowWidth, height As Integer
ArrowBoxPosition(left, top, arrowWidth, height)
Dim stripeColorBrush As Brush = New SolidBrush(SystemColors.Control)
Dim Larghezza As Integer = SystemInformation.VerticalScrollBarWidth _
- arrowWidth
If (Me.Enabled) Then
Dim b As Brush = New SolidBrush(SystemColors.Control)
g.FillRectangle(b, New Rectangle(left - Larghezza, top - 2,
SystemInformation.VerticalScrollBarWidth, height + 4))
End If
If Me.Enabled Then
Dim p As Pen = New Pen(m_NormalBorderColor)
g.DrawLine(p, New Point(ClientRectangle.Right -
SystemInformation.VerticalScrollBarWidth - 2, ClientRectangle.Top),
New Point(ClientRectangle.Right, ClientRectangle.Top))
g.DrawLine(p, New Point(ClientRectangle.Right -
SystemInformation.VerticalScrollBarWidth - 2, ClientRectangle.Bottom - 1),
New Point(ClientRectangle.Right, ClientRectangle.Bottom - 1))
If Not disable Then
DrawHighlightedArrow(g, True)
g.FillRectangle(stripeColorBrush, left, top - 1,
arrowWidth + 1, height + 2)
Else
g.FillRectangle(stripeColorBrush, left - 5, top - 1,
arrowWidth + 6, height + 2)
End If
DrawArrow(g, False)
Else
Dim p As Pen = New Pen(SystemColors.InactiveBorder)
g.DrawLine(p, New Point(ClientRectangle.Right -
SystemInformation.VerticalScrollBarWidth - 2, ClientRectangle.Top),
New Point(ClientRectangle.Right, ClientRectangle.Top))
g.DrawLine(p, New Point(ClientRectangle.Right -
SystemInformation.VerticalScrollBarWidth - 2, ClientRectangle.Bottom - 1),
New Point(ClientRectangle.Right, ClientRectangle.Bottom - 1))
' Now draw the unselected background
g.FillRectangle(stripeColorBrush, left - 5, top - 1,
arrowWidth + 6, height + 2)
DrawArrow(g, True)
End If
Highlighted = False
End If
End Sub
'Draw the Flat Arrow Box when highlighted
Private Sub DrawHighlightedArrow(ByRef g As Graphics, ByVal Delete As Boolean)
If Me.BorderStyle = TipiBordi.FlatXP Then
Dim left, top, arrowWidth, height As Integer
ArrowBoxPosition(left, top, arrowWidth, height)
If (Me.Enabled) Then
Dim comboTextWidth As Integer =
SystemInformation.VerticalScrollBarWidth - arrowWidth
If (comboTextWidth < 0) Then comboTextWidth = 1
Dim b As Brush = New SolidBrush(HighlightBorderColor)
End If
If Not Delete Then
If (DroppedDown) Then
Dim cbg As Graphics = CreateGraphics()
Dim pressedColorBrush As Brush =
New SolidBrush(m_DropDownArrowBackColor)
Dim Larghezza As Integer =
SystemInformation.VerticalScrollBarWidth - arrowWidth
cbg.FillRectangle(pressedColorBrush, New Rectangle(
(left - Larghezza), top - 1, SystemInformation.VerticalScrollBarWidth + 1,
height + 2))
Dim p As Pen = New Pen(HighlightBorderColor)
cbg.DrawRectangle(p, (left - Larghezza) - 1, top - 2,
SystemInformation.VerticalScrollBarWidth + 2, height + 4)
DrawArrow(cbg, False)
cbg.Dispose()
Exit Sub
Else
If Enabled Then
Dim b As Brush = New SolidBrush(m_DropDownBackColor)
Dim Larghezza As Integer =
SystemInformation.VerticalScrollBarWidth - arrowWidth
g.FillRectangle(b, New Rectangle((left - Larghezza), top - 1,
SystemInformation.VerticalScrollBarWidth + 1, height + 2))
Dim pencolor As Color = customBorderColor
If (pencolor.Equals(Color.Empty)) Then
pencolor = BackColor
End If
End If
End If
Else
Dim b As Brush = New SolidBrush(BackColor)
g.FillRectangle(b, left - 1, top - 1, arrowWidth + 2, height + 2)
End If
If Me.Enabled Then DrawArrow(g, False)
Highlighted = True
End If
End Sub
Private Sub DrawArrow(ByVal g As Graphics, ByVal Disable As Boolean)
If Me.BorderStyle = TipiBordi.FlatXP Then
Dim left, top, arrowWidth, height As Integer
ArrowBoxPosition(left, top, arrowWidth, height)
Dim extra As Integer = 1
If (bUsingLargeFont) Then extra = 2
'triangle vertex of the arrow
Dim pts(2) As Point
pts(0) = New Point(left + arrowWidth / 2 - 2 - extra - 2,
top + height / 2 - 1)
pts(1) = New Point(left + arrowWidth / 2 + 3 + extra - 1,
top + height / 2 - 1)
pts(2) = New Point(left + arrowWidth / 2 - 1,
(top + height / 2 - 1) + 3 + extra)
'draw the arrow as a polygon
If (Disable) Then
Dim b As Brush = New SolidBrush(arrowDisableColor)
g.FillPolygon(b, pts)
Else
Dim b As Brush = New SolidBrush(arrowColor)
g.FillPolygon(b, pts)
End If
End If
End Sub
Private Sub DrawBorder(ByVal g As Graphics, ByVal DrawColor As Color)
If Me.BorderStyle = TipiBordi.FlatXP Then
g.DrawRectangle(New pen(Me.BackColor, 1), ClientRectangle.Left + 1,
ClientRectangle.Top + 1, ClientRectangle.Width - 1,
ClientRectangle.Height - 3)
'Draw the Border
If Me.Enabled = False Then 'combo disabilitato
DrawColor = SystemColors.InactiveBorder
End If
Dim pen As pen = New pen(DrawColor, 1)
'Border Rectangle
g.DrawRectangle(pen, ClientRectangle.Left, ClientRectangle.Top,
ClientRectangle.Width - 1, ClientRectangle.Height - 1)
'Button Rectangle
g.DrawRectangle(pen, ClientRectangle.Left, ClientRectangle.Top,
ClientRectangle.Width - SystemInformation.VerticalScrollBarWidth - 3,
ClientRectangle.Height - 1)
End If
End Sub
I think there is nothing tricky about this code, just some GDI+ work and a lot of time to reach the desired result! Autocomplete feature (UPDATED)This feature is based on the original "AutoComplete Protected Overrides Sub OnKeyPress_
(ByVal e As System.Windows.Forms.KeyPressEventArgs)
'AUTOCOMPLETE: we have to know when a key has been really pressed
If Me.DropDownStyle = CustomDropDownStyle.DropDown Then
PressedKey = True
If Asc(e.KeyChar) = 8 Then
If Me.SelectedText = Me.Text Then
Me.SelectedIndex = -1
End If
If Asc(e.KeyChar) = 13 Then e.Handled = True _
'This is used to suppress the "Beep" when Enter key is pressed
End If
Else
'ReadOnly AutoComplete Management
Dim sTypedText As String
Dim iFoundIndex As Integer
Dim currentText As String
Dim Start, selLength As Integer
If Asc(e.KeyChar) = 8 Then
If Me.SelectedText = Me.Text Then
PressedKey = True
Me.SelectedIndex = -1
Exit Sub
End If
End If
If Me.SelectionLength > 0 Then
Start = Me.SelectionStart
selLength = Me.SelectionLength
'This is equivalent to Me.Text, but sometimes using Me.Text
'it doesn't work
currentText = Me.AccessibilityObject.Value
Dim posizione As Long
Dim testingString As String
posizione = InStr(Me.Text, "&")
If posizione > 0 Then
'The "&" character is contained in Me.Text
testingString = Microsoft.VisualBasic.Left(Me.Text, _
posizione - 1) & Microsoft.VisualBasic.Right_
(Me.Text, Len(Me.Text) - posizione)
Else
testingString = Me.Text
End If
If UCase(testingString) = UCase(Me.AccessibilityObject.Value) _
Then
currentText = Me.Text
End If
currentText = currentText.Remove(Start, selLength)
currentText = currentText.Insert(Start, e.KeyChar)
sTypedText = currentText
Else
Start = Me.SelectionStart
sTypedText = Me.Text.Insert(Start, e.KeyChar)
End If
iFoundIndex = Me.FindString(sTypedText)
If (iFoundIndex >= 0) Then
PressedKey = True
Else
e.Handled = True
End If
End If
MyBase.OnKeyPress(e)
End Sub
Protected Overrides Sub OnKeyDown(ByVal e As _
System.Windows.Forms.KeyEventArgs)
Debug.WriteLine("OnKeyDown " & Me.Text)
If Me.DropDownStyle = CustomDropDownStyle.DropDownList _
AndAlso e.KeyCode = Keys.Delete Then
If Me.Text <> Me.SelectedText Then
e.Handled = True
Else
Me.SelectedIndex = -1
End If
End If
If Me.DropDownStyle = CustomDropDownStyle.DropDown _
AndAlso e.KeyCode = Keys.Delete Then
If Me.Text = Me.SelectedText Then
Me.SelectedIndex = -1
End If
End If
MyBase.OnKeyDown(e)
End Sub
Protected Overrides Sub OnKeyUp(ByVal e As System.Windows.Forms.KeyEventArgs)
'AUTOCOMPLETING
'WARNING: With VB.NET 2003 there is a strange behaviour.
'This event is raised not just when any key is pressed
'but also when the Me.Text property changes. Particularly,
'it happens when you write in a fast way (for example
'you press 2 keys and the event is raised 3 times).
'To manage this we have added a boolean variable PressedKey that
'is set to true in the OnKeyPress Event
Dim sTypedText As String
Dim iFoundIndex As Integer
Dim oFoundItem As Object
Dim sFoundText As String
Dim sAppendText As String
Debug.WriteLine("OnKeyUp " & Me.Text)
If PressedKey Then
'Ignoring alphanumeric chars
Select Case e.KeyCode
Case Keys.Left, Keys.Right, Keys.Up, Keys.Delete, _
Keys.Down, Keys.End, Keys.Home
Return
End Select
'Get the Typed Text and Find it in the list
sTypedText = Me.Text
If e.KeyCode <> Keys.Back Then
iFoundIndex = Me.FindString(sTypedText)
Else
iFoundIndex = Me.FindStringExact(sTypedText)
End If
'If we found the Typed Text in the list then Autocomplete
If iFoundIndex >= 0 AndAlso Me.Text <> "" Then
'Get the Item from the list (Return Type depends if
'Datasource was bound or List Created)
oFoundItem = Me.Items(iFoundIndex)
'Use the ListControl.GetItemText to resolve the Name
'in case the Combo was Data bound
sFoundText = Me.GetItemText(oFoundItem)
'Append then found text to the typed text to preserve case
sAppendText = sFoundText.Substring(sTypedText.Length)
Me.Text = sTypedText & sAppendText
'Select the Appended Text
Me.SelectionStart = sTypedText.Length
Me.SelectionLength = sAppendText.Length
If e.KeyCode = Keys.Enter Then
iFoundIndex = Me.FindStringExact(Me.Text)
Me.SelectedIndex = iFoundIndex
SendKeys.Send(vbTab)
e.Handled = True
End If
Else
'Forcing SelectedItem to Nothing if we can't Autocomplete
Me.SelectedIndex = -1
Me.SelectedItem = Nothing
End If
End If
PressedKey = False
End Sub
Protected Overrides Sub OnLeave(ByVal e As System.EventArgs)
'Selecting the item whose text is shown in the text area of the ComboBox
Dim iFoundIndex As Integer
'The Me.AccessibilityObject.Value is used instead of Me.Text to manage
'the event when you write in the combobox text and the DropDownList
'is open. In this case, if you click outside the combo, Me.Text maintains
'the old value and not the current one
Dim currentText As String
currentText = Me.AccessibilityObject.Value
Dim testingString As String
Dim posizione As Long
posizione = InStr(Me.Text, "&")
If posizione > 0 Then
'The "&" character is contained in Me.Text
testingString = Microsoft.VisualBasic.Left(Me.Text, posizione - 1) _
& Microsoft.VisualBasic.Right(Me.Text, Len(Me.Text) - posizione)
Else
testingString = Me.Text
End If
If UCase(testingString) = UCase(Me.AccessibilityObject.Value) Then
currentText = Me.Text
End If
iFoundIndex = Me.FindStringExact(currentText)
Me.SelectedIndex = iFoundIndex
If iFoundIndex = -1 Then
Me.SelectedItem = Nothing
End If
MyBase.OnLeave(e)
End Sub
The Multicolumn itemsTo have more columns in the Public Class MTGCComboBoxItem
'Since all we need is a "Text" property for this example we can
'Subclass by inheriting any object desired.
'For this example, we'll use the ListViewItem
Inherits ListViewItem
Implements IComparable
'each of the below public declarations will be "visible" to the outside
'You may add as many of these declarations using whatever types you desire
Public Col1 As String
Public Col2 As String
Public Col3 As String
Public Col4 As String
'every value of MyInfo you want to store, get's added to the NEW declaration
Sub New(ByVal C1 As String, Optional ByVal C2 As String = "",
Optional ByVal C3 As String = "", Optional ByVal C4 As String = "")
MyBase.New()
'transfer all incoming parameters to your local storage
Col1 = C1
Col2 = C2
Col3 = C3
Col4 = C4
'and finally, pass back the Text property
Me.Text = C1
End Sub
'Function used to sort the items on first element Col1
Private Function CompareTo(ByVal obj As Object)
As Integer Implements IComparable.CompareTo
'every not nothing object is greater than nothing
If obj Is Nothing Then Return 1
'this is used to take care of late binding
Dim other As MTGCComboBoxItem = CType(obj, MTGCComboBoxItem)
'comparing strings
Return StrComp(Col1, other.Col1, CompareMethod.Text)
End Function
End Class
Every In this way the user can load data in multiple columns, but they have to be shown when the arrow is clicked and the '......
'item selected
e.Graphics.FillRectangle(New SolidBrush(DropDownBackColor), r)
Select Case Me.ColumnNum
Case 1
If wcol1 > 0 Then
If Me.LoadingType = CaricamentoCombo.DataTable Then
e.Graphics.DrawString(Assegna(m_DataTable.Rows(e.Index)
(Indice(0))).ToString, Me.Font, New SolidBrush(DropDownForeColor),
rd.X, rd.Y, sf)
ElseIf Me.LoadingType = CaricamentoCombo.ComboBoxItem Then
e.Graphics.DrawString(Me.Items.Item(e.Index).Col1.ToString,
Me.Font, New SolidBrush(DropDownForeColor), rd.X, rd.Y, sf)
End If
If Me.m_GridLineHorizontal Then
e.Graphics.DrawLine(New Pen(GridLineColor, 1), rd.X, rd.Y +
rd.Height - 1, rd.X + Me.DropDownWidth, rd.Y + rd.Height - 1)
End If
End If
Case 2
If wcol1 > 0 Then
If Me.LoadingType = CaricamentoCombo.DataTable Then
e.Graphics.DrawString(Assegna(m_DataTable.Rows(
e.Index)(Indice(0))).ToString, Me.Font, New SolidBrush(
DropDownForeColor), rd.X, rd.Y, sf)
ElseIf Me.LoadingType = CaricamentoCombo.ComboBoxItem Then
e.Graphics.DrawString(Me.Items.Item(e.Index).Col1.ToString,
Me.Font, New SolidBrush(DropDownForeColor), rd.X, rd.Y, sf)
End If
End If
If wcol2 > 0 Then
If Me.m_GridLineVertical Then
e.Graphics.DrawLine(New Pen(GridLineColor, 1), rd.X +
CInt(wcol1) - 2, rd.Y, rd.X + CInt(wcol1) - 2, rd.Y + 15)
End If
e.Graphics.FillRectangle(New SolidBrush(DropDownBackColor),
rd.X + CInt(wcol1) - 1, rd.Y, r.Width - CInt(wcol1) + 1, r.Height)
If Me.LoadingType = CaricamentoCombo.DataTable Then
e.Graphics.DrawString(Assegna(m_DataTable.Rows(e.Index)
(Indice(1))).ToString, Me.Font, New SolidBrush(DropDownForeColor),
rd.X + CInt(wcol1), rd.Y, sf)
ElseIf Me.LoadingType = CaricamentoCombo.ComboBoxItem Then
e.Graphics.DrawString(Me.Items.Item(e.Index).Col2.ToString,
Me.Font, New SolidBrush(DropDownForeColor), rd.X + CInt(wcol1), rd.Y, sf)
End If
End If
If Me.m_GridLineHorizontal Then
e.Graphics.DrawLine(New Pen(GridLineColor, 1), rd.X, rd.Y +
rd.Height - 1, rd.X + Me.DropDownWidth, rd.Y + rd.Height - 1)
End If
Case 3
If wcol1 > 0 Then
If Me.LoadingType = CaricamentoCombo.DataTable Then
e.Graphics.DrawString(Assegna(m_DataTable.Rows(e.Index)
(Indice(0))).ToString, Me.Font, New SolidBrush(DropDownForeColor),
rd.X, rd.Y, sf)
ElseIf Me.LoadingType = CaricamentoCombo.ComboBoxItem Then
e.Graphics.DrawString(Me.Items.Item(e.Index).Col1.ToString,
Me.Font, New SolidBrush(DropDownForeColor), rd.X, rd.Y, sf)
End If
End If
If wcol2 > 0 Then
If Me.m_GridLineVertical Then
e.Graphics.DrawLine(New Pen(GridLineColor, 1), rd.X + CInt(wcol1)
- 2, rd.Y, rd.X + CInt(wcol1) - 2, rd.Y + 15)
End If
e.Graphics.FillRectangle(New SolidBrush(DropDownBackColor), rd.X +
CInt(wcol1) - 1, rd.Y, r.Width - CInt(wcol1) + 1, r.Height)
If Me.LoadingType = CaricamentoCombo.DataTable Then
e.Graphics.DrawString(Assegna(m_DataTable.Rows(e.Index)
(Indice(1))).ToString, Me.Font, New SolidBrush(DropDownForeColor),
rd.X + CInt(wcol1), rd.Y, sf)
ElseIf Me.LoadingType = CaricamentoCombo.ComboBoxItem Then
e.Graphics.DrawString(Me.Items.Item(e.Index).Col2.ToString,
Me.Font, New SolidBrush(DropDownForeColor), rd.X + CInt(wcol1), rd.Y, sf)
End If
End If
If wcol3 > 0 Then
If Me.m_GridLineVertical Then
e.Graphics.DrawLine(New Pen(GridLineColor, 1), rd.X + CInt(wcol1)
+ CInt(wcol2) - 2, rd.Y, rd.X + CInt(wcol1) + CInt(wcol2) - 2,
rd.Y + 15)
End If
e.Graphics.FillRectangle(New SolidBrush(DropDownBackColor), rd.X
+ CInt(wcol1) + CInt(wcol2) - 1, rd.Y, r.Width - CInt(wcol1) -
CInt(wcol2) + 1, r.Height)
If Me.LoadingType = CaricamentoCombo.DataTable Then
e.Graphics.DrawString(Assegna(m_DataTable.Rows(e.Index)
(Indice(2))).ToString, Me.Font, New SolidBrush(DropDownForeColor),
rd.X + CInt(wcol1) + CInt(wcol2), rd.Y, sf)
ElseIf Me.LoadingType = CaricamentoCombo.ComboBoxItem Then
e.Graphics.DrawString(Me.Items.Item(e.Index).Col3.ToString, Me.Font,
New SolidBrush(DropDownForeColor), rd.X + CInt(wcol1)
+ CInt(wcol2), rd.Y, sf)
End If
End If
If Me.m_GridLineHorizontal Then
e.Graphics.DrawLine(New Pen(GridLineColor, 1), rd.X, rd.Y
+ rd.Height - 1, rd.X + Me.DropDownWidth, rd.Y + rd.Height - 1)
End If
Case 4
If wcol1 > 0 Then
If Me.LoadingType = CaricamentoCombo.DataTable Then
e.Graphics.DrawString(Assegna(m_DataTable.Rows(
e.Index)(Indice(0))).ToString, Me.Font, New SolidBrush(
DropDownForeColor), rd.X, rd.Y, sf)
ElseIf Me.LoadingType = CaricamentoCombo.ComboBoxItem Then
e.Graphics.DrawString(Me.Items.Item(e.Index).Col1.ToString,
Me.Font, New SolidBrush(DropDownForeColor), rd.X, rd.Y, sf)
End If
End If
If wcol2 > 0 Then
If Me.m_GridLineVertical Then
e.Graphics.DrawLine(New Pen(GridLineColor, 1), rd.X +
CInt(wcol1) - 2, rd.Y, rd.X + CInt(wcol1) - 2, rd.Y + 15)
End If
e.Graphics.FillRectangle(New SolidBrush(DropDownBackColor),
rd.X + CInt(wcol1) - 1, rd.Y, r.Width - CInt(wcol1) +
1, r.Height)
If Me.LoadingType = CaricamentoCombo.DataTable Then
e.Graphics.DrawString(Assegna(m_DataTable.Rows(e.Index)(Indice(1)
)).ToString, Me.Font, New SolidBrush(DropDownForeColor), rd.X +
CInt(wcol1), rd.Y, sf)
ElseIf Me.LoadingType = CaricamentoCombo.ComboBoxItem Then
e.Graphics.DrawString(Me.Items.Item(e.Index).Col2.ToString,
Me.Font, New SolidBrush(DropDownForeColor), rd.X + CInt(wcol1), rd.Y, sf)
End If
End If
If wcol3 > 0 Then
If Me.m_GridLineVertical Then
e.Graphics.DrawLine(New Pen(GridLineColor, 1), rd.X + CInt(wcol1) +
CInt(wcol2) - 2, rd.Y, rd.X + CInt(wcol1) + CInt(wcol2) - 2,
rd.Y + 15)
End If
e.Graphics.FillRectangle(New SolidBrush(DropDownBackColor),
rd.X + CInt(wcol1) + CInt(wcol2) - 1, rd.Y, r.Width - CInt(wcol1)
- CInt(wcol2) + 1, r.Height)
If Me.LoadingType = CaricamentoCombo.DataTable Then
e.Graphics.DrawString(Assegna(m_DataTable.Rows(e.Index)(
Indice(2))).ToString, Me.Font, New SolidBrush(DropDownForeColor),
rd.X + CInt(wcol1) + CInt(wcol2), rd.Y, sf)
ElseIf Me.LoadingType = CaricamentoCombo.ComboBoxItem Then
e.Graphics.DrawString(Me.Items.Item(e.Index).Col3.ToString, Me.Font,
New SolidBrush(DropDownForeColor), rd.X + CInt(wcol1)
+ CInt(wcol2), rd.Y, sf)
End If
End If
If wcol4 > 0 Then
If Me.m_GridLineVertical Then
e.Graphics.DrawLine(New Pen(GridLineColor, 1), rd.X + CInt(wcol1)
+ CInt(wcol2) + CInt(wcol3) - 2, rd.Y, rd.X + CInt(wcol1) +
CInt(wcol2) + CInt(wcol3) - 2, rd.Y + 15)
End If
e.Graphics.FillRectangle(New SolidBrush(DropDownBackColor), rd.X +
CInt(wcol1) + CInt(wcol2) + CInt(wcol3) - 1, rd.Y, r.Width -
CInt(wcol1) - CInt(wcol2) - CInt(wcol3) + 1, r.Height)
If Me.LoadingType = CaricamentoCombo.DataTable Then
e.Graphics.DrawString(Assegna(m_DataTable.Rows(e.Index)(
Indice(3))).ToString, Me.Font, New SolidBrush(DropDownForeColor),
rd.X + CInt(wcol1) + CInt(wcol2) + CInt(wcol3), rd.Y, sf)
ElseIf Me.LoadingType = CaricamentoCombo.ComboBoxItem Then
e.Graphics.DrawString(Me.Items.Item(e.Index).Col4.ToString,
Me.Font, New SolidBrush(DropDownForeColor), rd.X + CInt(wcol1)
+ CInt(wcol2) + CInt(wcol3), rd.Y, sf)
End If
End If
If Me.m_GridLineHorizontal Then
e.Graphics.DrawLine(New Pen(GridLineColor, 1), rd.X, rd.Y +
rd.Height - 1, rd.X + Me.DropDownWidth, rd.Y + rd.Height - 1)
End If
End Select
If Me.BorderStyle = TipiBordi.FlatXP Then
'Use the border color to highlight the selected item
If Me.GridLineHorizontal Then
e.Graphics.DrawRectangle(New Pen(Me.HighlightBorderColor, 1),
r.X, r.Y, r.Width - 1, r.Height - 2)
Else
e.Graphics.DrawRectangle(New Pen(Me.HighlightBorderColor, 1),
r.X, r.Y, r.Width - 1, r.Height - 1)
End If
End If
e.DrawFocusRectangle()
'.......
'Here is the code for the unselected items
'.......
This piece of code represents the drawing of the selected item. Depending on how many columns have to be shown (the Using the controlOk, I guess you want to know how to use this control! Add it to your VS ToolBox referencing the MTGCComboBox.dll file, then drag and drop the control to your form. First thing to notice: the text control is empty! How many times we had to manually empty it, especially with Loading ItemsLet's go on! Now you can set its properties in the way you want in design mode, and after that write the code to load the combo. For example, suppose you want to load the combo with information about our five continents: name, extension and population. You will have to do something like this: comboContinent.BorderStyle = MTGCComboBox.TipiBordi.FlatXP
comboContinent.LoadingType = MTGCComboBox.CaricamentoCombo.ComboBoxItem
comboContinent.ColumnNum = 3
comboContinent.ColumnWidth = "80;120;100"
comboContinent.Items.Add(New MTGCComboBoxItem("Africa",
"30,065,000 sq km", "807,419,000"))
comboContinent.Items.Add(New MTGCComboBoxItem("America",
"42,293,000 sq km", "830,722,000"))
comboContinent.Items.Add(New MTGCComboBoxItem("Asia",
"44,579,000 sq km", "3,701,000,000"))
comboContinent.Items.Add(New MTGCComboBoxItem("Europe",
"9,938,000 sq km", "730,916,000 "))
comboContinent.Items.Add(New MTGCComboBoxItem("Oceania",
"8,112,000 sq km", "31,090,000"))
The ATTENTION: the In this case, we add each Dim continentItems(4) As MTGCComboBoxItem
continentItems(0) = New MTGCComboBoxItem("Africa",
"30,065,000 sq km", "807,419,000")
continentItems(1) = New MTGCComboBoxItem("America",
"42,293,000 sq km", "830,722,000")
continentItems(2) = New MTGCComboBoxItem("Asia",
"44,579,000 sq km", "3,701,000,000")
continentItems(3) = New MTGCComboBoxItem("Europe",
"9,938,000 sq km", "730,916,000 ")
continentItems(4) = New MTGCComboBoxItem("Oceania",
"8,112,000 sq km", "31,090,000")
comboContinent.Items.AddRange(continentItems)
The other way to load the combo is through a Dim dtContinents As New DataTable("ContinentInfo")
dtContinents.Columns.Add("Name", System.Type.GetType("System.String"))
dtContinents.Columns.Add("Extension",
System.Type.GetType("System.String"))
dtContinents.Columns.Add("Population",
System.Type.GetType("System.String"))
Dim dr As DataRow
dr = dtContinents.NewRow
dr("Name") = "Africa"
dr("Extension") = "30,065,000 sq km"
dr("Population") = "807,419,000"
dtContinents.Rows.Add(dr)
dr = dtContinents.NewRow
dr("Name") = "America"
dr("Extension") = "42,293,000 sq km"
dr("Population") = "830,722,000"
dtContinents.Rows.Add(dr)
dr = dtContinents.NewRow
dr("Name") = "Asia"
dr("Extension") = "44,579,000 sq km"
dr("Population") = "3,701,000,000"
dtContinents.Rows.Add(dr)
dr = dtContinents.NewRow
dr("Name") = "Europe"
dr("Extension") = "9,938,000 sq km"
dr("Population") = "730,916,000"
dtContinents.Rows.Add(dr)
dr = dtContinents.NewRow
dr("Name") = "Oceania"
dr("Extension") = "8,112,000 sq km"
dr("Population") = "31,090,000"
dtContinents.Rows.Add(dr)
comboContinent.LoadingType =
MTGCComboBox.CaricamentoCombo.DataTable
comboContinent.SourceDataString = New String(2)
{"Name", "Extension", "Population"}
comboContinent.SourceDataTable = dtContinents
Obviously, in most cases you won't fill the Selecting items (NEW)To select an item in the combobox you can use the Here is an example: 'Usually, when I Select an item during the Loading of a Form,
'I set the SelectedIndex property to -1
'If no value is found, but this is not mandatory
If Not mcbo.ItemSelect(2, "Value1", False, False) Then
mcbo.SelectedIndex = -1
End If
Accessing items (NEW)When an item is selected, you can access each column's value in this way: Dim Name, Population, Extension as String
Name = comboContinent.SelectedItem.col1
Extension = comboContinent.SelectedItem.col2
Population= comboContinent.SelectedItem.col3
Points of InterestDid you know that the classic VS
Work in progressYeah, we want to improve this control (so we need your help to correct bugs and with advices about new features). Right now, our work is focused on:
History
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||