Vb.net ComboBox Autocomplete - vb.net

I have a ComboBox with DropDownStyle = Simple and have bound it to a DataSet. What I want is : when I type 'a', I want my ComboBox to show only the items starting with the letter 'a' just Dictionary Program.
I have tried the AutoComplete property but it just shows a DropDown and that's not what I want
HERE IS MY CODE:
Private Sub TICKETING_FORM_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
da.Fill(ds, "STYLE")
bs.DataSource = ds.Tables("STYLE")
With Style_ComboBox
.DataSource = bs
.DisplayMember = "STYLE"
.ValueMember = "STYLE"
.AutoCompleteMode = AutoCompleteMode.Suggest
.AutoCompleteSource = AutoCompleteSource.ListItems
End With
End Sub

You need to set these property for AutoComplete
ComboBox1.AutoCompleteMode = AutoCompleteMode.Append
ComboBox1.DropDownStyle = ComboBoxStyle.DropDown
ComboBox1.AutoCompleteSource = AutoCompleteSource.ListItems
Private Sub TICKETING_FORM_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
da.Fill(ds, "STYLE")
bs.DataSource = ds.Tables("STYLE")
With Style_ComboBox
.DataSource = bs
.DisplayMember = "STYLE"
.ValueMember = "STYLE"
.DropDownStyle = ComboBoxStyle.DropDown
.AutoCompleteMode = AutoCompleteMode.Suggest
.AutoCompleteSource = AutoCompleteSource.ListItems
End With
End Sub

Change this Setting
DropDownStyle = DropDownList

Private Sub TICKETING_FORM_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
da.Fill(ds, "STYLE")
bs.DataSource = ds.Tables("STYLE")
With Style_ComboBox
.DataSource = bs
.DisplayMember = "STYLE"
.ValueMember = "STYLE"
.AutoCompleteMode = AutoCompleteMode.SuggestAppend ' This is necessary
.AutoCompleteSource = AutoCompleteSource.ListItems
End With
End Sub

The answers is quite simple. You load your combobox like you would normally by adding the display member and the value member. Then you load your autocomplete by the Display member and whala. Keep in mind that the auto complete function is basicly just a filter option on the display member side under normal conditions.
Here follows and example. Keep in mind that this example draws the list of names from our AD account and is nor shown here....
Dim col As New AutoCompleteStringCollection
Dim i As Integer
If Not MyUsers Is Nothing Then
For i = 0 To MyUsers.Rows.Count - 1
col.Add(MyUsers.Rows(i)("displayname").ToString)
Next
Request_ManagerComboBox.AutoCompleteSource = AutoCompleteSource.CustomSource
Request_ManagerComboBox.AutoCompleteCustomSource = col
Request_ManagerComboBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend
Request_ManagerComboBox.DisplayMember = MyUsers.Rows(i - 1)("displayname").ToString
Request_ManagerComboBox.ValueMember = MyUsers.Rows(i - 1)("samAccountName").ToString
End If

Related

Bounded ComboBox AutoComplete Sort

I have a bounded Combobox and it's data source is sorted but it seems that auto complete re-sort its items when I start typing.
For example I use this code:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim dt As New DataTable
With dt
.Columns.Add("id", GetType(Long))
.Columns.Add("Name", GetType(String))
.Rows.Add({1, "John"})
.Rows.Add({2, "Jan"})
End With
With Me.ComboBox1
.DataSource = dt
.ValueMember = "id"
.DisplayMember = "Name"
.DropDownStyle = ComboBoxStyle.DropDown
.AutoCompleteMode = AutoCompleteMode.SuggestAppend
.AutoCompleteSource = AutoCompleteSource.ListItems
End With
End Sub
Now when I type j, the Combobox suggest Jan but I want the first item John.
For unbounded Combobox, this works: How to provide automatic text completion for a ComboBox control in Visual Basic .NET or in Visual Basic 2005 and also this question is For unbounded Combobox.
I modified the code in this link and now it works as needed:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim dt As New DataTable
With dt
.Columns.Add("id", GetType(Long))
.Columns.Add("Name", GetType(String))
.Rows.Add({1, "John"})
.Rows.Add({2, "Jan"})
End With
With Me.ComboBox1
.DataSource = dt
.ValueMember = "id"
.DisplayMember = "Name"
.DropDownStyle = ComboBoxStyle.DropDown
.AutoCompleteMode = AutoCompleteMode.None
End With
End Sub
Private Sub ComboBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles ComboBox1.KeyUp
' Do nothing for some keys such as navigation keys.
If ((e.KeyCode = Keys.Back) Or
(e.KeyCode = Keys.Left) Or
(e.KeyCode = Keys.Right) Or
(e.KeyCode = Keys.Up) Or
(e.KeyCode = Keys.Delete) Or
(e.KeyCode = Keys.Down) Or
(e.KeyCode = Keys.PageUp) Or
(e.KeyCode = Keys.PageDown) Or
(e.KeyCode = Keys.Home) Or
(e.KeyCode = Keys.End)) Then
Return
End If
Dim actual As String = Me.ComboBox1.Text
Dim dt As DataTable = Me.ComboBox1.DataSource
Dim dr = dt.Select("Name like '" & actual & "%'")
If dr.Count > 0 Then
Dim found As String
With Me.ComboBox1
.DroppedDown = True
Cursor.Current = Cursors.Default 'to show the cursor again
found = dr(0)("Name")
.Text = found
.SelectionStart = actual.Length
.SelectionLength = found.Length
End With
End If
End Sub
I added this line Cursor.Current = Cursors.Default to resolve this problem.

VB.Net DatagridviewComboBoxColumn CellClick behavior

I have a DatagridviewComboBoxColumn populated from a DataTable and whenever I click on any part of the DataGridViewComboBoxCell the first value of the list shows up as it had been clicked. However, when I move the focus to another cell without selecting a value, it disappears.
Strangely, the behavior is not consistent if I apply the ComboBox values with .Items.Add(" "). Can anyone shed some light on this issue.
Here is a sample code and a gif image:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DataGridView1.Rows.Add()
DataGridView1.Rows.Add()
''DataGridViewComboBoxColumn1
DataGridViewComboBoxColumn1.Items.Add("Name1")
DataGridViewComboBoxColumn1.Items.Add("Name2")
'DataGridViewComboBoxColumn2
Dim dt As New DataTable
dt.Columns.Add("id")
dt.Columns.Add("name")
dt.Rows.Add("1", "Name1")
dt.Rows.Add("2", "Name2")
With DataGridViewComboBoxColumn2
.ValueMember = dt.Columns(0).ColumnName
.DisplayMember = dt.Columns(1).ColumnName
.DataSource = dt
End With
End Sub
End Class
Successfully replicated the behavior, but i also added 2 ComboBox to the same form and the result is same
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
DataGridView1.Rows.Add()
DataGridView1.Rows.Add()
''DataGridViewComboBoxColumn1
DataGridViewComboBoxColumn1.Items.Add("Name1")
DataGridViewComboBoxColumn1.Items.Add("Name2")
'DataGridViewComboBoxColumn2
Dim dt As New DataTable
dt.Columns.Add("id")
dt.Columns.Add("name")
dt.Rows.Add("1", "Name1")
dt.Rows.Add("2", "Name2")
With DataGridViewComboBoxColumn2
.ValueMember = dt.Columns(0).ColumnName
.DisplayMember = dt.Columns(1).ColumnName
.DataSource = dt
End With
ComboBox1.Items.Add("Name1")
ComboBox1.Items.Add("Name2")
With ComboBox2
.ValueMember = dt.Columns(0).ColumnName
.DisplayMember = dt.Columns(1).ColumnName
.DataSource = dt
End With
End Sub
End Class
Click to see gif of form in action
I found a way to correct the behavior by referring to this post:
DataGridComboBoxColumn shows first value on CellEnter
Here is the code I have used:
Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
If TypeOf e.Control Is ComboBox Then
Dim comboBox As ComboBox = DirectCast(e.Control, ComboBox)
If DataGridView1.CurrentCell.Value Is Nothing Then comboBox.SelectedIndex = -1
End If
End Sub

Auto-complete text in the datagridview for single column only

I tried to provide auto-complete text in datagridview. But, I got auto-complete text in all column and after clicking on third column (with combobox) application start displaying error on editing other cells.
I want auto-complete text box in first column i.e. "Name" only. I cannot manage code so that auto-complete doesn't show in second column i.e. "Age" and also error not occurred after clicking on combobox column.
The code is As follow.
Public Class Form1
Private Sub appData(ByVal data As AutoCompleteStringCollection, ByVal c As String)
data.Add("Ravi")
data.Add("Raj")
data.Add("Raja")
data.Add("r " & c)
End Sub
Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
Try
MsgBox(e.Control.ToString)
Dim header As String = DataGridView1.Columns(0).HeaderText
If TypeOf e.Control Is TextBox AndAlso header.Equals("Name") AndAlso DataGridView1.CurrentCell.ColumnIndex = 0 Then
If DataGridView1.CurrentCell.ColumnIndex = 0 Then
Dim text As TextBox = TryCast(e.Control, TextBox)
If text IsNot Nothing Then
text.AutoCompleteMode = AutoCompleteMode.Suggest
text.AutoCompleteSource = AutoCompleteSource.CustomSource
Dim data As AutoCompleteStringCollection = New AutoCompleteStringCollection()
appData(data, DataGridView1.CurrentCellAddress.ToString)
text.AutoCompleteCustomSource = data
End If
Else
Dim text As TextBox = TryCast(e.Control, TextBox)
text.AutoCompleteCustomSource = Nothing
text.AutoCompleteSource = AutoCompleteSource.None
text.AutoCompleteMode = AutoCompleteMode.None
End If
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DataGridView1.Columns.Add("ColName", "Name")
DataGridView1.Columns.Add("ColAge", "Age")
Dim ComCol As New DataGridViewComboBoxColumn
ComCol.Items.Add(1)
ComCol.Items.Add(2)
ComCol.HeaderText = "Combobox Col"
DataGridView1.Columns.Add(ComCol)
End Sub
End Class
Do a test in Else clause as :
If header.Equals("Name") Then
If DataGridView1.CurrentCell.ColumnIndex = 0 Then
Dim text As TextBox = TryCast(e.Control, TextBox)
If text IsNot Nothing Then
text.AutoCompleteMode = AutoCompleteMode.Suggest
text.AutoCompleteSource = AutoCompleteSource.CustomSource
Dim data As AutoCompleteStringCollection = New AutoCompleteStringCollection()
appData(data, DataGridView1.CurrentCellAddress.ToString)
text.AutoCompleteCustomSource = data
End If
ElseIf TypeOf e.Control Is TextBox Then
Dim text As TextBox = TryCast(e.Control, TextBox)
text.AutoCompleteCustomSource = Nothing
text.AutoCompleteSource = AutoCompleteSource.None
text.AutoCompleteMode = AutoCompleteMode.None
End If
End If
Private Sub DataGridView1_DataError(sender As Object, e As DataGridViewDataErrorEventArgs) Handles DataGridView1.DataError
Try
Catch ex As Exception
End Try
End Sub
Use just :
If header.Equals("Name") Then
instead of :
If TypeOf e.Control Is TextBox AndAlso header.Equals("Name") AndAlso
DataGridView1.CurrentCell.ColumnIndex = 0 Then
I believe the problem is that your DataGridView1_EditingControlShowing is firing more often than you realize.
Perhaps you should just do your autocomplete work on form load event instead? That way, it only has to run once...
The 'autocomplete work' I'm talking about, being this part:
text.AutoCompleteMode = AutoCompleteMode.Suggest
text.AutoCompleteSource = AutoCompleteSource.CustomSource
Dim data As AutoCompleteStringCollection = New AutoCompleteStringCollection()
appData(data, DataGridView1.CurrentCellAddress.ToString)
text.AutoCompleteCustomSource = data

Runtime datatable update error

I have a datagridview with a checkbox column. When I click(check/Uncheck) on checkbox field as random, two other fields in corrensponding row should be added OR removed in a datatable (declared runtime).
So that I can do some procedures with data in the datatable.
For that I have declared a datatable as global.
Now the problem is, each time when I click on a checkbox, a simple mouse scrolling is required to update datatable, OR a click needed in the new datagridview which is showing values in the datatable.
My code given below,
global declaration: Public PaymentTable As DataTable
Private Sub ShowOrdersFrm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DataBind()
Me.DGVOrders.RowsDefaultCellStyle.BackColor = Color.GhostWhite
Me.DGVOrders.AlternatingRowsDefaultCellStyle.BackColor = Color.PaleGoldenrod
PaymentTable = New DataTable()
PaymentTable.Columns.Add("RowId", GetType(Integer))
PaymentTable.Columns.Add("Amount", GetType(Decimal))
End Sub
Private Sub DataBind()
DGVOrders.DataSource = Nothing
DGVOrders.Columns.Clear()
Dim cmd As New SqlCommand
Dim da As New SqlDataAdapter
Dim dt As New DataTable
con.Open()
With cmd
.Connection = con
.CommandText = "select * from VIEW_PAYMENTS_DUES_BYORDER where CustCode='" & CustCode & "' order by OrderNo DESC"
End With
da.SelectCommand = cmd
da.Fill(dt)
BindingSource1.DataSource = dt
DGVOrders.DataSource = BindingSource1
DGVOrders.ClearSelection()
con.Close()
DGVOrders.Columns(0).Visible = False
DGVOrders.Columns(1).HeaderText = "Order No"
DGVOrders.Columns(2).HeaderText = "Cust Code"
DGVOrders.Columns(3).HeaderText = "Name"
DGVOrders.Columns(4).HeaderText = "Order Date"
DGVOrders.Columns(5).HeaderText = "Order Price"
DGVOrders.Columns(6).HeaderText = "Total Payment"
DGVOrders.Columns(7).HeaderText = "Dues"
For i = 0 To DGVOrders.RowCount - 1
If DGVOrders.Rows(i).Cells(7).Value > 0 Then
DGVOrders.Rows(i).Cells(7).Style.ForeColor = System.Drawing.Color.Red
Else
DGVOrders.Rows(i).Cells(7).Style.ForeColor = System.Drawing.Color.Green
End If
Next
' CHECK BOX
Dim colmnchk As New DataGridViewCheckBoxColumn
colmnchk.DataPropertyName = "chkSelect"
colmnchk.HeaderText = "SELECT"
colmnchk.Name = "chkSelect"
DGVOrders.Columns.Add(colmnchk)
For i = 0 To DGVOrders.RowCount - 1
Next
'CHECK BOX END
End Sub
Private Sub DGVOrders_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGVOrders.CellValueChanged
If DGVOrders.Columns(e.ColumnIndex).Name = "chkSelect" Then
Dim checkCell As DataGridViewCheckBoxCell = _
CType(DGVOrders.Rows(e.RowIndex).Cells("chkSelect"), _
DataGridViewCheckBoxCell)
If checkCell.Value = True Then
PaymentTable.Rows.Add(DGVOrders.Rows(e.RowIndex).Cells(0).Value, DGVOrders.Rows(e.RowIndex).Cells(7).Value)
Else
Dim toRemoveID As Integer = DGVOrders.Rows(e.RowIndex).Cells(0).Value
For i = 0 To PaymentTable.Rows.Count - 1
If PaymentTable.Rows(i).Item(0) = toRemoveID Then
PaymentTable.Rows(i).Delete()
Exit Sub
End If
Next
End If
DataGridView1.DataSource = PaymentTable
End If
End Sub
Can sombody to solve the issue, or is there any other good method if my code is wrong ?
Try using a different event like CellContentClick and testing for the ColumnIndex. The drawback with this is the event will fire whenever you click on any cell.
Private Sub DGVOrders_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DGVOrders.CellContentClick
'Only interested in the CheckBox column
If e.ColumnIndex = colmnchk.Index Then
Debug.WriteLine("In DGVOrders_CellContentClick for the checkbox")
End If
End Sub

vb.net - Add Databinding to a ToolstripComboBox

i just managed to get my ToolstripComboBox to display a Datasource (Dictionary).
But now i want to add a DataBinding to the SelectedValue Property, but i doesn't work.
For a normal ComboBox it works :/..
My Code:
tscbb_Test.ComboBox.DataBindings.Add("SelectedValue", My.Settings, "Setting from mySettings")
Can somebody help?
I just tried this and it worked perfectly for me:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim table As New DataTable
With table.Columns
.Add("ID", GetType(Integer))
.Add("Name", GetType(String))
End With
With table.Rows
.Add(1, "Peter")
.Add(2, "Paul")
.Add(3, "Mary")
.Add(4, "John")
End With
Me.BindingSource1.DataSource = table
Me.BindingSource2.DataSource = table
With Me.ToolStripComboBox1.ComboBox
.DisplayMember = "Name"
.ValueMember = "ID"
.DataSource = Me.BindingSource1
.DataBindings.Add("SelectedValue", My.Settings, "ToolStripSelectedValue")
End With
With Me.ComboBox1
.DisplayMember = "Name"
.ValueMember = "ID"
.DataSource = Me.BindingSource2
.DataBindings.Add("SelectedValue", My.Settings, "FormSelectedValue")
End With
End Sub
I could run the project, select a different item in each ComboBox, close the project, run it again and the items I previously selected were selected again, indicating that the settings must have been saved.
I just tried this code with two settings of type Keys and it worked as expected:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim keys = [Enum].GetValues(GetType(Keys))
Me.BindingSource1.DataSource = keys
Me.BindingSource2.DataSource = keys
With Me.ToolStripComboBox1.ComboBox
.DataSource = Me.BindingSource1
.DataBindings.Add("SelectedItem", My.Settings, "ToolStripSelection")
End With
With Me.ComboBox1
.DataSource = Me.BindingSource2
.DataBindings.Add("SelectedItem", My.Settings, "FormSelection")
End With
End Sub