Click here to Skip to main content
15,909,530 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: database password with vb6? Pin
a_yosef25-Feb-05 20:59
a_yosef25-Feb-05 20:59 
GeneralDHTML Edit Control Pin
Sumit Domyan23-Feb-05 19:27
Sumit Domyan23-Feb-05 19:27 
Generalexporting crystal report to text format Pin
trishalhen0823-Feb-05 12:39
trishalhen0823-Feb-05 12:39 
Generalchanging combo box text Pin
i hate combo boxes23-Feb-05 10:01
sussi hate combo boxes23-Feb-05 10:01 
GeneralRe: changing combo box text Pin
Acheto23-Feb-05 23:40
Acheto23-Feb-05 23:40 
GeneralRe: changing combo box text Pin
Tom John24-Feb-05 2:50
Tom John24-Feb-05 2:50 
GeneralRe: changing combo box text Pin
i hate combo boxes24-Feb-05 4:00
sussi hate combo boxes24-Feb-05 4:00 
Generalvb.net datagrid combobox Pin
jake07223-Feb-05 9:05
jake07223-Feb-05 9:05 
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 Frown | :(

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:
<br />
Option Strict Off<br />
Option Explicit On <br />
<br />
Imports Microsoft.VisualBasic<br />
Imports System<br />
Imports System.ComponentModel<br />
Imports System.Data<br />
Imports System.Data.Common<br />
Imports System.Data.OleDb<br />
Imports System.Drawing<br />
Imports System.Windows.Forms<br />
<br />
Namespace DataGridTextBoxCombo<br />
    ' Step 1. Derive a custom column style from DataGridTextBoxColumn<br />
    '	a) add a ComboBox member<br />
    '  b) track when the combobox has focus in Enter and Leave events<br />
    '  c) override Edit to allow the ComboBox to replace the TextBox<br />
    '  d) override Commit to save the changed data<br />
    Public Class DataGridComboBoxColumn<br />
        Inherits DataGridTextBoxColumn<br />
        Public WithEvents ColumnComboBox As NoKeyUpCombo<br />
        Private _source As System.Windows.Forms.CurrencyManager<br />
        Private _rowNum As Integer<br />
        Private _isEditing As Boolean<br />
        Public Shared _RowCount As Integer<br />
<br />
<br />
        Public Sub New()<br />
            _source = Nothing<br />
            _isEditing = False<br />
            _RowCount = -1<br />
<br />
            ColumnComboBox = New NoKeyUpCombo<br />
            ColumnComboBox.DropDownStyle = ComboBoxStyle.DropDown<br />
<br />
            AddHandler ColumnComboBox.SelectionChangeCommitted, AddressOf ComboStartEditing<br />
        End Sub 'New<br />
<br />
        Private Sub HandleScroll(ByVal sender As Object, ByVal e As EventArgs)<br />
            If ColumnComboBox.Visible Then<br />
                ColumnComboBox.Hide()<br />
            End If<br />
        End Sub 'HandleScroll<br />
<br />
        Private Sub ComboStartEditing(ByVal sender As Object, ByVal e As EventArgs)<br />
            _isEditing = True<br />
            MyBase.ColumnStartedEditing(sender)<br />
        End Sub 'ComboMadeCurrent<br />
<br />
<br />
        Private Sub CompletionCombo_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles ColumnComboBox.Leave<br />
            If _isEditing Or ColumnComboBox._isEditing Then<br />
                SetColumnValueAtRow(_source, _rowNum, ColumnComboBox.Text)<br />
                _isEditing = False<br />
                Invalidate()<br />
<br />
            End If<br />
            ColumnComboBox.Hide()<br />
            AddHandler Me.DataGridTableStyle.DataGrid.Scroll, New EventHandler(AddressOf HandleScroll)<br />
        End Sub 'LeaveComboBox<br />
<br />
<br />
        Protected Overloads Overrides Sub Edit(ByVal [source] As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal bounds As System.Drawing.Rectangle, ByVal [readOnly] As Boolean, ByVal instantText As String, ByVal cellIsVisible As Boolean)<br />
<br />
            MyBase.Edit([source], rowNum, bounds, [readOnly], instantText, cellIsVisible)<br />
<br />
            '_isEditing = True<br />
            _rowNum = rowNum<br />
            _source = [source]<br />
<br />
            ColumnComboBox.Parent = Me.TextBox.Parent<br />
            ColumnComboBox.Location = Me.TextBox.Location<br />
            ColumnComboBox.Size = New Size(Me.TextBox.Size.Width, ColumnComboBox.Size.Height)<br />
            'ColumnComboBox.SelectedIndex = ColumnComboBox.FindStringExact(Me.TextBox.Text)<br />
            ColumnComboBox.Text = Me.TextBox.Text<br />
            Me.TextBox.Visible = False<br />
            ColumnComboBox.Visible = True<br />
            AddHandler Me.DataGridTableStyle.DataGrid.Scroll, AddressOf HandleScroll<br />
<br />
            ColumnComboBox.BringToFront()<br />
            ColumnComboBox.Focus()<br />
        End Sub 'Edit<br />
<br />
<br />
        Protected Overrides Function Commit(ByVal dataSource As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer) As Boolean<br />
<br />
            If _isEditing Then<br />
                _isEditing = False<br />
                SetColumnValueAtRow(dataSource, rowNum, ColumnComboBox.Text)<br />
            End If<br />
            Return True<br />
        End Function 'Commit<br />
<br />
<br />
        Protected Overrides Sub ConcedeFocus()<br />
            Console.WriteLine("ConcedeFocus")<br />
            MyBase.ConcedeFocus()<br />
        End Sub 'ConcedeFocus<br />
<br />
        Protected Overrides Function GetColumnValueAtRow(ByVal [source] As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer) As Object<br />
<br />
            Dim s As Object = MyBase.GetColumnValueAtRow([source], rowNum)<br />
            Dim dv As DataView = CType(Me.ColumnComboBox.DataSource, DataView)<br />
            Dim rowCount As Integer = dv.Count<br />
            Dim i As Integer = 0<br />
            Dim s1 As Object<br />
<br />
            'if things are slow, you could order your dataview<br />
            '& use binary search instead of this linear one<br />
            While i < rowCount<br />
                s1 = dv(i)(Me.ColumnComboBox.ValueMember)<br />
                If (Not s1 Is DBNull.Value) AndAlso _<br />
                    (Not s Is DBNull.Value) AndAlso _<br />
                            s = s1 Then<br />
                    Exit While<br />
                End If<br />
                i = i + 1<br />
            End While<br />
<br />
            If i < rowCount Then<br />
                Return dv(i)(Me.ColumnComboBox.DisplayMember)<br />
            End If<br />
            Return DBNull.Value<br />
        End Function 'GetColumnValueAtRow<br />
<br />
<br />
        Protected Overrides Sub SetColumnValueAtRow(ByVal [source] As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal value As Object)<br />
            Dim s As Object = value<br />
<br />
            Dim dv As DataView = CType(Me.ColumnComboBox.DataSource, DataView)<br />
            Dim rowCount As Integer = dv.Count<br />
            Dim i As Integer = 0<br />
            Dim s1 As Object<br />
<br />
            'if things are slow, you could order your dataview<br />
            '& use binary search instead of this linear one<br />
            While i < rowCount<br />
                s1 = dv(i)(Me.ColumnComboBox.DisplayMember)<br />
                If (Not s1 Is DBNull.Value) AndAlso _<br />
                            s = s1 Then<br />
                    Exit While<br />
                End If<br />
                i = i + 1<br />
            End While<br />
            If i < rowCount Then<br />
                s = dv(i)(Me.ColumnComboBox.ValueMember)<br />
            Else<br />
                s = DBNull.Value<br />
            End If<br />
            MyBase.SetColumnValueAtRow([source], rowNum, s)<br />
        End Sub 'SetColumnValueAtRow <br />
<br />
    End Class 'DataGridComboBoxColumn<br />
<br />
<br />
End Namespace<br />


NoKeyUpCombo.vb:
<br />
Option Strict Off<br />
Option Explicit On <br />
<br />
Imports Microsoft.VisualBasic<br />
Imports System<br />
Imports System.ComponentModel<br />
Imports System.Data<br />
Imports System.Data.Common<br />
Imports System.Data.OleDb<br />
Imports System.Drawing<br />
Imports System.Windows.Forms<br />
<br />
Namespace DataGridTextBoxCombo<br />
    Public Class NoKeyUpCombo<br />
        Inherits ComboBox<br />
        Private WM_KEYUP As Integer = &H101<br />
        Public _isEditing As Boolean = False<br />
        <br />
        Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)<br />
            If m.Msg = WM_KEYUP Then<br />
                'ignore keyup to avoid problem with tabbing & dropdownlist;<br />
                Return<br />
            End If<br />
            MyBase.WndProc(m)<br />
        End Sub 'WndProc<br />
<br />
        Protected Overrides Sub OnEnter(ByVal e As System.EventArgs)<br />
            _isEditing = True<br />
            MyBase.Focus()<br />
        End Sub<br />
<br />
        Private Sub CompletionCombo_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp<br />
            Dim sTypedText As String<br />
            Dim iFoundIndex As Integer<br />
            Dim oFoundItem As Object<br />
            Dim sFoundText As String<br />
            Dim sAppendText As String<br />
<br />
            If Me.Items.Count > 0 Then<br />
                'Allow select keys without Autocompleting<br />
                Select Case e.KeyCode<br />
                    Case Keys.Back, Keys.Left, Keys.Right, Keys.Up, Keys.Delete, Keys.Down, Keys.ControlKey, Keys.ShiftKey, Keys.Alt, Keys.ShiftKey, Keys.MButton, Keys.LButton, Keys.RButton, Keys.Home, Keys.End<br />
                        'e.Handled = False<br />
                        Exit Sub<br />
                End Select<br />
<br />
                'Get the Typed Text and Find it in the list<br />
                If Me.Text.Length > 0 And Me.Text.Length <> Me.SelectedText.Length Then<br />
                    sTypedText = Me.Text<br />
                    iFoundIndex = Me.FindString(sTypedText)<br />
<br />
                    'If we found the Typed Text in the list then Autocomplete<br />
                    If iFoundIndex >= 0 Then<br />
                        'Get the Item from the list (Return Type depends if Datasource was bound <br />
                        ' or List Created)<br />
                        oFoundItem = Me.Items(iFoundIndex)<br />
<br />
                        'Use the ListControl.GetItemText to resolve the Name in case the Combo <br />
                        ' was Data bound<br />
                        sFoundText = Me.GetItemText(oFoundItem)<br />
<br />
                        'Append then found text to the typed text to preserve case<br />
                        sAppendText = sFoundText.Substring(sTypedText.Length)<br />
                        Me.Text = sTypedText & sAppendText<br />
<br />
                        'Select the Appended Text<br />
                        Me.SelectionStart = sTypedText.Length<br />
                        Me.SelectionLength = sAppendText.Length<br />
                        'Me.SelectedIndex = iFoundIndex<br />
                    End If<br />
                End If<br />
            End If<br />
<br />
        End Sub<br />
<br />
        Private Sub CompletionCombo_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Leave<br />
            Dim iFoundIndex As Integer<br />
<br />
            If Me.Items.Count > 0 Then<br />
                Dim sFoundText As String = ""<br />
                If Me.SelectedText.Length <> Me.Text.Length Then<br />
                    sFoundText = Me.Text.Substring(Me.SelectedText.Length, Me.Text.Length)<br />
                End If<br />
                If sFoundText.Length > 0 Then<br />
                    iFoundIndex = Me.FindString(Me.Text)<br />
<br />
                    Me.SelectedIndex = iFoundIndex<br />
                End If<br />
            End If<br />
<br />
            ' Now ask to make a new address, if there is no autocomplete.<br />
            If MyBase.SelectedIndex = -1 And MyBase.Text.Length > 0 Then<br />
                If MessageBox.Show("Cannot find item: '" & MyBase.Text & "'." & vbCrLf & "Would you like to create a new item?", "Create a New Entry?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then<br />
                    ' TODO: Add code to make a new inventory item.<br />
                Else<br />
                    MyBase.Text = ""<br />
                    MyBase.Focus()<br />
                End If<br />
            End If<br />
        End Sub<br />
    End Class 'NoKeyUpCombo<br />
End Namespace<br />
<br />

GeneralRe: vb.net datagrid combobox Pin
j45mw12-Mar-05 17:35
j45mw12-Mar-05 17:35 
GeneralVB.NET: Diagramm but no Excel! Pin
Acheto23-Feb-05 7:31
Acheto23-Feb-05 7:31 
GeneralSessions lost Pin
partt23-Feb-05 7:25
partt23-Feb-05 7:25 
GeneralRe: Sessions lost Pin
Colin Angus Mackay24-Feb-05 0:17
Colin Angus Mackay24-Feb-05 0:17 
GeneralDouble problem: dynamic adding with autoscroll and dynamic removing Pin
Nanaki2k23-Feb-05 7:14
Nanaki2k23-Feb-05 7:14 
GeneralRe: Double problem: dynamic adding with autoscroll and dynamic removing Pin
Nanaki2k23-Feb-05 8:55
Nanaki2k23-Feb-05 8:55 
Generaltext box value to double Pin
dyerstein23-Feb-05 7:00
dyerstein23-Feb-05 7:00 
GeneralRe: text box value to double Pin
Jim Matthews23-Feb-05 7:29
Jim Matthews23-Feb-05 7:29 
GeneralRe: text box value to double Pin
dyerstein23-Feb-05 7:46
dyerstein23-Feb-05 7:46 
GeneralRe: text box value to double Pin
Jim Matthews23-Feb-05 9:14
Jim Matthews23-Feb-05 9:14 
GeneralRe: text box value to double Pin
dyerstein23-Feb-05 9:45
dyerstein23-Feb-05 9:45 
GeneralRe: text box value to double Pin
Christian Graus23-Feb-05 9:39
protectorChristian Graus23-Feb-05 9:39 
Generalvb.net and sql 2000 Pin
Member 107391923-Feb-05 6:16
Member 107391923-Feb-05 6:16 
GeneralRe: vb.net and sql 2000 Pin
Colin Angus Mackay24-Feb-05 0:24
Colin Angus Mackay24-Feb-05 0:24 
GeneralRe: vb.net and sql 2000 Pin
Member 107391924-Feb-05 8:32
Member 107391924-Feb-05 8:32 
Generalowner drawn combo box focus rectangle Pin
Anonymous23-Feb-05 6:06
Anonymous23-Feb-05 6:06 
QuestionUSB Card Reader. How to read data from usb into string? Pin
Member 116828123-Feb-05 5:02
Member 116828123-Feb-05 5:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.