Results 1 to 8 of 8

Thread: Can a TextBox control contain a history of typed strings?

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2017
    Posts
    54

    Question Can a TextBox control contain a history of typed strings?

    Hi, I would like to add some functionality to a program I made. The program contains a single line text box. I would like to press the down or up arrow key to cycle through the text that has already been entered.

    Would this be something one can enable in the properties of the control?

    Thanks

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Can a TextBox control contain a history of typed strings?

    It could be done, either by inheritance or by handling appropriate events, but my main concern would be how you decide what values to keep. Not the mechanics but the logic.

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2017
    Posts
    54

    Re: Can a TextBox control contain a history of typed strings?

    It's just a history of what was entered in the text box, enter must be pressed for the string to be saved. If the user doesn't want to commit the string to the history, enter is never pressed.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Can a TextBox control contain a history of typed strings?

    You first need to decide whether you want this to be handled by the application or the control itself. If it's just one control then I'd be inclined to do it in the application. If it's something that you want to do in more than one place then you should create your own custom control that inherits TextBox and build the functionality into it. If you might want to do this in multiple applications then should create that control in a library project and then add the control to the Toolbox to be added to any form like any other control.

  5. #5

    Thread Starter
    Member
    Join Date
    Aug 2017
    Posts
    54

    Re: Can a TextBox control contain a history of typed strings?

    Going on what you said, having it handled by the application sounds like what I would do.

    Is this an advanced thing to do? I don't want to get in over my head.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Can a TextBox control contain a history of typed strings?

    Here's a quick example I just knocked up. It could use some polish but most of what you need is there.
    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private ReadOnly textHistory As New List(Of String)
    4.  
    5.     Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
    6.         If e.KeyCode = Keys.Up OrElse e.KeyCode = Keys.Down Then
    7.             Dim allText As New List(Of String)
    8.  
    9.             allText.AddRange(textHistory)
    10.  
    11.             Dim currentText = TextBox1.Text
    12.  
    13.             'Merge the current text with the history and sort.
    14.             If Not allText.Contains(currentText) Then
    15.                 allText.Add(currentText)
    16.                 allText.Sort()
    17.             End If
    18.  
    19.             Dim currentIndex = allText.IndexOf(currentText)
    20.             Dim newIndex As Integer
    21.  
    22.             Select Case e.KeyCode
    23.                 Case Keys.Up
    24.                     'Move up one in the history and wrap to the end.
    25.                     newIndex = If(currentIndex = 0, allText.Count - 1, currentIndex - 1)
    26.                 Case Keys.Down
    27.                     'Move down one in the history and wrap to the beginning.
    28.                     newIndex = If(currentIndex = allText.Count - 1, 0, currentIndex + 1)
    29.             End Select
    30.  
    31.             TextBox1.Text = allText(newIndex)
    32.         End If
    33.     End Sub
    34.  
    35.     Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
    36.         If e.KeyChar = Convert.ToChar(Keys.Return) Then
    37.             'Prevent the application from providing visual and audible feedback.
    38.             e.Handled = True
    39.  
    40.             Dim newText = TextBox1.Text
    41.  
    42.             If Not textHistory.Contains(newText) Then
    43.                 textHistory.Add(newText)
    44.                 textHistory.Sort()
    45.             End If
    46.         End If
    47.     End Sub
    48.  
    49. End Class

  7. #7
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Can a TextBox control contain a history of typed strings?

    It sounds like he probably wants it to work like the command line history does in a cmd window, so it shouldn't sort the strings, just add them to a list in the order they are typed and only when the Enter key is pressed.
    If you hit the Up or Down arrow in the cmd window, whatever you were typing is lost, it isn't added to the history. Only completed commands (i.e. you have to have pressed enter) are stored in the History.

    Keeping the strings in the order you entered them allows easily repeating a series of commands as you can hit a number of up arrow strokes in a row to get back to the beginning of the series, then hit Enter to re-execute the first command. You can then just hit the down arrow and Enter key repeatedly to repeat each successive command that you did before.
    But you've given a good example of how to add items to a list, and perhaps he can add the logic behind keeping an index associated with the arrow key presses, etc... to emulate the way the cmd line maintains and accesses history, if that is what he wants.

  8. #8
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,104

    Re: Can a TextBox control contain a history of typed strings?

    I did something similar to this with an associated List of Strings. I think that's the best way.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width