Not clicking to use a button click

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sliqer
    New Member
    • Apr 2009
    • 4

    Not clicking to use a button click

    Hi there. I need to simulate a button's method (say button2_click) by clicking button 1. what actually needs to happen is when i click button_1, if a check is passed, a method outside the button1_click must performs the button2_click method. I'm a bit of a beginner and I've bn hunting the net for ages now but to no avail.
    any help would be appreciated
  • Bassem
    Contributor
    • Dec 2008
    • 344

    #2
    Did you mean that:
    Code:
            private void button1_Click(object sender, EventArgs e)
            {   // anything to do... .
                button2_Click(sender, e);
                // anything to do... .
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                // anything to do... .
            }

    Comment

    • sliqer
      New Member
      • Apr 2009
      • 4

      #3
      Hi that isnt quite what I'm looking for.I nd it to access an external method.im not sure if it can be done.
      something like this:
      Code:
      private void button1_Click(object sender, EventArgs e) 
      {   // anything to do... . 
      
          externalMethod()
          // anything to do... . 
      } 
        
      void externalMethod()
      {
          [I]//somehow perform this....
          button2_Click(sender, e); [/I]
      }      
      
      private void button2_Click(object sender, EventArgs e) 
      { 
           // anything to do... . 
      }
      Last edited by PRR; Apr 17 '09, 07:09 AM. Reason: Please post code in [code] [/code] tags.

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        By "outside method" are you saying you want your program to click the button of a different program? You want to fool the other program into thinking a human clicked it's button?

        Comment

        • sliqer
          New Member
          • Apr 2009
          • 4

          #5
          no no i think that would be a little beyond me :D. i just want to know how to use a click method in a second,differen t method (which isnt itself a click method).so i want to be able to run a method, that as part of its code, performs the click method of a button but the 2 methods are in the same program.i hope that makes it a bit clearer

          Comment

          • tlhintoq
            Recognized Expert Specialist
            • Mar 2008
            • 3532

            #6
            Really the best thing to do is to not code that way.

            Write a method that performs the action you need.
            Have the Click event call that method.
            Have your second method call that method.

            Code:
            Public void  myButton_Click(object sender, eventargs e)
            {
                SaveFile();
            }
            
            public void CompletlyDifferentMethod()
            {
                 SaveFile();
            }
            private void SaveFile()
            {
               // This is the Saving method
            }

            But... If you insist on putting the meat of your code in the button click method and want to call it...

            Code:
            myButton_Click(this, eventargs.empty);
            ... will usually do the trick.

            All the button click does is call a method with two parameters: An object and an eventargs. You are going to do the same this. You send it an object (usually 'this' or 'null' will due) and an eventargs.

            Comment

            • sliqer
              New Member
              • Apr 2009
              • 4

              #7
              ok thanks so much.this seems to be doing the trick.i think the problem is i normally just get a bee in my bonnet and sit down and start typing.i spose i could plan a bit more! thx again

              Comment

              • tlhintoq
                Recognized Expert Specialist
                • Mar 2008
                • 3532

                #8
                Originally posted by sliqer
                ok thanks so much.this seems to be doing the trick.i think the problem is i normally just get a bee in my bonnet and sit down and start typing.i spose i could plan a bit more! thx again
                Planning ahead is more than just good: It's vital. A novelist doesn't start writting at page 1; he creates a storyline, an outline, works out the story arc from beginning to end, marks major events, then fills out the characters and details.

                Do you want to be whacker... or a Software Engineer that actually engineers his software?

                Comment

                Working...