custom control

Hi experts,
I made this class/custom control :
Public Class CustomCBO
    Inherits ComboBox
    Protected Overrides Sub OnKeyPress(ByVal e As KeyPressEventArgs)
        If e.KeyChar = ChrW(13) Then
            SendKeys.Send("{TAB}")
            e.Handled = True
        Else
            MyBase.OnKeyPress(e)
        End If
    End Sub
End Class

And now I'd like to add this behaviour to it ( autofill ) :

    Private Sub ComboBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Cat2_idCBO.KeyPress
        Dim cbo As ComboBox = sender
        If Char.IsControl(e.KeyChar) Then Return
        With cbo
            Dim ToFind As String = .Text.Substring(0, .SelectionStart) & e.KeyChar
            Dim Index As Integer = .FindStringExact(ToFind)
            If Index = -1 Then Index = .FindString(ToFind)
            If Index = -1 Then Return
            .SelectedIndex = Index
            .SelectionStart = ToFind.Length
            .SelectionLength = .Text.Length - .SelectionStart
            e.Handled = True
        End With
    End Sub

 thus I'll have this behavior in all its istance

Thanks
Best regards
CYBERCICCIOAsked:
Who is Participating?
 
Bob LearnedConnect With a Mentor Commented:
Hmmm...you have a custom class, that inherits from ComboBox, and you are trying to get validation to work.

I started with this, and it compiled just fine:


Public Class ComboBoxEx
    Inherits ComboBox
 
    Protected Overrides Sub OnValidating(ByVal e As System.ComponentModel.CancelEventArgs)
        MyBase.OnValidating(e)
    End Sub
 
End Class

Open in new window

0
 
Bob LearnedCommented:
Are you looking for auto-complete with a ComboBox?  That is achievable with the native control.
0
 
CYBERCICCIOAuthor Commented:
How ?
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
Bob LearnedCommented:
Read this, and ask questions if it doesn't make sense:

AutoComplete ComboBox and TextBox
http://blogs.vbcity.com/xtab/archive/2007/05/08/8274.aspx
0
 
CYBERCICCIOAuthor Commented:
So trivial ...
Thanks, but how to manage : accept only items in list ?
0
 
Bob LearnedCommented:
I would handle the Validating event, and display a message indicating that the item is invalid, and set e.Cancel = True.   With an auto-complete ComboBox, you need to be able to type in the text portion of the control, so you can't set the drop-down style to List.
0
 
CYBERCICCIOAuthor Commented:
I must repeat for each occurence of my control :
    Private Sub Cat1_idCBO_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Cat1_idCBO.Validating
        Dim cbo As ComboBox = sender
        Dim Index As Integer = cbo.FindStringExact(cbo.Text)
        If Index = -1 Then
            MsgBox("Select an Item in list")
            e.Cancel = True
        End If
    End Sub

Thus, as at the beginning:
Is  there a way to define the behaviour to the event in the base class ?

Public Class CustomCBO
    Inherits ComboBox
    Protected Overrides Sub OnKeyPress(ByVal e As KeyPressEventArgs)
        If e.KeyChar = ChrW(13) Then
            SendKeys.Send("{TAB}")
            e.Handled = True
        Else
            MyBase.OnKeyPress(e)
        End If
    End Sub
End Class
0
 
Bob LearnedCommented:
Yes, the Validating event handler can be defined in the custom control.  You have done that with the OnKeyPress, and it should be the same with OnValidating.
0
 
CYBERCICCIOAuthor Commented:
If I do this
    Public Class CustomCBO
    Inherits ComboBox
    Protected Overrides Sub onKeypress(ByVal e As KeyPressEventArgs)
        If e.KeyChar = ChrW(13) Then
            SendKeys.Send("{TAB}")
            e.Handled = True
        Else
            MyBase.OnKeyPress(e)
        End If
    End Sub

    Protected Overrides Sub Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs)
        Dim cbo As ComboBox = sender
        Dim Index As Integer = cbo.FindStringExact(cbo.Text)
        If Index = -1 Then
            MsgBox("Select an Item in list")
            e.Cancel = True
        End If
    End Sub
End Class

Compiler error is:
Error      3      sub 'Validating' cannot be declared 'Overrides' because it does not override a sub in a base class.      c:\CustomCBO.vb      12      29      FRMbaseclass

And you know why with combobox the chr(13)  is not cathced, only alphanumeric key ?
0
 
Bob LearnedCommented:
I think that you misunderstood slightly, I was talking about overriding OnValidating, like OnKeyPress.
0
 
CYBERCICCIOAuthor Commented:
It's the same
sub 'OnValidating' cannot be declared 'Overrides' because it does not override a sub in a base class
0
 
CYBERCICCIOAuthor Commented:
If I do the suggest : change Overrides in Overload, no error but do nothing.

0
 
CYBERCICCIOAuthor Commented:
Right..
Thanks
The last :
In the same class I try to :
    Protected Overrides Sub onKeypress(ByVal e As KeyPressEventArgs)
        If e.KeyChar = ChrW(13) Then
            SendKeys.Send("{TAB}")
            e.Handled = True
        Else
            MyBase.OnKeyPress(e)
        End If
    End Sub

but It doesn't enter here whene pressing enter key, only for alfanumeric keys.
I do the same thing in this class:
Public Class CustomTextBox
    Inherits TextBox
    Protected Overrides Sub OnKeyPress(ByVal e As KeyPressEventArgs)
        If e.KeyChar = ChrW(13) Then
            SendKeys.Send("{TAB}")
            e.Handled = True
        Else
            MyBase.OnKeyPress(e)
        End If
    End Sub
End Class
and it function.
Why for the combo not ?

Regards
0
 
CYBERCICCIOAuthor Commented:
Solved and understood :
    Protected Overrides Sub onKeydown(ByVal e As KeyEventArgs)
        If e.KeyCode = Keys.Enter Then
            SendKeys.Send("{TAB}")
            e.Handled = True
        Else
            MyBase.OnKeyDown(e)
        End If
    End Sub
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.