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. When to use .PerformClick()

When to use .PerformClick()

Scheduled Pinned Locked Moved C#
helpquestion
6 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
    MAW30
    wrote on last edited by
    #1

    When I try to click a button programmatically using .performClick() it only works when the button is in the same class // this works this.buttonStart.PerformClick(); But if I try to use the perform click outside the class it doesn't work // this doesn' work FormCalendarCS FCCS = new FormCalendarCS(); FCCS.buttonStart.PerformClick(); However if I use the following code I can access the button's method as follows, but doesn't click the button: // this works FCCS.buttonStart_Click(sender, e); Why is this so, and how do I click on the button in another class. I appreciate any help, thanks in advance. Mihael

    D D R 3 Replies Last reply
    0
    • M MAW30

      When I try to click a button programmatically using .performClick() it only works when the button is in the same class // this works this.buttonStart.PerformClick(); But if I try to use the perform click outside the class it doesn't work // this doesn' work FormCalendarCS FCCS = new FormCalendarCS(); FCCS.buttonStart.PerformClick(); However if I use the following code I can access the button's method as follows, but doesn't click the button: // this works FCCS.buttonStart_Click(sender, e); Why is this so, and how do I click on the button in another class. I appreciate any help, thanks in advance. Mihael

      D Offline
      D Offline
      DaveyM69
      wrote on last edited by
      #2

      It seems like you have made buttonStart_Click public static in your FormCalendarCS class which is why you can access it (probably not a good idea!), whereas buttonStart is an instance created in the designer of your FormCalendarCS (exactly as it should be, check the contents of the designer.cs file). Creating a new instance of the form will not get you the original button, just a new one on a new form that you haven't made visible so you will never see it. First of all, change buttonStart_Click to private void buttonStart_Click(object sender, EventArgs e), there is no valid reason to have it any other way IMO, and if you have changed it, set the access modifier on buttonStart back to private. Inside FormCalendarCS create this method:

      public void ButtonStartPerformClick()
      {
      buttonStart.PerformClick();
      }

      Inside your form where you first show your FormCalendarCS instance, add a field:

      private FormCalendarCS formCalendarCS;

      Before you show it, save the instance to the field.

      formCalendarCS = new FormCalendarCS();
      formCalendarCS.Show();

      Now whenever you want to perform the click, just call:

      formCalendarCS.ButtonStartPerformClick();

      If the relationships between your classes/forms are more complex, you will need to route the method call(s) appropriately and you may need to use events too if you need to call back up the chain. Your problem boils down to not understanding instance vs static objects and methods. These are a fundemental part of any OOP language including C# so I suggest some study on this area.

      Dave
      Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
      BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

      M 1 Reply Last reply
      0
      • D DaveyM69

        It seems like you have made buttonStart_Click public static in your FormCalendarCS class which is why you can access it (probably not a good idea!), whereas buttonStart is an instance created in the designer of your FormCalendarCS (exactly as it should be, check the contents of the designer.cs file). Creating a new instance of the form will not get you the original button, just a new one on a new form that you haven't made visible so you will never see it. First of all, change buttonStart_Click to private void buttonStart_Click(object sender, EventArgs e), there is no valid reason to have it any other way IMO, and if you have changed it, set the access modifier on buttonStart back to private. Inside FormCalendarCS create this method:

        public void ButtonStartPerformClick()
        {
        buttonStart.PerformClick();
        }

        Inside your form where you first show your FormCalendarCS instance, add a field:

        private FormCalendarCS formCalendarCS;

        Before you show it, save the instance to the field.

        formCalendarCS = new FormCalendarCS();
        formCalendarCS.Show();

        Now whenever you want to perform the click, just call:

        formCalendarCS.ButtonStartPerformClick();

        If the relationships between your classes/forms are more complex, you will need to route the method call(s) appropriately and you may need to use events too if you need to call back up the chain. Your problem boils down to not understanding instance vs static objects and methods. These are a fundemental part of any OOP language including C# so I suggest some study on this area.

        Dave
        Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

        M Offline
        M Offline
        MAW30
        wrote on last edited by
        #3

        Thanks Dave, I took your advice and did as you suggested, however, the button isn't static but was changed to public. I changed it back to private and accessed the button as you said. I also understand what you mean by a new instance of that class, I seem to forget that as I have did that more than once, only to realize my mistake. Michael

        1 Reply Last reply
        0
        • M MAW30

          When I try to click a button programmatically using .performClick() it only works when the button is in the same class // this works this.buttonStart.PerformClick(); But if I try to use the perform click outside the class it doesn't work // this doesn' work FormCalendarCS FCCS = new FormCalendarCS(); FCCS.buttonStart.PerformClick(); However if I use the following code I can access the button's method as follows, but doesn't click the button: // this works FCCS.buttonStart_Click(sender, e); Why is this so, and how do I click on the button in another class. I appreciate any help, thanks in advance. Mihael

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

          I've never found a reason to use PerformClick, ever. The code you put in the event handler really needs to be moved to it's own method. Then you can call it from the button Click event handler or from any outside caller without even touching the button.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak

          D 1 Reply Last reply
          0
          • D Dave Kreskowiak

            I've never found a reason to use PerformClick, ever. The code you put in the event handler really needs to be moved to it's own method. Then you can call it from the button Click event handler or from any outside caller without even touching the button.

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak

            D Offline
            D Offline
            DaveyM69
            wrote on last edited by
            #5

            I've never had a reason to use it either - the only advantage is it actually performs the click and so raises any other handlers attached to the Button's Click event whereas calling the handling code would not do that.

            Dave
            Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
            BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

            1 Reply Last reply
            0
            • M MAW30

              When I try to click a button programmatically using .performClick() it only works when the button is in the same class // this works this.buttonStart.PerformClick(); But if I try to use the perform click outside the class it doesn't work // this doesn' work FormCalendarCS FCCS = new FormCalendarCS(); FCCS.buttonStart.PerformClick(); However if I use the following code I can access the button's method as follows, but doesn't click the button: // this works FCCS.buttonStart_Click(sender, e); Why is this so, and how do I click on the button in another class. I appreciate any help, thanks in advance. Mihael

              R Offline
              R Offline
              Ravi Bhavnani
              wrote on last edited by
              #6

              MAW30 wrote:

              it only works when the button is in the same class

              Also, PerformClick() won't work if the button is disabled. /ravi

              My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

              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