Combobox: use special/custom font for the selecteditem only - vb.net

I have a combobox with several customised items. Here's my code (which works perfectly):
Private intcmbFontStyleTotalHeight As Integer
Private CorpusFontStyleTitre1 As Font = New Font(New FontFamily("Lato"), 18, FontStyle.Bold, 3)
Private CorpusFontStyleTitre2 As Font = New Font(New FontFamily("Lato"), 16, FontStyle.Underline Or FontStyle.Bold, 3)
Private CorpusFontStyleTitre3 As Font = New Font(New FontFamily("Lato"), 14, FontStyle.Underline Or FontStyle.Bold, 3)
Private CorpusFontStyleTitre4 As Font = New Font(New FontFamily("Lato"), 12, FontStyle.Underline, 3)
Private CorpusFontStyleCorpsdeTexte As Font = New Font(New FontFamily("Lato"), 12, FontStyle.Regular, 3)
Private FontStyleForDisplay As Font = New Font(New FontFamily("Lato"), 9, FontStyle.Regular, 3)
Public Structure CorpusFontStyleItem
Dim strTextStyleName As String
Dim fontTextStyle As Font
Public Overrides Function ToString() As String
Return strTextStyleName
End Function
End Structure
Private Sub frmCorpusManagement_Load(sender As Object, e As EventArgs) Handles MyBase.Load
LoadcmbFontStyle()
cmbFontStyle.ComboBox.DrawMode = DrawMode.OwnerDrawVariable
AddHandler cmbFontStyle.ComboBox.DrawItem, AddressOf cmbFontStyle_DrawItem
AddHandler cmbFontStyle.ComboBox.MeasureItem, AddressOf cmbFontStyle_MeasureItem
End Sub
Private Sub cmbFontStyle_MeasureItem(ByVal sender As Object, ByVal e As MeasureItemEventArgs)
Select Case e.Index
Case "1"
e.ItemHeight = 50
Case "2"
e.ItemHeight = 40
Case "3"
e.ItemHeight = 30
Case "4"
e.ItemHeight = 20
Case "5"
e.ItemHeight = 10
End Select
cmbFontStyle.DropDownHeight = intcmbFontStyleTotalHeight
End Sub
Private Sub cmbFontStyle_DrawItem(ByVal sender As Object, ByVal e As DrawItemEventArgs)
e.DrawBackground()
Dim myItem As CorpusFontStyleItem = DirectCast(cmbFontStyle.Items(e.Index), CorpusFontStyleItem)
e.Graphics.DrawString(myItem.strTextStyleName, myItem.fontTextStyle, New SolidBrush(Color.Black), e.Bounds)
e.DrawFocusRectangle()
End Sub
Private Sub LoadcmbFontStyle()
Dim itemCorpusFontStyleTitre1 As New CorpusFontStyleItem With {.strTextStyleName = "Titre 1", .fontTextStyle = CorpusFontStyleTitre1}
Dim itemCorpusFontStyleTitre2 As New CorpusFontStyleItem With {.strTextStyleName = "Titre 2", .fontTextStyle = CorpusFontStyleTitre2}
Dim itemCorpusFontStyleTitre3 As New CorpusFontStyleItem With {.strTextStyleName = "Titre 3", .fontTextStyle = CorpusFontStyleTitre3}
Dim itemCorpusFontStyleTitre4 As New CorpusFontStyleItem With {.strTextStyleName = "Titre 4", .fontTextStyle = CorpusFontStyleTitre4}
Dim itemCorpusFontStyleCorps As New CorpusFontStyleItem With {.strTextStyleName = "Corps de Texte", .fontTextStyle = CorpusFontStyleCorpsdeTexte}
cmbFontStyle.Items.Add(itemCorpusFontStyleTitre1)
cmbFontStyle.Items.Add(itemCorpusFontStyleTitre2)
cmbFontStyle.Items.Add(itemCorpusFontStyleTitre3)
cmbFontStyle.Items.Add(itemCorpusFontStyleTitre4)
cmbFontStyle.Items.Add(itemCorpusFontStyleCorps)
End Sub
Now, I need that the selected item displays using FontStyleForDisplay (same feature as the font combobox in Word).
I guessed it would be achieved with SelectedIndexChanged event and tried this but doesn't seem to work:
Private Sub cmbFontStyle_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbFontStyle.SelectedIndexChanged
cmbFontStyle.SelectedItem.Graphics.DrawString(cmbFontStyle.Text, FontStyleForDisplay, New SolidBrush(Color.Black), cmbFontStyle.SelectedItem.Bounds)
cmbFontStyle.SelectedItem.DrawFocusRectangle()
End Sub
I found some answers for wpf but not for Winforms

The only way to solve my problem was to set the droplistmode of the combobox to DropDown instead of DropDownList.

Related

Finding the name of labels based on text inside its name

I’m not really sure this is possible but it would be helpful: I have quite a large grid of labels and would like to change the visibility of them based on words inside their names, for example finding a list of labels which all contain “Twentytwo” in their name and setting their visibility to false
Here is a solution based on Control.GetNextControl:
Const textToSearch = "Twentytwo" 'Text to search
Dim c As Control = Me.GetNextControl(Me, True)
Do Until c Is Nothing
'Check if c is label and its name contains "textToSearch"
If TypeOf c Is Label AndAlso c.Name.Contains(textToSearch) Then
c.Visible = False 'Hide this label
End If
c = Me.GetNextControl(c, True)
Loop
If you want to search "Twentytwo" in the label text and not in its name, replace c.Name.Containswith c.Text.Contains.
Let's say that your labels are all in one container, perhaps a Panel.
Further, let's assume you that you want only the specified labels to have .Visible = False, so the visibility is simply whether or not the label's name contains the given string.
You can select for controls of a particular type, so that there's no need to check the type of the control before using it, like this:
Sub SetLabelVisibility(container As Control, namePart As String)
container.SuspendLayout()
For Each lbl In container.Controls.OfType(Of Label)
lbl.Visible = lbl.Name.Contains(namePart)
Next
container.ResumeLayout()
End Sub
And you would call it with
SetLabelVisibility(Panel1, "Twentytwo")
Suspending the layout of the container while the updates to its child controls are being made is so that the display of it is done in one go.
Demonstration using an appropriately-sized Panel on a Form:
Public Class Form1
Dim tim As New Timer With {.Interval = 1000}
Sub CreateLabels(target As Panel)
For j = 0 To 6
For i = 0 To 4
Dim lbl = New Label With {
.Name = $"Label{j}_{i}",
.BackColor = Color.BlanchedAlmond,
.Text = i & j,
.Location = New Point(j * 40, i * 20),
.Size = New Size(38, 18),
.TextAlign = ContentAlignment.MiddleCenter
}
target.Controls.Add(lbl)
Next
Next
End Sub
Sub SetLabelVisibility(container As Control, namePart As String)
container.SuspendLayout()
For Each lbl In container.Controls.OfType(Of Label)
lbl.Visible = lbl.Name.Contains(namePart)
Next
container.ResumeLayout()
End Sub
Sub ChangeVisibleLabels(sender As Object, e As EventArgs)
Static n As Integer = 0
SetLabelVisibility(Panel1, n.ToString())
n = (n + 1) Mod 7
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
CreateLabels(Panel1)
AddHandler tim.Tick, AddressOf ChangeVisibleLabels
tim.Start()
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
If tim IsNot Nothing Then
RemoveHandler tim.Tick, AddressOf ChangeVisibleLabels
tim.Dispose()
End If
End Sub
End Class
Public Class Form1
Dim tim As New Timer With {.Interval = 1000}
Sub CreateLabels(target As Panel)
For j = 0 To 6
For i = 0 To 4
Dim lbl = New Label With {
.Name = $"Label{j}_{i}",
.BackColor = Color.BlanchedAlmond,
.Text = i & j,
.Location = New Point(j * 40, i * 20),
.Size = New Size(38, 18),
.TextAlign = ContentAlignment.MiddleCenter
}
target.Controls.Add(lbl)
Next
Next
End Sub
Sub SetLabelVisibility(container As Control, namePart As String)
container.SuspendLayout()
For Each lbl In container.Controls.OfType(Of Label)
lbl.Visible = lbl.Name.Contains(namePart)
Next
container.ResumeLayout()
End Sub
Sub ChangeVisibleLabels(sender As Object, e As EventArgs)
Static n As Integer = 0
SetLabelVisibility(Panel1, n.ToString())
n = (n + 1) Mod 7
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
CreateLabels(Panel1)
AddHandler tim.Tick, AddressOf ChangeVisibleLabels
tim.Start()
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
If tim IsNot Nothing Then
RemoveHandler tim.Tick, AddressOf ChangeVisibleLabels
tim.Dispose()
End If
End Sub
End Class
Public Class Form1
Dim tim As New Timer With {.Interval = 1000}
Sub CreateLabels(target As Panel)
For j = 0 To 6
For i = 0 To 4
Dim lbl = New Label With {
.Name = $"Label{j}_{i}",
.BackColor = Color.BlanchedAlmond,
.Text = i & j,
.Location = New Point(j * 40, i * 20),
.Size = New Size(38, 18),
.TextAlign = ContentAlignment.MiddleCenter
}
target.Controls.Add(lbl)
Next
Next
End Sub
Sub SetLabelVisibility(container As Control, namePart As String)
container.SuspendLayout()
For Each lbl In container.Controls.OfType(Of Label)
lbl.Visible = lbl.Name.Contains(namePart)
Next
container.ResumeLayout()
End Sub
Sub ChangeVisibleLabels(sender As Object, e As EventArgs)
Static n As Integer = 0
SetLabelVisibility(Panel1, n.ToString())
n = (n + 1) Mod 7
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
CreateLabels(Panel1)
AddHandler tim.Tick, AddressOf ChangeVisibleLabels
tim.Start()
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
If tim IsNot Nothing Then
RemoveHandler tim.Tick, AddressOf ChangeVisibleLabels
tim.Dispose()
End If
End Sub
End Class

Change Font of a ListView Column

I am trying to set a ListView item or a specific column in the ListView into bold. Here is my code but its not working so I am asking for help in order to make it work.
Do While dr.Read = True
x = New ListViewItem(dr("ID").ToString)
x.UseItemStyleForSubItems = False
x.SubItems.Add(dr("full_name"))
x.SubItems(1).Font = New Font(New FontFamily("Arial"), 16, FontStyle.Bold)
x.SubItems.Add(dr("address"))
x.SubItems.Add(dr("city"))
ListView1.Items.Add(x)
Loop
I just tested this code and it worked for me:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For i = 1 To 10
Dim item As New ListViewItem(i.ToString())
item.UseItemStyleForSubItems = False
Dim subitem = item.SubItems.Add(i.ToString())
subitem.Font = New Font(New FontFamily("Arial"), 16, FontStyle.Bold)
item.SubItems.Add(i.ToString())
ListView1.Items.Add(item)
Next
End Sub
End Class
There's something else up with your code.

Loop through Dates vb.net

I want to create a datagridview every day. Here is my code so far. It's not displaying any errors but when I run the code it just load the form but the dgv does not appear. What can I do?
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim start As Date = Date.Now
Dim apocap As Date = Date.Parse(#8/22/2050#)
Dim loopdate As Date = start
While start < apocap
Dim dgv As New DataGridView
With dgv
.Size = New Size(250, 250)
.ColumnCount = 2
.RowCount = 12
.Location = New Point(12, 9)
.Visible = True
End With
start = start.Date.AddDays(1)
End While
End Sub
End Class
You have to add them to your form. Additionally, you'll want to change the .Left and/or .Top (.Location) properties so they don't all stack on top of each other:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim start As DateTime = DateTime.Today
Dim apocap As New DateTime(2050, 8, 22)
Dim i As Integer = 0
While start < apocap
Dim dgv As New DataGridView
With dgv
.Size = New Size(250, 250)
.ColumnCount = 2
.RowCount = 12
.Location = New Point(12, (9 + (250 * i)))
.Visible = True
End With
Me.Controls.Add(dgv)
i += 1
start = start.AddDays(1)
End While
End Sub
For fun, I like to write it like this:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim start As DateTime = DateTime.Today
Dim apocap As New DateTime(2050, 8, 22)
Dim count As Integer = CInt((apocap - start).TotalDays)
Me.Controls.AddRange(Enumerable.Range(0, count).Select(
Function(i)
Return New DataGridView With {
.Size = New Size(250, 250),
.ColumnCount = 21,
.RowCount = 12,
.Location = New Point(12, (9 + (250 * i))),
.Visible = True
}
'start.AddDays(i) is there if you need it
End Function
).ToArray())
End Sub

Bound Listbox turns items invisible on data refresh. Why?

I have a list box bound to a list is a class. All works fine until I try to add a new item to the list. during this process the data source is set to nothing to refresh the list and apparently 'Refresh' doesn't do it. The list gets refreshed and the other controls bound to the listboxes data show that the list is there and is correct however the list appears empty although it does show scroll bars. I have tried to change the font color, just in case.. Nothing!
Does someone know why this happens? how to fix it? Or a better way to refresh?
Code:
Private Sub btnNew_Click(sender As Object, e As EventArgs) Handles btnNew.Click
'lbNames is the listbox carrying all the data
Dim oContacts As List(Of clsContact) = lbNames.DataSource
lbNames.DataSource = Nothing
'Build the new Item, add it to the collection
Dim oNewCont As New clsContact
oNewCont.Editable = True
oNewCont.IsActive = True
oNewCont.Firstname = "Jimmy"
oNewCont.Lastname = "Smith"
oContacts.Add(oNewCont)
lbNames.Refresh()
' Re-Set up Autocomplete text box
Dim MySource As New AutoCompleteStringCollection()
For Each oc As clsContact In oContacts
MySource.Add(oc.FullName)
Next
txtName.AutoCompleteMode = AutoCompleteMode.Suggest
txtName.AutoCompleteCustomSource = MySource
txtName.AutoCompleteSource = AutoCompleteSource.CustomSource
'Set List Box data back to the collection
lbNames.DataSource = oContacts
lbNames.DisplayMember = "FullName"
End Sub
The starting LOAD:
Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim oCont As List(Of clsContact)
lbNames.DrawMode = DrawMode.OwnerDrawVariable
Dim oTypes As List(Of clsPhoneType) = loadTypes()
cboPhoneType.DataSource = oTypes
cboPhoneType.DisplayMember = "Type"
cboPhoneType.ValueMember = "ID"
oCont = LoadNames()
lbNames.DataSource = oCont
lbNames.DisplayMember = "FullName"
Dim MySource As New AutoCompleteStringCollection()
For Each oc As clsContact In oCont
MySource.Add(oc.FullName)
Next
txtName.AutoCompleteMode = AutoCompleteMode.Suggest
txtName.AutoCompleteCustomSource = MySource
txtName.AutoCompleteSource = AutoCompleteSource.CustomSource
End Sub
Private Sub lbNames_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles lbNames.DrawItem
e.DrawBackground()
Dim textBrush As Brush = Brushes.Black
Dim drawFont As System.Drawing.Font = e.Font
If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
e.Graphics.FillRectangle(Brushes.WhiteSmoke, e.Bounds)
End If
Dim oCont As clsContact = DirectCast(sender, System.Windows.Forms.ListBox).Items(e.Index)
If oCont.IsActive Then
textBrush = Brushes.Black
If oCont.IsDirty Then textBrush = Brushes.LightCoral
Else
textBrush = Brushes.LightGray
End If
Dim str = oCont.FullName
e.Graphics.DrawString(str, e.Font, textBrush, e.Bounds, StringFormat.GenericDefault)
e.DrawFocusRectangle()
End Sub
You're using the wrong tools. The List(Of T) doesn't raise any events whatsoever. When bound to a control, the control doesn't know if any items are added/removed/moved. Luckily, the BindingList(Of T) comes to rescue. It will raise a ListChanged event whenever the list is modified. As a bonus, if your class/model implements the INotifyPropertyChanged interface, the control will also reflect property changes.
lbNames.DataSource = New BindingList(Of clsContact)(oCont)
Here's a sample form to show you the basics:
Imports System.ComponentModel
Public Class Form1
Public Sub New()
Me.InitializeComponent()
Me.btnAdd = New Button With {.TabIndex = 0, .Dock = DockStyle.Top, .Height = 30, .Text = "Add new contact"}
Me.btnChange = New Button With {.TabIndex = 1, .Dock = DockStyle.Top, .Height = 30, .Text = "Change random contact name"}
Me.lbContacts = New ListBox With {.TabIndex = 2, .Dock = DockStyle.Fill}
Me.Controls.AddRange({Me.lbContacts, Me.btnChange, Me.btnAdd})
End Sub
Private Sub HandleMeLoad(sender As Object, e As EventArgs) Handles Me.Load
Dim list As New List(Of Contact)
For i As Integer = 1 To 10
list.Add(New Contact With {.Name = String.Format("Contact # {0}", i)})
Next
Me.lbContacts.DataSource = New BindingList(Of Contact)(list)
Me.lbContacts.DisplayMember = "Name"
End Sub
Private Sub HandleButtonAddClick(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim list As BindingList(Of Contact) = DirectCast(Me.lbContacts.DataSource, BindingList(Of Contact))
list.Add(New Contact With {.Name = String.Format("Contact # {0}", (list.Count + 1))})
End Sub
Private Sub HandleButtonChangeClick(sender As Object, e As EventArgs) Handles btnChange.Click
Static rnd As New Random()
Dim list As BindingList(Of Contact) = DirectCast(Me.lbContacts.DataSource, BindingList(Of Contact))
If (list.Count > 0) Then
With list.Item(rnd.Next(0, list.Count))
.Name = Guid.NewGuid().ToString()
End With
End If
End Sub
Public Class Contact
Implements INotifyPropertyChanged
Public Event PropertyChanged As PropertyChangedEventHandler Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
Public Property Name As String
Get
Return Me.m_name
End Get
Set(value As String)
If (value <> Me.m_name) Then
Me.m_name = value
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("Name"))
End If
End Set
End Property
Private m_name As String
End Class
Private WithEvents btnAdd As Button
Private WithEvents btnChange As Button
Private WithEvents lbContacts As ListBox
End Class
It seems that the solution to fixing this problem is to flip the drawmode back to normal.
adding:
lbNames.DrawMode = DrawMode.Normal
lbNames.DrawMode = DrawMode.OwnerDrawVariable
Fixes the problem.
I guess what is happening is that the drawing handler is becoming disconnected for some reason so the contests of the list are not redrawn when the data source is attached.
My new working code:
Private Sub btnNew_Click(sender As Object, e As EventArgs) Handles btnNew.Click
'lbNames is the listbox carrying all the data
Dim oContacts As List(Of clsContact) = lbNames.DataSource
lbNames.DataSource = Nothing
'Build the new Item, add it to the collection
Dim oNewCont As New clsContact
oNewCont.Editable = True
oNewCont.IsActive = True
oNewCont.Firstname = "Jimmy"
oNewCont.Lastname = "Smith"
oContacts.Add(oNewCont)
' Re-Set up Autocomplete text box
Dim MySource As New AutoCompleteStringCollection()
For Each oc As clsContact In oContacts
MySource.Add(oc.FullName)
Next
txtName.AutoCompleteMode = AutoCompleteMode.Suggest
txtName.AutoCompleteCustomSource = MySource
txtName.AutoCompleteSource = AutoCompleteSource.CustomSource
'Set List Box data back to the collection
lbNames.DataSource = oContacts
lbNames.DisplayMember = "FullName"
lbNames.DrawMode = DrawMode.Normal
lbNames.DrawMode = DrawMode.OwnerDrawVariable
End Sub

Issue with combobox autocomplete

I am trying to incorporate three comboboxes into a functional GUI which consist of textboxes, numericupdown controls and now comboboxes.
On those form I have navigation with keyboard (me.selectnextcontrol) with keys up and down.
What happens?
When I step in those GUI for first time everything work OK and I can move up/down with keyboard as expected.
Problem becomes when I edit a combobox which is in the midle of my gui and is set like this:
mycombo.AutoCompleteMode = AutoCompleteMode.SuggestAppend
mycombo.AutoCompleteSource = AutoCompleteSource.CustomSource
mycombo.AutoCompleteCustomSource = myAutoCompleteStringCollection
Problem is that when I come back to those combobox my navigation loop don't get keypress (up or down keys) anymore because combobox takes them for itselfs purpose (change index).
I try with mycombo.AutoCompleteSource = AutoCompleteSource.CustomSource after combobox_Leave but that turns off autocomplete what is not wanted.
Question is:
Is here possible to set described combobox after usage in mode like it was at the beginning of program, when it was not edited, so I can navigate with keyboard through such comboboxes at initial way but that autosuggest option remain enabled if I need to edit it again.
EDITED:
Here is simple example which shows a problem:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim tb As New TextBox
Dim cbb As New ComboBox
Dim tbb As New TextBox
Dim b1 As New Button
Dim b2 As New Button
With Me
.KeyPreview = True
.Size = New Size(350, 200)
With .Controls
.Add(tb)
With tb
.TabIndex = 0
.Location = New Point(95, 20)
.Text = "This is"
End With
.Add(cbb)
With cbb
.TabIndex = 1
.Items.AddRange(New String() {"alabama", "africa", "australia", "grenland"})
.Location = New Point(95, 50)
.Text = "an Example"
.DropDownStyle = ComboBoxStyle.DropDown
.AutoCompleteMode = AutoCompleteMode.SuggestAppend
.AutoCompleteSource = AutoCompleteSource.ListItems
End With
.Add(tbb)
With tbb
.TabIndex = 2
.Location = New Point(95, 80)
.Text = "textbox"
End With
.Add(b1)
With b1
.TabStop = False
.Location = New Point(90, 130)
.Text = "Nothing"
End With
.Add(b2)
With b2
.TabStop = False
.Location = New Point(170, 130)
.Text = "Exit"
AddHandler b2.Click, AddressOf b2_Click
End With
End With
End With
End Sub
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Up Then
e.Handled = True
Me.SelectNextControl(Me.ActiveControl, False, True, True, True)
End If
If e.KeyCode = Keys.Down Then
e.Handled = True
Me.SelectNextControl(Me.ActiveControl, True, True, True, True)
End If
End Sub
Private Sub b2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Me.Close()
End Sub
End Class
When you start this program pass several times through all controls with key down arrow. Then stop on combobox and type letter "a", then try to navigate again with key down arrow.
Public Class Form1
Private tb As New TextBox
Private cbb As New ComboBox
Private tbb As New TextBox
Private b1 As New Button
Private b2 As New Button
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AddHandler cbb.PreviewKeyDown, AddressOf cbb_PreviewKeyDown
AddHandler tb.PreviewKeyDown, AddressOf tb_PreviewKeyDown
AddHandler tbb.PreviewKeyDown, AddressOf tbb_PreviewKeyDown
Me.Size = New Size(350, 200)
With tb
.Parent = Me
.TabIndex = 0
.Location = New Point(95, 20)
.Text = "This is"
End With
With cbb
.Parent = Me
.TabIndex = 1
.Items.AddRange(New String() {"alabama", "africa", "australia", "grenland"})
.Location = New Point(95, 50)
.Text = "an Example"
.DropDownStyle = ComboBoxStyle.DropDown
.AutoCompleteMode = AutoCompleteMode.SuggestAppend
.AutoCompleteSource = AutoCompleteSource.ListItems
End With
With tbb
.Parent = Me
.TabIndex = 2
.Location = New Point(95, 80)
.Text = "textbox"
End With
With b1
.Parent = Me
.TabStop = False
.Location = New Point(90, 130)
.Text = "Nothing"
End With
With b2
.Parent = Me
.TabStop = False
.Location = New Point(170, 130)
.Text = "Exit"
AddHandler b2.Click, AddressOf b2_Click
End With
End Sub
Private Sub tb_PreviewKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs)
If e.KeyCode = Keys.Up Then tbb.Focus()
If e.KeyCode = Keys.Down Then cbb.Focus()
End Sub
Private Sub cbb_PreviewKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs)
If e.KeyCode = Keys.Up Then
cbb.AutoCompleteMode = AutoCompleteMode.None
tb.Focus()
cbb.AutoCompleteMode = AutoCompleteMode.SuggestAppend
If Not cbb.Items.Contains(cbb.Text) Then cbb.Text = ""
End If
If e.KeyCode = Keys.Down Then
cbb.AutoCompleteMode = AutoCompleteMode.None
tbb.Focus()
cbb.AutoCompleteMode = AutoCompleteMode.SuggestAppend
If Not cbb.Items.Contains(cbb.Text) Then cbb.Text = ""
End If
End Sub
Private Sub tbb_PreviewKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs)
If e.KeyCode = Keys.Up Then cbb.Focus()
If e.KeyCode = Keys.Down Then tb.Focus()
End Sub
Private Sub b2_Click()
Me.Close()
End Sub
End Class
I modified your code. And now, it's working. :)