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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
Are you looking for auto-complete with a ComboBox?  That is achievable with the native control.
0
CYBERCICCIOAuthor Commented:
How ?
0
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
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

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
Bob LearnedCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.