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. Visual Basic
  4. Detect if any of controls inside a TableLayoutPanel is clicked

Detect if any of controls inside a TableLayoutPanel is clicked

Scheduled Pinned Locked Moved Visual Basic
question
18 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.
  • D dilkonika

    Hello ! Is there any way to detect if any of the controls inside a TableLaoutPanel is clicked ? ( I mean without using Click event for each control ). I try using MouseClick's event of TableLayoutPanel , but this doesn't fire when a control is clicked. Thank you !

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #2

    You need to add an event handler which is connected to each control.

    D 1 Reply Last reply
    0
    • L Lost User

      You need to add an event handler which is connected to each control.

      D Offline
      D Offline
      dilkonika
      wrote on last edited by
      #3

      Ok , but what is going to handle this event ? Because if he's going to handle the click action , this is the same as using all control's click event.

      D S 2 Replies Last reply
      0
      • D dilkonika

        Ok , but what is going to handle this event ? Because if he's going to handle the click action , this is the same as using all control's click event.

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

        Yes. You have to wire up the ``Click`` event of each control to a central Click handler to do what you want. It seems like your concern is based on the assumption that only one handler can be assigned to the ``Click`` event. That is NOT the case. Multiple handlers can be assigned to the same event and they will all get executed. So, you CAN have your general ``Click`` event handler for every control that you want to be notified for and you can have individual ``Click`` handlers for each control for specific functionality for whatever a certain button, or whatever controls, is going to do when clicked.

        A guide to posting questions on CodeProject

        Click this: Asking questions is a skill. Seriously, do it.
        Dave Kreskowiak

        D 1 Reply Last reply
        0
        • D dilkonika

          Ok , but what is going to handle this event ? Because if he's going to handle the click action , this is the same as using all control's click event.

          S Offline
          S Offline
          Sascha Lefevre
          wrote on last edited by
          #5

          What's the trouble with using all control's click event?

          D 1 Reply Last reply
          0
          • D Dave Kreskowiak

            Yes. You have to wire up the ``Click`` event of each control to a central Click handler to do what you want. It seems like your concern is based on the assumption that only one handler can be assigned to the ``Click`` event. That is NOT the case. Multiple handlers can be assigned to the same event and they will all get executed. So, you CAN have your general ``Click`` event handler for every control that you want to be notified for and you can have individual ``Click`` handlers for each control for specific functionality for whatever a certain button, or whatever controls, is going to do when clicked.

            A guide to posting questions on CodeProject

            Click this: Asking questions is a skill. Seriously, do it.
            Dave Kreskowiak

            D Offline
            D Offline
            dilkonika
            wrote on last edited by
            #6

            sorry , but I have a central Click event. The problem is that the code inside the click event is depending from the case where the click event is fired from user action ( so a user has clicked ) , or is fired from code. I can't distinguish these 2 cases , then I ask if is possible to detect a click fro the TableLayoutPanel in general.

            D 1 Reply Last reply
            0
            • S Sascha Lefevre

              What's the trouble with using all control's click event?

              D Offline
              D Offline
              dilkonika
              wrote on last edited by
              #7

              The problem is that the code inside the click event is depending from the case where the click event is fired from user action ( so a user has clicked ) , or is fired from code. I can't distinguish these 2 cases , then I ask if is possible to detect a click fro the TableLayoutPanel in general.

              S 1 Reply Last reply
              0
              • D dilkonika

                The problem is that the code inside the click event is depending from the case where the click event is fired from user action ( so a user has clicked ) , or is fired from code. I can't distinguish these 2 cases , then I ask if is possible to detect a click fro the TableLayoutPanel in general.

                S Offline
                S Offline
                Sascha Lefevre
                wrote on last edited by
                #8

                I would say this is the point where you should ask yourself "why is this getting weird?" - and my answer would be "because a previous design decision was wrong/not optimal". I wouldn't trigger the Click-event programmatically. Instead I would move the code from the Click-Eventhandler into a separate method with a parameter that indicates whether it's being called from the Click-Eventhandler (= by a user-click) or from code:

                SomeButton.Click += SomeButtonClicked;

                void SomeButtonClicked(object sender, EventArgs e)
                {
                DoStuff(userClicked: true);
                }

                void SimulateButtonClick()
                {
                DoStuff(userClicked: false);
                }

                void DoStuff(bool userClicked)
                {
                if (userClicked)
                // ...
                else
                // ...
                }

                D 2 Replies Last reply
                0
                • S Sascha Lefevre

                  I would say this is the point where you should ask yourself "why is this getting weird?" - and my answer would be "because a previous design decision was wrong/not optimal". I wouldn't trigger the Click-event programmatically. Instead I would move the code from the Click-Eventhandler into a separate method with a parameter that indicates whether it's being called from the Click-Eventhandler (= by a user-click) or from code:

                  SomeButton.Click += SomeButtonClicked;

                  void SomeButtonClicked(object sender, EventArgs e)
                  {
                  DoStuff(userClicked: true);
                  }

                  void SimulateButtonClick()
                  {
                  DoStuff(userClicked: false);
                  }

                  void DoStuff(bool userClicked)
                  {
                  if (userClicked)
                  // ...
                  else
                  // ...
                  }

                  D Offline
                  D Offline
                  dilkonika
                  wrote on last edited by
                  #9

                  Thank you , but the situation it's a little complicated and I can't use your solution. I have another Event let's say Event2 that is fired when the button's click event is executed. But this event2 can be fired even without button's click event but from another action. And if this case is true , inside the event2 button's click event is called from code. So the events are depending to each other , and I can't find other way but only to distinguish that the click's event is fired by user click or by code.

                  1 Reply Last reply
                  0
                  • S Sascha Lefevre

                    I would say this is the point where you should ask yourself "why is this getting weird?" - and my answer would be "because a previous design decision was wrong/not optimal". I wouldn't trigger the Click-event programmatically. Instead I would move the code from the Click-Eventhandler into a separate method with a parameter that indicates whether it's being called from the Click-Eventhandler (= by a user-click) or from code:

                    SomeButton.Click += SomeButtonClicked;

                    void SomeButtonClicked(object sender, EventArgs e)
                    {
                    DoStuff(userClicked: true);
                    }

                    void SimulateButtonClick()
                    {
                    DoStuff(userClicked: false);
                    }

                    void DoStuff(bool userClicked)
                    {
                    if (userClicked)
                    // ...
                    else
                    // ...
                    }

                    D Offline
                    D Offline
                    dilkonika
                    wrote on last edited by
                    #10

                    I'm thinking , is possible that when I call the click event from code , to insert an special argment on its Args , and inside the click event to check this arg ? Can this be done ?

                    S 1 Reply Last reply
                    0
                    • D dilkonika

                      I'm thinking , is possible that when I call the click event from code , to insert an special argment on its Args , and inside the click event to check this arg ? Can this be done ?

                      S Offline
                      S Offline
                      Sascha Lefevre
                      wrote on last edited by
                      #11

                      Sure, either object sender or EventArgs e or both. I try to avoid doing that because I find it a bit messy but it'll work.

                      D 1 Reply Last reply
                      0
                      • S Sascha Lefevre

                        Sure, either object sender or EventArgs e or both. I try to avoid doing that because I find it a bit messy but it'll work.

                        D Offline
                        D Offline
                        dilkonika
                        wrote on last edited by
                        #12

                        I don't want to change the sender because I need this. Can you explain to me , how can pass a special argument and to check on the code inside ?

                        S 1 Reply Last reply
                        0
                        • D dilkonika

                          I don't want to change the sender because I need this. Can you explain to me , how can pass a special argument and to check on the code inside ?

                          S Offline
                          S Offline
                          Sascha Lefevre
                          wrote on last edited by
                          #13

                          Just derive your custom class from EventArgs that carries your custom arguments. Then check in the called eventhandler for the type of e and cast it to your custom class if it is the same.

                          D 1 Reply Last reply
                          0
                          • S Sascha Lefevre

                            Just derive your custom class from EventArgs that carries your custom arguments. Then check in the called eventhandler for the type of e and cast it to your custom class if it is the same.

                            D Offline
                            D Offline
                            dilkonika
                            wrote on last edited by
                            #14

                            Not very clear for me. Please could you write some code ? Thank you !

                            S 1 Reply Last reply
                            0
                            • D dilkonika

                              sorry , but I have a central Click event. The problem is that the code inside the click event is depending from the case where the click event is fired from user action ( so a user has clicked ) , or is fired from code. I can't distinguish these 2 cases , then I ask if is possible to detect a click fro the TableLayoutPanel in general.

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

                              Again, the TableLayoutPanel is NOT a control you can click on. It is an extension to other controls where it adds properties to them to change the way they layout in their parent container, such as a Form or Panel. There is no way to differentiate where a ``Click`` event came from. It's always going to be from the button itself. If you're kicking off a ``Click`` event from code, you're doing it wrong. Move the code from inside the event handler into it's own method and call it from both your code and from the ``Click`` event handler.

                              A guide to posting questions on CodeProject

                              Click this: Asking questions is a skill. Seriously, do it.
                              Dave Kreskowiak

                              1 Reply Last reply
                              0
                              • D dilkonika

                                Not very clear for me. Please could you write some code ? Thank you !

                                S Offline
                                S Offline
                                Sascha Lefevre
                                wrote on last edited by
                                #16

                                Ehm. Yes. Just realized we're in the VB-Forum. Can you read C# ? Because I'm not good at writing VB ;)

                                class MyEventArgs : EventArgs
                                {
                                public string Foo { get; private set; }

                                public MyEventArgs(string foo)
                                {
                                    Foo = foo;
                                } 
                                

                                }

                                class MyButton : Button
                                {
                                public void SimulateClick(string foo)
                                {
                                base.OnClick(new MyEventArgs(foo));
                                }
                                }

                                void SomeEventhandler(object sender, EventArgs e)
                                {
                                if(e is MyEventArgs)
                                {
                                string foo = ((MyEventArgs)e).Foo;
                                }
                                else
                                {
                                // ...
                                }
                                }

                                D 1 Reply Last reply
                                0
                                • S Sascha Lefevre

                                  Ehm. Yes. Just realized we're in the VB-Forum. Can you read C# ? Because I'm not good at writing VB ;)

                                  class MyEventArgs : EventArgs
                                  {
                                  public string Foo { get; private set; }

                                  public MyEventArgs(string foo)
                                  {
                                      Foo = foo;
                                  } 
                                  

                                  }

                                  class MyButton : Button
                                  {
                                  public void SimulateClick(string foo)
                                  {
                                  base.OnClick(new MyEventArgs(foo));
                                  }
                                  }

                                  void SomeEventhandler(object sender, EventArgs e)
                                  {
                                  if(e is MyEventArgs)
                                  {
                                  string foo = ((MyEventArgs)e).Foo;
                                  }
                                  else
                                  {
                                  // ...
                                  }
                                  }

                                  D Offline
                                  D Offline
                                  dilkonika
                                  wrote on last edited by
                                  #17

                                  Thank you , I understand your code. But I have a last question : Is it possible not to use your class , but this idea : When I call the Button's click event from code , I use this instruction : myButton_Click(myButton, EventArgs.Empty) Now when this event is fired from a real user click , the EventsArg is empty or not ? Because if is not empty , I can just test inside the click event , if eventArgs is empty or not ? Can this idea work ? Thank you !

                                  S 1 Reply Last reply
                                  0
                                  • D dilkonika

                                    Thank you , I understand your code. But I have a last question : Is it possible not to use your class , but this idea : When I call the Button's click event from code , I use this instruction : myButton_Click(myButton, EventArgs.Empty) Now when this event is fired from a real user click , the EventsArg is empty or not ? Because if is not empty , I can just test inside the click event , if eventArgs is empty or not ? Can this idea work ? Thank you !

                                    S Offline
                                    S Offline
                                    Sascha Lefevre
                                    wrote on last edited by
                                    #18

                                    You could do that. If the Click-Event comes from a real click, then e is of type MouseEventArgs. I would recommend using a custom class for two reasons: You can give it a name that explains its purpose so that your code will speak for itself. And if the need would arise to have a third cause for the event you would be out of options when testing if e equals EventArgs.Empty or not.

                                    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