Click here to Skip to main content
15,887,135 members
Home / Discussions / C#
   

C#

 
AnswerRe: Storing Info Between Events Pin
Luc Pattyn23-Jun-12 5:11
sitebuilderLuc Pattyn23-Jun-12 5:11 
GeneralRe: Storing Info Between Events Pin
Richard MacCutchan23-Jun-12 5:14
mveRichard MacCutchan23-Jun-12 5:14 
GeneralRe: Storing Info Between Events Pin
Dave Kreskowiak23-Jun-12 5:37
mveDave Kreskowiak23-Jun-12 5:37 
AnswerRe: Storing Info Between Events Pin
Luc Pattyn23-Jun-12 4:04
sitebuilderLuc Pattyn23-Jun-12 4:04 
GeneralRe: Storing Info Between Events Pin
ASPnoob23-Jun-12 7:24
ASPnoob23-Jun-12 7:24 
GeneralRe: Storing Info Between Events Pin
Dave Kreskowiak23-Jun-12 9:56
mveDave Kreskowiak23-Jun-12 9:56 
AnswerRe: Storing Info Between Events Pin
Luc Pattyn23-Jun-12 10:48
sitebuilderLuc Pattyn23-Jun-12 10:48 
GeneralRe: Storing Info Between Events Pin
ignrod27-Jun-12 1:11
ignrod27-Jun-12 1:11 
Sorry for the delay. If you want to keep number of clicks inside the button, all you have to do is subclass Button class and add a field with the number of clicks. Then, override the OnClick method to increase the number of clicks by one every time the button is clicked. Don't forget to call the parent OnClick so that the normal button click behavior is executed.
You have an example below.
C#
using System;
using System.Drawing;
using System.Windows.Forms;

class CountClickButton : Button {
	
	int clickCount; // Number of clicks
	
	public CountClickButton() {
		clickCount = 0;
	}
	
	// OnClick is called when the button is clicked. It must increase clickCount by one
	// and then execute the event handlers (which is done when base.OnClick
	// is executed).
	protected override void OnClick(EventArgs e) {
		clickCount = clickCount + 1;
		base.OnClick(e);
	}
	
	// Only allow other classes to read number of clicks. Number of clicks cannot be
	// modified by other classes.
	public int ClickCount {
		get {
			return clickCount;
		}
	}
}

class CountClickForm : Form {
	
	CountClickButton b;
	
	public CountClickForm() {
		b = new CountClickButton();
		b.Text = "Click me!";
		b.Size = b.PreferredSize;
		ClientSize = new Size(700, 400); // Should be big enough. Fit to your own needs.
		b.Left = (ClientSize.Width - b.Width) / 2;
		b.Top = (ClientSize.Height - b.Height) / 2;
		b.Click += new EventHandler(b_clicked);
		Controls.Add(b);
		Text = "I count clicks!";
	}
	
	// b_clicked is registered as an event. It will be executed when button is clicked,
	// after number of clicks is increased by one.
	void b_clicked(object sender, EventArgs e) {
		string text = String.Format("Button has been clicked {0} times.", b.ClickCount);
		if (b.ClickCount < 1) { 
			// This should never happen.
			text = "Sorry, I was drunk when programming this. Cheers!!";
		}
		if (b.ClickCount == 1) {
			text = "Button has been clicked once.";
		}
		if (b.ClickCount == 2) {
			text = "Button has been clicked twice.";
		}
		Text = text;
	}
		
	[STAThreadAttribute]
	static void Main() {
		Application.Run(new CountClickForm());
	}
}

QuestionConverting BindingSource and Guid [solved] Pin
Midnight Ahri22-Jun-12 17:11
Midnight Ahri22-Jun-12 17:11 
AnswerRe: Converting BindingSource and Guid Pin
OriginalGriff22-Jun-12 19:50
mveOriginalGriff22-Jun-12 19:50 
AnswerRe: Converting BindingSource and Guid Pin
Midnight Ahri22-Jun-12 20:08
Midnight Ahri22-Jun-12 20:08 
GeneralRe: Converting BindingSource and Guid Pin
OriginalGriff22-Jun-12 20:24
mveOriginalGriff22-Jun-12 20:24 
QuestionListbox Pin
Dirk Higbee22-Jun-12 12:57
Dirk Higbee22-Jun-12 12:57 
AnswerRe: Listbox Pin
OriginalGriff22-Jun-12 19:52
mveOriginalGriff22-Jun-12 19:52 
GeneralRe: Listbox Pin
Dirk Higbee23-Jun-12 7:30
Dirk Higbee23-Jun-12 7:30 
QuestionHow to split byte[] to string & bytes Pin
caspidoron22-Jun-12 8:53
caspidoron22-Jun-12 8:53 
AnswerRe: How to split byte[] to string & bytes Pin
Ian Shlasko22-Jun-12 9:00
Ian Shlasko22-Jun-12 9:00 
AnswerRe: How to split byte[] to string & bytes Pin
Richard Andrew x6422-Jun-12 9:02
professionalRichard Andrew x6422-Jun-12 9:02 
QuestionC# work with database Pin
sc steinhayse22-Jun-12 7:33
sc steinhayse22-Jun-12 7:33 
AnswerRe: C# work with database Pin
Ravi Bhavnani23-Jun-12 4:00
professionalRavi Bhavnani23-Jun-12 4:00 
QuestionC# to update table Pin
sc steinhayse22-Jun-12 3:43
sc steinhayse22-Jun-12 3:43 
AnswerRe: C# to update table Pin
PIEBALDconsult22-Jun-12 5:09
mvePIEBALDconsult22-Jun-12 5:09 
GeneralRe: C# to update table Pin
OctavioNA22-Jun-12 7:19
OctavioNA22-Jun-12 7:19 
GeneralRe: C# to update table Pin
PIEBALDconsult22-Jun-12 18:24
mvePIEBALDconsult22-Jun-12 18:24 
AnswerRe: C# to update table Pin
kishhr22-Jun-12 6:33
kishhr22-Jun-12 6:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.