|
-
Jan 10th, 2007, 06:15 PM
#1
Thread Starter
Lively Member
[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
-
Jan 10th, 2007, 07:18 PM
#2
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.
-
Jan 10th, 2007, 07:24 PM
#3
Thread Starter
Lively Member
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;
}
-
Jan 10th, 2007, 07:30 PM
#4
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.
-
Jan 10th, 2007, 07:52 PM
#5
Thread Starter
Lively Member
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.
-
Jan 10th, 2007, 08:15 PM
#6
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";
}
-
Jan 10th, 2007, 08:26 PM
#7
Thread Starter
Lively Member
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!
-
Jan 10th, 2007, 09:09 PM
#8
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();
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|