Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to execute the button1_Click event in the other button's Click event or textBox1_TextChanged?

How to execute the button1_Click event in the other button's Click event or textBox1_TextChanged?

Scheduled Pinned Locked Moved C#
tutorialquestion
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mctramp168
    wrote on last edited by
    #1

    There are two buttons (Button1、Button2) and one TextBox in the winform,I have two questions as below: 1、I want to the execute the button1_Click event when clicking the button2_Click? 2、when the text of textbox1 is changed, then call the button1_Click event, how to write in the textBox1_TextChanged? thanks in advance,pls give the codes in detail. (Don't use this method: place all the codes of button1_Click in one method then use this method in button2_Click)

    L 1 Reply Last reply
    0
    • M mctramp168

      There are two buttons (Button1、Button2) and one TextBox in the winform,I have two questions as below: 1、I want to the execute the button1_Click event when clicking the button2_Click? 2、when the text of textbox1 is changed, then call the button1_Click event, how to write in the textBox1_TextChanged? thanks in advance,pls give the codes in detail. (Don't use this method: place all the codes of button1_Click in one method then use this method in button2_Click)

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, I know of 3 solutions: 1. put the logic in a simple method which gets called by all interested handlers, so have button1_Click and textBox1_TextChanged call the simple method; 2. call button1_Click() with 2 dummy arguments; maybe null, null is sufficient; maybe this, new EventArgs() is needed. In both cases, they do not really carry much information. 3. some actions can easily be simulated, try button1.PerformClick() Most of the time I prefer #1. :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


      Prolific encyclopedia fixture proof-reader browser patron addict?
      We all depend on the beast below.


      Y 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, I know of 3 solutions: 1. put the logic in a simple method which gets called by all interested handlers, so have button1_Click and textBox1_TextChanged call the simple method; 2. call button1_Click() with 2 dummy arguments; maybe null, null is sufficient; maybe this, new EventArgs() is needed. In both cases, they do not really carry much information. 3. some actions can easily be simulated, try button1.PerformClick() Most of the time I prefer #1. :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        Prolific encyclopedia fixture proof-reader browser patron addict?
        We all depend on the beast below.


        Y Offline
        Y Offline
        yu jian
        wrote on last edited by
        #3

        I often us the this.button_click(null, null). Commonly, these two parameters of the method of button_click are no use. :|

        D 1 Reply Last reply
        0
        • Y yu jian

          I often us the this.button_click(null, null). Commonly, these two parameters of the method of button_click are no use. :|

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          That's not the best method to do it. Luc's number 1 response is the best way to do it since the code you write in other places doesn't have to know that it's passing bogus parameters to a method that doesn't even require them.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007, 2008
          But no longer in 2009...

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups