?
Solved

Overriding OnKeyPress for a Inherited Textbox

Posted on 2005-04-03
2
Medium Priority
?
624 Views
Last Modified: 2008-03-03
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
Comment
Question by:Shemmie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 14

Accepted Solution

by:
amyhxu earned 2000 total points
ID: 13692400
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
 

Author Comment

by:Shemmie
ID: 13693222
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question