vb.net datagrid combobox
-
I am trying to create an autocomplete combobox for a datagrid in vb.net. I have everything working, except that I cannot force the column to stop when a user presses TAB to enter the column. I need to allow the user to press tab, and then my datagridcolumn must gain focus, but I cannot figure out how :( Anyways, I've attached my code (It's based on some previous articles I have read). I can get the control to retain focus when TAB is pressed if I put a break-point in the OnEnter(ByVal e as System.EventArgs) of the NoKeyUpCombo Class, but not without that. Please help!!!! Jake DataGridComboBoxColumn:
Option Strict Off Option Explicit On Imports Microsoft.VisualBasic Imports System Imports System.ComponentModel Imports System.Data Imports System.Data.Common Imports System.Data.OleDb Imports System.Drawing Imports System.Windows.Forms Namespace DataGridTextBoxCombo ' Step 1. Derive a custom column style from DataGridTextBoxColumn ' a) add a ComboBox member ' b) track when the combobox has focus in Enter and Leave events ' c) override Edit to allow the ComboBox to replace the TextBox ' d) override Commit to save the changed data Public Class DataGridComboBoxColumn Inherits DataGridTextBoxColumn Public WithEvents ColumnComboBox As NoKeyUpCombo Private _source As System.Windows.Forms.CurrencyManager Private _rowNum As Integer Private _isEditing As Boolean Public Shared _RowCount As Integer Public Sub New() _source = Nothing _isEditing = False _RowCount = -1 ColumnComboBox = New NoKeyUpCombo ColumnComboBox.DropDownStyle = ComboBoxStyle.DropDown AddHandler ColumnComboBox.SelectionChangeCommitted, AddressOf ComboStartEditing End Sub 'New Private Sub HandleScroll(ByVal sender As Object, ByVal e As EventArgs) If ColumnComboBox.Visible Then ColumnComboBox.Hide() End If End Sub 'HandleScroll Private Sub ComboStartEditing(ByVal sender As Object, ByVal e As EventArgs) _isEditing = True MyBase.ColumnStartedEditing(sender) End Sub 'ComboMadeCurrent Private Sub CompletionCombo_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles ColumnComboBox.Leave If _isEditing Or ColumnComboBox._isEditing Then SetColumnValueAtRow(_source, _rowNum, Colum
-
I am trying to create an autocomplete combobox for a datagrid in vb.net. I have everything working, except that I cannot force the column to stop when a user presses TAB to enter the column. I need to allow the user to press tab, and then my datagridcolumn must gain focus, but I cannot figure out how :( Anyways, I've attached my code (It's based on some previous articles I have read). I can get the control to retain focus when TAB is pressed if I put a break-point in the OnEnter(ByVal e as System.EventArgs) of the NoKeyUpCombo Class, but not without that. Please help!!!! Jake DataGridComboBoxColumn:
Option Strict Off Option Explicit On Imports Microsoft.VisualBasic Imports System Imports System.ComponentModel Imports System.Data Imports System.Data.Common Imports System.Data.OleDb Imports System.Drawing Imports System.Windows.Forms Namespace DataGridTextBoxCombo ' Step 1. Derive a custom column style from DataGridTextBoxColumn ' a) add a ComboBox member ' b) track when the combobox has focus in Enter and Leave events ' c) override Edit to allow the ComboBox to replace the TextBox ' d) override Commit to save the changed data Public Class DataGridComboBoxColumn Inherits DataGridTextBoxColumn Public WithEvents ColumnComboBox As NoKeyUpCombo Private _source As System.Windows.Forms.CurrencyManager Private _rowNum As Integer Private _isEditing As Boolean Public Shared _RowCount As Integer Public Sub New() _source = Nothing _isEditing = False _RowCount = -1 ColumnComboBox = New NoKeyUpCombo ColumnComboBox.DropDownStyle = ComboBoxStyle.DropDown AddHandler ColumnComboBox.SelectionChangeCommitted, AddressOf ComboStartEditing End Sub 'New Private Sub HandleScroll(ByVal sender As Object, ByVal e As EventArgs) If ColumnComboBox.Visible Then ColumnComboBox.Hide() End If End Sub 'HandleScroll Private Sub ComboStartEditing(ByVal sender As Object, ByVal e As EventArgs) _isEditing = True MyBase.ColumnStartedEditing(sender) End Sub 'ComboMadeCurrent Private Sub CompletionCombo_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles ColumnComboBox.Leave If _isEditing Or ColumnComboBox._isEditing Then SetColumnValueAtRow(_source, _rowNum, Colum
Create a class module and stick this code in it: Imports System Imports System.Data Imports System.Windows.Forms Imports System.Drawing Namespace ComboBoxCode Public Class DataGridComboBoxColumn Inherits DataGridTextBoxColumn Private _comboBox As DataGridComboBox Private _sorce As CurrencyManager Private _rowNumber As Integer Private _editing As Boolean Public Sub New(ByVal dataSource As DataView, _ ByVal displayMember As String, _ ByVal valueMember As String) _sorce = Nothing _editing = False _comboBox = New DataGridComboBox _comboBox.DropDownStyle = ComboBoxStyle.DropDownList _comboBox.Visible = False _comboBox.DataSource = dataSource _comboBox.DisplayMember = displayMember _comboBox.ValueMember = valueMember AddHandler _comboBox.Leave, AddressOf _comboBox_Leave AddHandler _comboBox.SelectionChangeCommitted, _ AddressOf _comboBox_SelectionChangeCommitted End Sub Public ReadOnly Property ComboBox() As DataGridComboBox Get Return _comboBox End Get End Property Private Sub _comboBox_Leave(ByVal sender As Object, _ ByVal e As EventArgs) If _editing Then _editing = False SetColumnValueAtRow(_sorce, _rowNumber, _ _comboBox.Text) Invalidate() End If _comboBox.Visible = False AddHandler DataGridTableStyle.DataGrid.Scroll, _ AddressOf DataGrid_Scroll End Sub Private Sub DataGrid_Scroll(ByVal sender As Object, _ ByVal e As EventArgs) If _comboBox.Visible Then _comboBox.Visible = False End If End Sub Private Sub _comboBox_SelectionChangeCommitted( _ ByVal sender As Object, ByVal e As EventArgs) _editing = True MyBase.ColumnStartedEditing(CType(sender, Control)) End Sub Protected Overrides Function GetMinimumHeight() As Integer Return _comboBox.PreferredHeight End Function Protected Overrides Sub SetDataGridInColumn( _ ByVal value As DataGrid) MyBase.SetDataGridInColumn(value) _comboBox.Parent = CType(v