• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 626
  • Last Modified:

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.
0
Shemmie
Asked:
Shemmie
1 Solution
 
amyhxuCommented:
You don't need to write inherited textbox in this case. You can use existing event of the textbox to handle this. The right event to use is: TextBox.KeyDown (TextBox.KeyUp also works).

    Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown

        If e.KeyCode = Keys.Enter Then
            Console.WriteLine("Keys.Enter")
            EnterFunction()
        ElseIf e.KeyCode = Keys.Up Then
            Console.WriteLine("Key.Up")
            UpFunction()
        ElseIf e.KeyCode = Keys.Down Then
            Console.WriteLine("Key.Down")
            DownFunction()
        End If

    End Sub
0
 
ShemmieAuthor Commented:
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.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now