Link to home
Start Free TrialLog in
Avatar of Shemmie
Shemmie

asked on

Overriding OnKeyPress for a Inherited Textbox

Hi all,

I've been having some trouble trying to pick up key presses in a text box. I want to detect "Cursor up" and "Cursor down", AKA Keys.Up and Keys.Down.

I've created a class, CommandTextBox, that inherits textbox, and overrides Sub OnKeyPress to try and get around this problem.

The reason I'm doing this is that I wish to replicate Command Prompt funcionality. When the cursor up key is pressed, I want the text box to scroll through previously 'entered' entries, and down to scroll back down the list.

I've got it set up so that I've got a multiline textbox txtWindow, that all data entry is copied to on hitting enter. My "CommandTextBox" is a single line textbox that on hitting Enter sends the data across, before setting its own value to "".

The inherited class:

Public MustInherit Class CommandTextBox

    Inherits TextBox


    Public MustOverride Function EnterFunction()

    Public MustOverride Function UpFunction()


    Public MustOverride Function DownFunction()





    Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
        Select Case Asc(e.KeyChar)

            Case Keys.Enter
                EnterFunction()
                e.Handled = True

            Case Keys.Up
                UpFunction()
                e.Handled = True

            Case Keys.Down
                DownFunction()
                e.Handled = True

        End Select

        MyBase.OnKeyPress(e)

    End Sub

    Public Sub New()

    End Sub
End Class


The idea was that, as I will need to refer to a specifici instance of a normal text box (txtWindow) and I can't refer to an instance from a class definition, I'd have overrideable functions that I could add the specific details to. But that's getting OO mixed up, isn't it? Override applies to sub-classes I create using this class, rather than being able to define the functions when I create an instance?

Urgh...

Any help appreciated.
ASKER CERTIFIED SOLUTION
Avatar of amyhxu
amyhxu

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
Avatar of Shemmie
Shemmie

ASKER

I'd been going crazy trying to find a solution to this, getting more and more complicated. My last effort was a custom User Control and a Class to create the customized textbox... talk about overkill.

That works perfectly, thank you very much for the help.