A NewBie Question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Adnann
    New Member
    • Nov 2008
    • 7

    #1

    A NewBie Question

    Hi, I have just started learning C# on my own and I am stuck somewhere. Here is the code.

    Code:
     public partial class frmMain : Form
        {
            public frmMain()
            {
                InitializeComponent();
            }
    
            private void btnGo_Click(object sender, EventArgs e)
            {
                webBrowser1.Navigate(txtUrl.Text);
            }
    In fact it's a web browser based on Web Browser control provided by C#. I just want to trigger the Go button when someone presses Enter Key after typing the url in Text Box i.e. txtUrl. Can anyone help me about this. I read somewhere that I can use e.KeyDown method but I don't know how to reach this function. A help would be highly appreciated. Thanks a lot.
  • balabaster
    Recognized Expert Contributor
    • Mar 2007
    • 798

    #2
    The most straightforward way is to take the code from your button and put it in its own method - I gave my example an arbitrary name of navigate, but call it what you want, then in the TextChanged event of your textbox and the Click event of your button, you call your navigate method:

    Code:
    private void txtUrl_TextChanged(object sender, EventArgs e)
    {
        //The sender argument contains the object that triggered 
        //this event.  We need to cast it from object to its 
        //native type - which is textbox so we can access its 
        //methods and properties.
        navigate(((textbox)sender).Text);
    }
    
    private void btnGo_Click(object sender, EventArgs e) 
    { 
         navigate(txtUrl.Text);
    } 
    
    static void navigate(string path)
    {
        webBrowser1.Navigate(path);         
    }
    Does that make sense?
    Last edited by balabaster; Nov 28 '08, 06:30 PM. Reason: Modified code comments to minimize need for scrolling

    Comment

    • Adnann
      New Member
      • Nov 2008
      • 7

      #3
      I really can't get it, I guess what you are trying to do is to call the code behind the button from the textbox upon pressing the enter key. rigth?

      Comment

      • Adnann
        New Member
        • Nov 2008
        • 7

        #4
        or u mean to create a different routine which would be called both on button click and text change of the textbox.

        Comment

        • balabaster
          Recognized Expert Contributor
          • Mar 2007
          • 798

          #5
          Oops, my bad - I missed the point there entirely... okay, you would do this in the KeyDown and check the keycode of the button pressed. The KeyCode is held in the e argument of the KeyDown event. To save you remembering the keycodes for each key, they're stored in an enumeration called Keys - this will save you a world of time - look for Keys.Enter

          Code:
          static void txtURL_KeyDown(object sender, KeyEventArgs e)
          {
              if (e.KeyCode = Keys.Enter)
              {
                  navigate(((textbox)sender).Text);
              }
          }
          In your navigate method, you should also wrap your navigation code in a try{} catch{} block, and/or check to verify that the string entered into txtURL is a valid URL. This will prevent your application crashing if garbage is entered into the input box.

          Comment

          • Adnann
            New Member
            • Nov 2008
            • 7

            #6
            Yes, that's more like it. Now one little thing, how do I access txtURL_KeyDown method coz all I know is tou doubel click on the control to add code behind it, and it only gives me text change event by default. I'll be done once I'll be able to add this method KeyDown.

            Comment

            • balabaster
              Recognized Expert Contributor
              • Mar 2007
              • 798

              #7
              Originally posted by Adnann
              or u mean to create a different routine which would be called both on button click and text change of the textbox.
              And yes, this is what I meant... in an ideal world, you would never have a block of code inside an event handler (i.e. in your button click event) that doesn't pertain to the handling of the event directly. You would try always to write a separate routine that does the work and call this from within your event handler. This way you have access to run that code from other areas of your application. It can be a pain to raise an event of an object from another object - which is what I suspect you're wanting to do.

              The first instinct as a new developer is to put a bunch of code in the button click event. Now, I have some other event that I want to run this code, so I want to fire the button's click event "remotely" - by remotely I mean from another object - in this case, the textbox's KeyDown event. It can be done, but it would only be considered reasonable coding practice on the rarest of occasions... I can't think of a good reason to do this off the top of my head, but I'm sure someone would argue with me if I said there was never a good reason.

              So, what we do is put the task (by task I mean code) into its own method (the term method can be used interchangeably with routine or function) and call that method from within our events. In this case, I wrote my own navigate method that handles the task. I then call that method from my button's click event and from my textbox's KeyDown event if the Enter key is pressed.

              Comment

              • balabaster
                Recognized Expert Contributor
                • Mar 2007
                • 798

                #8
                Originally posted by Adnann
                Yes, that's more like it. Now one little thing, how do I access txtURL_KeyDown method coz all I know is tou doubel click on the control to add code behind it, and it only gives me text change event by default. I'll be done once I'll be able to add this method KeyDown.
                At the top of your code window, directly below the tabs that contain your file names you will find 2 drop down lists. The left one contains a list of objects on your form - select txtURL. The right one contains a list of methods and events for the currently selected item in the left drop down list. Select KeyDown.

                Upon selecting the method or event in the right drop down box, if that method has already been defined, your cursor will be transported to the definition. If that method has not yet been defined, a method stub will be created in which you can enter your code.

                Comment

                • Adnann
                  New Member
                  • Nov 2008
                  • 7

                  #9
                  Just forgive me for asking too many stupid questions but that's exactly what I was looking for, two drop down lists and I know we can select object and then the methods for them. But on the left one, the only thing I can see is My main form object, no other object is listed there.

                  Comment

                  • balabaster
                    Recognized Expert Contributor
                    • Mar 2007
                    • 798

                    #10
                    Ah yes, this is one of those areas where C# is different than VB... sorry, I forgot about this.

                    Go to design view of your form, select your textbox by clicking it once, now, Look at your properties box where you would normally set each of the properties. Right above it, one of the icons looks like a lightning bolt - click it and you will see a list of events. Find the KeyDown event and enter some text name for the event - something like "KeyDownEve nt" is fine - it's just an arbitrary identifier so whatever you think is appropriate is okay. Hit the enter key and a method stub will be created and you will be transferred to the code view.

                    Comment

                    • Adnann
                      New Member
                      • Nov 2008
                      • 7

                      #11
                      Your patience is awesome man and thank you so much, I just did it. I am really obliged. You brought a smile on my face ;) Thanks.

                      Comment

                      • balabaster
                        Recognized Expert Contributor
                        • Mar 2007
                        • 798

                        #12
                        Glad I could help... happy programming :)

                        Comment

                        • Adnann
                          New Member
                          • Nov 2008
                          • 7

                          #13
                          Thanks a lot, hope to see you around.

                          Comment

                          Working...