PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2013 [RESOLVED] Sending words from textbox to listbox when hitting enter-VBForums
Results 1 to 12 of 12

Thread: [RESOLVED] Sending words from textbox to listbox when hitting enter

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2006
    Location
    Minnesota
    Posts
    81

    Resolved [RESOLVED] Sending words from textbox to listbox when hitting enter

    So, I'm stuck on this part of my program and I'm not getting anywhere.

    This is what I want to do:
    I have a textbox called tbInput and another (textbox or listbox) not sure which is better for what I want to do yet, and when a certain word is entered in tbInput it sends the word to a sub routine after hitting the enter key.

    Code:
    Private Sub tbInput_KeyDown(sender As Object, e As KeyEventArgs) Handles tbInput.KeyDown
            If e.KeyCode = Keys.Enter Then
                If tbInput.Text <> "" Then
                    Output(tbInput.Text)
                End If
                tbInput.Text = ""
            End If
    End Sub
    
    Private Sub Output(ByVal s As String)
            Select Case s
                Case "ping"
                    Ping(s)
                Case "connect"
                    Connect(s)
            End Select
    End Sub
    
    Private Sub Ping(s As String)
            ListBox2.Items.Add(">> " & "Ping failed." & Environment.NewLine)
            tbInput.Text = ""
    End Sub
    
    Private Sub Connect(s As String)
            ListBox2.Items.Add(">> " & "Connect failed." & Environment.NewLine)
            tbInput.Text = ""
    End Sub
    It will grab the correct case and add it to listbox2, but it only works once. It will not loop through it again after entering a new word and hitting enter. What is confusing me is if I just do the code below, it will keep adding the word in the listbox.

    Code:
    Private Sub Output(ByVal s As String)
            ListBox2.Items.Add(s & Environment.NewLine)
            tbInput.Text = ""
    End Sub
    Thanks
    Last edited by tbutts; Feb 25th, 2015 at 04:51 PM.
    "Crowded elevator always smell different to midget."

  2. #2
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    311

    Re: Sending words from textbox to listbox when hitting enter

    Are these all under the Form's class? For example, does it look similar to:
    Code:
    Public Class Form1
        Private Sub tbInput_KeyDown(sender As Object, e As KeyEventArgs) Handles tbInput.KeyDown
            If e.KeyCode = Keys.Enter Then
                If tbInput.Text <> "" Then
                    Output(tbInput.Text)
                End If
                tbInput.Text = ""
            End If
        End Sub
    
        Private Sub Output(ByVal s As String)
            Select Case s
                Case "ping"
                    Ping(s)
                Case "connect"
                    Connect(s)
            End Select
        End Sub
    
        Private Sub Ping(s As String)
            ListBox2.Items.Add(">> " & "Ping failed." & Environment.NewLine)
            tbInput.Text = ""
        End Sub
    
        Private Sub Connect(s As String)
            ListBox2.Items.Add(">> " & "Connect failed." & Environment.NewLine)
            tbInput.Text = ""
        End Sub
    End Class
    Because this is what I did and didn't have any problems with having it work multiple times....

    BTW, your code is a little redundant with tbInput.Text = "" appearing in the KeyDown event-handler as well as in the Ping and Connect functions. Also, you don't need to add a NewLine character at the end of the String that you add to the ListBox as each .Add will put the String on its own line. Finally there isn't really a point to concatenate ">> " with another string constant like "Ping Failed."; you just need to use ListBox2.Items.Add(">> Ping failed.").

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2006
    Location
    Minnesota
    Posts
    81

    Re: Sending words from textbox to listbox when hitting enter

    Yes it is all under the Form's class.

    I know I have redundancy and extra stuff in there. I have so many revisions trying to rule things out why it's not working. I will clean it up when I get it working how I want it. Environment.NewLine point - ok.
    "Crowded elevator always smell different to midget."

  4. #4
    Frenzied Member Bulldog's Avatar
    Join Date
    Jun 2005
    Location
    South UK
    Posts
    1,950

    Re: Sending words from textbox to listbox when hitting enter

    As far as I can see your code will work as expected. If you type "ping" or "connect" in the TextBox, the chosen string will be appended to the ListBox.

    Each time you type "ping" the ListBox will add another line. When you say it does not "loop through again", what do you mean by that?, do you not see it added again?

    As written, the word "ping" and "connect" must be in the correct case. If you enter some other word then nothing will happen.


    • If my post helped you, please Rate it
    • If your problem is solved please also mark the thread resolved

    I use VS2015 (unless otherwise stated).
    _________________________________________________________________________________
    B.Sc(Hons), AUS.P, C.Eng, MIET, MIEEE, MBCS / MCSE+Sec, MCSA+Sec, MCP, A+, Net+, Sec+, MCIWD, CIWP, CIWA
    I wrote my very first program in 1979, using machine code on a mechanical Olivetti teletype connected to an 8-bit, 78 instruction, 1MHz, Motorola 6800 multi-user system with 2k of memory. Using Windows, I dont think my situation has improved.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Sep 2006
    Location
    Minnesota
    Posts
    81

    Re: Sending words from textbox to listbox when hitting enter

    Correct. It doesn't add it on a new line. I have the case set to lower the case if there are any letters in caps in the properties so that is covered.

    Again, it adds the line >> Ping failed. the first time you do it, but if you do it again it doesn't add it again on a new line. Every time you hit enter, it should go through Output, then through the correct sub, but it's not. I'm baffled.
    "Crowded elevator always smell different to midget."

  6. #6
    Frenzied Member Bulldog's Avatar
    Join Date
    Jun 2005
    Location
    South UK
    Posts
    1,950

    Re: Sending words from textbox to listbox when hitting enter

    Well that's very odd, I don't see why that should be. Have you stepped through it in debug mode?

    Also, is this your entire code? I think something else may be interfering.

    If this is your entire code, open a new project, place down a textbox (TextBox1) and a listbox (ListBox1), double click the form and paste this in. (This is what I did and it works).

    Code Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.  
    5.     End Sub
    6.     Private Sub tbInput_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles TextBox1.KeyDown
    7.         If e.KeyCode = Keys.Enter Then
    8.             If TextBox1.Text <> "" Then
    9.                 Output(TextBox1.Text)
    10.             End If
    11.             TextBox1.Text = ""
    12.         End If
    13.     End Sub
    14.  
    15.     Private Sub Output(ByVal s As String)
    16.         Select Case s
    17.             Case "ping"
    18.                 Ping(s)
    19.             Case "connect"
    20.                 Connect(s)
    21.         End Select
    22.     End Sub
    23.  
    24.     Private Sub Ping(ByVal s As String)
    25.         ListBox1.Items.Add(">> " & "Ping failed." & Environment.NewLine)
    26.         TextBox1.Text = ""
    27.     End Sub
    28.  
    29.     Private Sub Connect(ByVal s As String)
    30.         ListBox1.Items.Add(">> " & "Connect failed." & Environment.NewLine)
    31.         TextBox1.Text = ""
    32.     End Sub
    33.  
    34. End Class
    Last edited by Bulldog; Feb 25th, 2015 at 05:38 PM.


    • If my post helped you, please Rate it
    • If your problem is solved please also mark the thread resolved

    I use VS2015 (unless otherwise stated).
    _________________________________________________________________________________
    B.Sc(Hons), AUS.P, C.Eng, MIET, MIEEE, MBCS / MCSE+Sec, MCSA+Sec, MCP, A+, Net+, Sec+, MCIWD, CIWP, CIWA
    I wrote my very first program in 1979, using machine code on a mechanical Olivetti teletype connected to an 8-bit, 78 instruction, 1MHz, Motorola 6800 multi-user system with 2k of memory. Using Windows, I dont think my situation has improved.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Sep 2006
    Location
    Minnesota
    Posts
    81

    Re: Sending words from textbox to listbox when hitting enter

    Oh no, this is just a tiny part.

    I have stepped through it. I checked it using both ping and connect and it will go to the correct sub.

    Interesting that you say interference because after it leaves the sub (when stepping) it does go to another sub not related to this and not under these subs. I need to look into that real quick.
    "Crowded elevator always smell different to midget."

  8. #8
    Frenzied Member Bulldog's Avatar
    Join Date
    Jun 2005
    Location
    South UK
    Posts
    1,950

    Re: Sending words from textbox to listbox when hitting enter

    Yes, step it all the way through and see if something later is affecting it. I often comment out entire chunks of code just to make sure nothing else is interfering.


    • If my post helped you, please Rate it
    • If your problem is solved please also mark the thread resolved

    I use VS2015 (unless otherwise stated).
    _________________________________________________________________________________
    B.Sc(Hons), AUS.P, C.Eng, MIET, MIEEE, MBCS / MCSE+Sec, MCSA+Sec, MCP, A+, Net+, Sec+, MCIWD, CIWP, CIWA
    I wrote my very first program in 1979, using machine code on a mechanical Olivetti teletype connected to an 8-bit, 78 instruction, 1MHz, Motorola 6800 multi-user system with 2k of memory. Using Windows, I dont think my situation has improved.

  9. #9
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,569

    Re: Sending words from textbox to listbox when hitting enter

    What I would suggest doing is adding a button to your form. On the form that it's located on, set that button as the Accept Button. Then the button's click event would look like this:
    Code:
        Private Sub btnSubmit_Click(ByVal sender As Object, ByVal e As KeyEventArgs) Handles btnSubmit.Click
            'Check if:
            '1) The textbox is not empty
            '2) The textbox's text is either 'ping' or 'connect'
            If Not String.IsNullOrWhiteSpace(tbInput.Text) AndAlso (tbInput.Text = "ping" OrElse tbInput.Text = "connect" Then
                'If the conditions are met, add '>>' followed by the textbox's text followed by 'failed.'
                ListBox1.Items.Add(">> " & tbInput.Text & " failed."
            End If
        End Sub

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Sep 2006
    Location
    Minnesota
    Posts
    81

    Re: Sending words from textbox to listbox when hitting enter

    Thank you for the suggestion, but I don't want the user to have to click a button. Plus, these are just the two commands that can be entered. There are many.

    Anyways, driving home I had an AHA moment. tbInput textbox multiline was set to true. I changed it to false and it works as intended. I knew it was something silly like this.

    Thanks for all the help everyone.
    "Crowded elevator always smell different to midget."

  11. #11
    Frenzied Member Bulldog's Avatar
    Join Date
    Jun 2005
    Location
    South UK
    Posts
    1,950

    Re: [RESOLVED] Sending words from textbox to listbox when hitting enter

    Good, but I don't see how that fixes the problem. If Multiline was set to false and should have been true, then that would make more sense.


    • If my post helped you, please Rate it
    • If your problem is solved please also mark the thread resolved

    I use VS2015 (unless otherwise stated).
    _________________________________________________________________________________
    B.Sc(Hons), AUS.P, C.Eng, MIET, MIEEE, MBCS / MCSE+Sec, MCSA+Sec, MCP, A+, Net+, Sec+, MCIWD, CIWP, CIWA
    I wrote my very first program in 1979, using machine code on a mechanical Olivetti teletype connected to an 8-bit, 78 instruction, 1MHz, Motorola 6800 multi-user system with 2k of memory. Using Windows, I dont think my situation has improved.

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Sep 2006
    Location
    Minnesota
    Posts
    81

    Re: [RESOLVED] Sending words from textbox to listbox when hitting enter

    It was set to true because of the annoying ding when enter was clicked, not knowing that it would have this effect.
    "Crowded elevator always smell different to midget."

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width