Closed Thread
Results 1 to 6 of 6

Thread: Button Click

  1. #1
    Lop's Avatar
    Lop
    Lop is offline Speaks fluent binary
    Join Date
    May 2006
    Posts
    1,178
    Rep Power
    30

    Button Click

    Button.PerformClick();
    or
    Button_Click(null,null);

    Which method do you use?

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    moonrise is offline Learning Programmer
    Join Date
    May 2006
    Posts
    40
    Rep Power
    0
    Button_Click(null,null);
    is the method i use

  4. #3
    Crane's Avatar
    Crane is offline Programming Expert
    Join Date
    Nov 2005
    Posts
    398
    Rep Power
    25
    Quote Originally Posted by moonrise
    Button_Click(null,null);
    is the method i use
    Same here.

  5. #4
    brackett is offline Programmer
    Join Date
    May 2006
    Posts
    192
    Rep Power
    22
    First off, why do you have any code in your button event handler? If it was in a (hopefully well named) method, you could just call it instead.
    But, okay, you've got it in your event handler. Well, you should still probably call Button.PerformClick. Why?
    For starters, you may be on another thread. And calling the event handler directly isn't going to marshal the call to the UI thread. That may or may not be a problem depending on your handler code.
    Secondly, you're passing null as the sender parameter (and the eventargs, but nobody uses the eventargs anyway). If the handler is expecting to use that parameter (perhaps you have the same button handler for multiple buttons, and it's expected to get the calling button from the sender parameter), now it'll get a null reference exception - not good.
    Thirdly, what if there's another event handler attached to the Button.Click event? By calling your handler directly, the other handler(s) won't be fired. Maybe this is what you want, but probably not.
    So, to answer the question: if I use it, I'd do Button.PerformClick() - but I'd try not to use it to begin with.

  6. #5
    Jordan Guest
    Quote Originally Posted by brackett
    First off, why do you have any code in your button event handler? If it was in a (hopefully well named) method, you could just call it instead.
    But, okay, you've got it in your event handler. Well, you should still probably call Button.PerformClick. Why?
    For starters, you may be on another thread. And calling the event handler directly isn't going to marshal the call to the UI thread. That may or may not be a problem depending on your handler code.
    Secondly, you're passing null as the sender parameter (and the eventargs, but nobody uses the eventargs anyway). If the handler is expecting to use that parameter (perhaps you have the same button handler for multiple buttons, and it's expected to get the calling button from the sender parameter), now it'll get a null reference exception - not good.
    Thirdly, what if there's another event handler attached to the Button.Click event? By calling your handler directly, the other handler(s) won't be fired. Maybe this is what you want, but probably not.
    So, to answer the question: if I use it, I'd do Button.PerformClick() - but I'd try not to use it to begin with.
    Nice post Brackett.

    I think what is being said here is you click a button such as button1
    button1_click() {
    params = true;
    callFunction(params);
    }

    So when you need to execute this again from inside your code such as ran with a param you can instead call the button1

    button1.performclick(); instead of retyping
    params = true;
    callfunction(params);

    With the above example it is hard to see why you would call the button function instead of just callfunction(true). button clicks that call multiple functions with larges amounts of data that require information based on click location or whatever may need code insde of the button function.

    That being said, I see both ways as beneficial and I do not see a reason why you would or wouldn't put code inside of the button function - that is what it is for, correct?

  7. #6
    brackett is offline Programmer
    Join Date
    May 2006
    Posts
    192
    Rep Power
    22
    Quote Originally Posted by Jordan
    Nice post Brackett.

    I think what is being said here is you click a button such as button1
    button1_click() {
    params = true;
    callFunction(params);
    }

    So when you need to execute this again from inside your code such as ran with a param you can instead call the button1

    button1.performclick(); instead of retyping
    params = true;
    callfunction(params);

    With the above example it is hard to see why you would call the button function instead of just callfunction(true). button clicks that call multiple functions with larges amounts of data that require information based on click location or whatever may need code insde of the button function.

    That being said, I see both ways as beneficial and I do not see a reason why you would or wouldn't put code inside of the button function - that is what it is for, correct?
    That's way too UI centric for me. Except in quick 'n' dirty situations, I'd prefer that any real logic be handled by a business object (or, at the very least, a decently named method in the Form) - not in a UI event handler. You get the benefit of reusable business objects containing all the "processing" logic, easier testing of your methods (automated testing of a GUI is pain that I really don't want to get any better at ), decent names, and being able to swap out your GUI code (relatively) effortlessly. The UI event handler only serves as a notification that a user clicked the button, and the Form code maps that button click to a business method.
    Basically, the only thing a Button.Click event handler has access to is the sender and an empty EventArgs parameter. Nothing useful can be gleaned from that that the business object would need. In your example of grabbing the click location, calling the event handler directly wouldn't help - as the user didn't actually click.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Change button and link after click
    By ifeelcash in forum JavaScript and CSS
    Replies: 2
    Last Post: 02-28-2011, 08:05 PM
  2. Simulate a button click?
    By bluemasterflows in forum Java Help
    Replies: 1
    Last Post: 12-23-2010, 02:45 PM
  3. Help Click a Button on a web page?
    By lobfredd in forum Visual Basic Programming
    Replies: 2
    Last Post: 05-15-2010, 06:47 AM
  4. Anyone Know How to Click A Button Using Javascript?
    By tradingjamie in forum JavaScript and CSS
    Replies: 1
    Last Post: 11-18-2009, 05:17 PM
  5. click ads on webpage using a button
    By evilhackerz in forum HTML Programming
    Replies: 5
    Last Post: 05-20-2009, 12:18 PM

Tags for this Thread

Bookmarks

Posting Permissions

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