Problems with the Return key

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BowserYo
    New Member
    • Jul 2008
    • 4

    Problems with the Return key

    Hi! I'm very new to Visual Basic Express 2008 and I am trying to get a basic knowledge of the program so I took the tutorial that shows you how to make a basic webbrowser. Well I took it a little farther and added buttons like go forward/back refresh ect..

    The problem I am having is this: When im typing in a web address and press the return key I want that to be the same as pressing the go button... Here is the code I have and it doesn't say anything is wrong it just doesn't work.

    Code:
    Private Sub TextBox1_KeyPress(ByVal KeyAscii As Integer)
            If KeyAscii = 13 Then WebBrowser1.Navigate(TextBox1.Text)
        End Sub
    Here is my entire code incase this helps

    Code:
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            WebBrowser1.Navigate(TextBox1.Text)
        End Sub
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            WebBrowser1.Refresh()
        End Sub
    
        Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
            WebBrowser1.GoForward()
        End Sub
    
        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            WebBrowser1.GoBack()
        End Sub
    
        Private Sub TextBox1_KeyPress(ByVal KeyAscii As Integer)
            If KeyAscii = 13 Then WebBrowser1.Navigate(TextBox1.Text)
        End Sub
    
    End Class
    I've read stuff about "focus" but I'm not sure how to use that or if I even need to.

    Please I have been having a LOT of trouble with this so any help will be awesome!
  • BowserYo
    New Member
    • Jul 2008
    • 4

    #2
    Did I post in the wrong forum or something?

    Comment

    • debasisdas
      Recognized Expert Expert
      • Dec 2006
      • 8119

      #3
      question moved to .NET forum.

      Comment

      • DrBunchman
        Recognized Expert Contributor
        • Jan 2008
        • 979

        #4
        Hi BowserYo,

        Have you defined this subroutine as an event anywhere? How does your code know that it is supposed to fire this sub when you press the enter key?

        An easier way to achieve what you want would probably be to put TextBox1 and Button1 inside a Panel and set the DefaultButton property of the panel to the ID of Button1. Doing so means that while the panel has focus (i.e. while the user is clicking or typing in a control inside that panel) then pressing return will always act like you have clicked on Button1. Your Button1_Click event will then fire and redirect your user.

        Here's an example of what I'm talking about.
        Code:
         
        <asp:Panel ID="Panel1" DefaultButton="Button1" runat="Server>
        	 <asp:Textbox ID="TextBox1" runat="Server" />
        	 <asp:Button ID="Button1" Text="Click Me!" runat="Server" />
        </asp:Panel>
        Does this make sense? Let me know if this helps.

        Dr B

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          This is a windows application yes?
          Your event handler routine does not match the correct keypress event handler
          [code=vbnet]
          Private Sub TextBox1_KeyPre ss(ByVal sender As object, ByVal e As KeyPressEventAr gs)
          [/code]

          Comment

          • BowserYo
            New Member
            • Jul 2008
            • 4

            #6
            Okay, They are all in a panel now... But im not 100 percent sure how I set the default button, I tried adding your code and it did not work. I also looked in the Properties of the panel but could not find a default button property. And yes this is a windows form app. But you understand what I am trying to do at least.

            Plater, I am very new to this program so what you said just went over my head... Sorry guys I'm just trying to learn!

            Comment

            • DrBunchman
              Recognized Expert Contributor
              • Jan 2008
              • 979

              #7
              BowserYo,

              Ignore me, I mistakenly read that you were using Visual Web Developer Express - the advice I gave you would only be relevant for a webpage. Apologies for any confusion!

              I'm sure Plater will be able to help you out.

              Dr B

              Comment

              • MrMancunian
                Recognized Expert Contributor
                • Jul 2008
                • 569

                #8
                You wrote just a sub, without telling the program when to use it. You can fix that like this:

                Code:
                Private Sub TextBox1_KeyPress(ByVal sender As object, ByVal e As KeyPressEventArgs) [B]Handles Textbox1.KeyPress[/B]
                  If e.KeyCode = Keys.Return Then 
                     WebBrowser1.Navigate(TextBox1.Text)
                  End If
                End Sub
                Now, the program knows that when you hit a key in the textbox, it should check whether Return has been hit.

                Steven

                Comment

                • BowserYo
                  New Member
                  • Jul 2008
                  • 4

                  #9
                  Originally posted by MrMancunian
                  You wrote just a sub, without telling the program when to use it. You can fix that like this:

                  Code:
                  Private Sub TextBox1_KeyPress(ByVal sender As object, ByVal e As KeyPressEventArgs) [B]Handles Textbox1.KeyPress[/B]
                    If e.KeyCode = Keys.Return Then 
                       WebBrowser1.Navigate(TextBox1.Text)
                    End If
                  End Sub
                  Now, the program knows that when you hit a key in the textbox, it should check whether Return has been hit.

                  Steven
                  Okay, its having some problems with "e.KeyCode"

                  Error 1 'KeyCode' is not a member of 'System.Windows .Forms.KeyPress EventArgs'.

                  Thanks all for taking the time to try and help me.

                  Comment

                  • Plater
                    Recognized Expert Expert
                    • Apr 2007
                    • 7872

                    #10
                    Originally posted by BowserYo
                    Okay, its having some problems with "e.KeyCode"

                    Error 1 'KeyCode' is not a member of 'System.Windows .Forms.KeyPress EventArgs'.

                    Thanks all for taking the time to try and help me.
                    It's KeyChar for that object.
                    You should try looking at these objects yourself. Use the intellisense on objects, it can very usefull.

                    Comment

                    Working...