Link to home
Start Free TrialLog in
Avatar of CYBERCICCIO
CYBERCICCIO

asked on

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
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Are you looking for auto-complete with a ComboBox?  That is achievable with the native control.
Avatar of CYBERCICCIO
CYBERCICCIO

ASKER

How ?
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
So trivial ...
Thanks, but how to manage : accept only items in list ?
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.
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
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.
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 ?
I think that you misunderstood slightly, I was talking about overriding OnValidating, like OnKeyPress.
It's the same
sub 'OnValidating' cannot be declared 'Overrides' because it does not override a sub in a base class
If I do the suggest : change Overrides in Overload, no error but do nothing.

ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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