Results 1 to 8 of 8

Thread: [RESOLVED] [2.0] count mouseclicks ?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Location
    Denmark
    Posts
    67

    Resolved [RESOLVED] [2.0] count mouseclicks ?

    I want to count everytime i press the mousebutton, and put the number of times i pressed in a label... but I cant figure out, bin sitting with it for 2 hours :S plz help me a little.


    [edit] its on a windows Form, but if you can show how to do it in ASP.NET to, that would be great.
    Last edited by RoflJOE; Jan 10th, 2007 at 06:18 PM. Reason: forgot something

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

    Re: [2.0] count mouseclicks ?

    You should create a single method to handle the MouseClick event for every control on your form plus the form itself. In that method you would simply increment an integer variable declared at the class level and then display that value in your Label.

    To create the event handler you should select the form in the designer and then go to the Properties window and press the Events button. Now double-click the MouseClick event to create the event handler. Now go back to the form and type Ctrl+A to select all your controls. Go back to the Properties window and select the method you previously created from the drop-down list for the MouseClick event. If that event is not listed it means that you have selected at least component that doesn't support that event, so you must manually deselect each one of those first. Alternatively you can deselect all and manually select each one that does support that event.
    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

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Location
    Denmark
    Posts
    67

    Re: [2.0] count mouseclicks ?

    sry but it aint much that i understand from what you said, im danish and aint that good to complicated english... could you just give me a little code examble and show how to detect the clicks, something like

    Code:
            private void Form1_MouseClick(object sender, MouseEventArgs e)
            {
                label1.text *= 1;
            }

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

    Re: [2.0] count mouseclicks ?

    I said you need to declare an integer at the class level and increment that, then display that value in your Label. The Text property of a Label is a string so you cannot perfrom mathematics on it. Also, you're trying to multiply by 1, which will have no effect. You have to add 1.
    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
    Lively Member
    Join Date
    Aug 2006
    Location
    Denmark
    Posts
    67

    Re: [2.0] count mouseclicks ?

    Code:
                private void Form1_MouseClick(object sender, MouseEventArgs e)
            {
                int i = 1;
                i = i + 1;
                label1.Text = "lol " + i;
            }
    it aint really working :O how do i tell that I has to multiply with the number in the label
    Last edited by RoflJOE; Jan 10th, 2007 at 08:09 PM.

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

    Re: [2.0] count mouseclicks ?

    As I said, you need to declare your integer variable at the class level, not locally. A local variable ceases to exist once the block it was declared in completes, so the value will not be remembered next time the event is raised:[CODE]private int clickCount = 0;

    private void Form1_MouseClick(object sender, MouseEventArgs e)
    {
    this.clickCount++; // "++" is equivalent to "+= 1"
    this.label1.Text = this.clickCount.ToString() + " clicks";
    }
    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
    Lively Member
    Join Date
    Aug 2006
    Location
    Denmark
    Posts
    67

    Re: [2.0] count mouseclicks ?

    Code:
    
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace mouseclick
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            int i = 1;
            private void Form1_MouseClick(object sender, MouseEventArgs e)
            {
                i += 1;
                label1.Text = "" + i;
            }
        }
    }
    
    Its working now, Im new to C# and I still dont quite get it, I programmed VB before. all the ++ and so on is still strange for me but thx dude. now I can sleep tonight!

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

    Re: [RESOLVED] [2.0] count mouseclicks ?

    Don't do this:
    Code:
    label1.Text = "" + i;
    There is absolutely no point or advantage to concatenating an empty string to anything. If you want display a number in a control then convert it to a string as I showed in my previous post:
    Code:
    this.label1.Text = this.clickCount.ToString();
    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

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