Results 1 to 9 of 9

Thread: Calling Control_MouseDown

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2009
    Posts
    14

    Question Calling Control_MouseDown

    Hello,


    How to call the code below:

    Code:
                
    private void Control_MouseDown(System.Object sender,
                    System.Windows.Forms.MouseEventArgs e)
                {
                    Control control = (Control)sender;
                    if (control.Capture)
                    {
                        TextBox1.Text = control.Name + " has captured the mouse";
                    }
                }
    from a Button1 Click event:

    Code:
                private void Button1_Click(object sender, EventArgs e)
                {
                    //this code doesn't work - returns invalid arguments
                   Control_MouseDown(sender, control)
                }

  2. #2
    Addicted Member
    Join Date
    Nov 2006
    Location
    Minnesota
    Posts
    235

    Re: Calling Control_MouseDown

    You need to match the signature. The MouseDown event requires that 2 things are passed to it:

    1) An object (This is generally the control that raised the event)
    2) An object of System.Windows.Forms.MouseEventArgs

    So you could do this...
    Code:
    Control_MouseDown(sender, new System.Windows.Forms.MouseEventArgs(System.Windows.Forms.MouseButtons.Left, 0,1,1,0));
    It is hard to figure out what to give you for an answer as it isn't easy to understand what you are attempting.
    If someone has helped you, please make sure to rate them.

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2009
    Posts
    14

    Re: Calling Control_MouseDown

    Thanks I get the point and do what I need

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Calling Control_MouseDown

    The simple fact is that you shouldn't be calling that event handler directly.
    csharp Code:
    1. private void Control_MouseDown(Object sender, MouseEventArgs e)
    2. {
    3.     Control control = (Control) sender;
    4.  
    5.     this.IdentifyMouseCapture(control);
    6. }
    7.  
    8. private void Button1_Click(object sender, EventArgs e)
    9. {
    10.     Control control = (Control)sender;
    11.  
    12.     this.IdentifyMouseCapture(control);
    13. }
    14.  
    15. private void IdentifyMouseCapture(Control ctl)
    16. {
    17.     if (ctl.Capture)
    18.     {
    19.         textBox1.Text = ctl.Name + " has captured the mouse";
    20.     }
    21. }
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    New Member
    Join Date
    Dec 2009
    Posts
    14

    Re: Calling Control_MouseDown

    Hi,

    Can you give me more info why shouldn't be calling event handler directly? I believe you, but i just want to understand it.
    For me at this stage seems more complicated to call one more procedure - IdentifyMouseCapture.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Calling Control_MouseDown

    An event handler is a method so you certainly can call it directly if you want to. The thing is, by creating "fake" arguments for an event handler you can get into the situation where you're not dealing with the data you think you are. This is one of those situations where if you just make a rule not to call event handlers directly then you can never get it wrong. It's very simple to structure your code not to have to call an event handler so you may as well do so.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    New Member
    Join Date
    Dec 2009
    Posts
    14

    Re: Calling Control_MouseDown

    1. When I call an event handler I thought I can call arguments, like any other procedure. I didn't know these arguments are a kind of "fake" arguments. I still didn't learn the conception around arguments in event handlers, that's why in my first post I asked such a beginner question.

    2. Do you know some proper articles/sample code around that topic? Especially about calling directly and not calling directly pros+/cons-?

  8. #8
    Member
    Join Date
    Dec 2009
    Posts
    34

    Re: Calling Control_MouseDown

    The "fake" parameter you are passing is the MouseEventArgs object that you are creating.

    You will run into this situation pretty frequently. The pattern that works in most cases is to factor out the common code into it's own function and then call the re-factored function as needed (just as jmcilhinney suggested) instead of the event handler.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Calling Control_MouseDown

    The arguments received by an event handler are created by the object that raised the event. If there's no event and you created the arguments yourself then they are obviously fake, becaue they don't represent a real event.

    You don't need a tutorial for this because it's pretty simple: event handlers are supposed to handle events. If there's no event there should be no event handler. You can call one directly because it's still a method. Doing so saves you having to write a separate method but, as one of our main aims when writing code is clarity, we shouldn't create a less clear, possibly problematic situation to save a few keystrokes.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width