Storing Info Between Events
-
Huh? when I try a static inside a method, I do get a compiler error (tried with .NET 2.0) It is the C++ doc, and not the C# doc (unfortunately), that says: When you declare a variable in a function, the static keyword specifies that the variable retains its state between calls to that function. :confused:
Luc Pattyn [My Articles] Nil Volentibus Arduum
Maybe I was not clear but I was talking about a static class variable.
-
Maybe I was not clear but I was talking about a static class variable.
As I can't get a local static to work in C# (as C/C++ would allow me to), I figured you meant a class member, but then I am puzzled as to why you want it to be static at all. And that is why I provided another answer to the OP, trying to state things more clearly. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
As I can't get a local static to work in C# (as C/C++ would allow me to), I figured you meant a class member, but then I am puzzled as to why you want it to be static at all. And that is why I provided another answer to the OP, trying to state things more clearly. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
Luc Pattyn wrote:
I am puzzled as to why you want it to be static at all
My C++ brain again ...
-
Luc Pattyn wrote:
I am puzzled as to why you want it to be static at all
My C++ brain again ...
you should kickstart the brain that fits the forum. :-D
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
you should kickstart the brain that fits the forum. :-D
Luc Pattyn [My Articles] Nil Volentibus Arduum
Even with a kick it's slow to react. :(
-
Huh? when I try a static inside a method, I do get a compiler error (tried with .NET 2.0) It is the C++ doc, and not the C# doc (unfortunately), that says: When you declare a variable in a function, the static keyword specifies that the variable retains its state between calls to that function. :confused:
Luc Pattyn [My Articles] Nil Volentibus Arduum
Same problem. I should have specified class scope statics.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
Your code seems to suggest that you are maintaining the state of your app in a variable (clickCount) which is declared inside the button Click handler, which means it won't survive the handling of a click, hence it will always restart having the value of 1. You should move the declaration outside the method, i.e. make it a class member. That way, for each instance of the Form (assuming you might create more than one of them), the button would remember its state. You do not need the
static
keyword, unless you want to have all instances of that Form to share their state (which seems unlikely to me). IMO it is easier and robuster to put the state inside the button itself; there are several ways to achieve this; - one is to create your own MyButton class, inheriting from Button, and taking care of its peculiarities (IMO it would be overkill here); - another is to hide the state in the Tag property, which is a field provided but unused by Windows for all WinForms Controls, so it is available for whatever purpose you decide to use it for. Mind you, it is of type object, so you would need some casting. (I prefer this approach). - and then you could derive the state from existing variables and/or properties; in your example, you could perhaps just check the txtDisplay.Text and when it ends on "Side1" just replace "Side1" by "Side2". (I typically don't do it this way, as it is less robust; e.g. when you change languages, there is more code that needs changing). :)Luc Pattyn [My Articles] Nil Volentibus Arduum
Hi Luc, thanks for replying. I have searched for a way to "put the state inside the button itself" like you have suggested but all I've found were maintaining session state for web apps and not windows form apps. I'm also interested in your suggestion to "hide the state in the Tag property" but I can't find an example of how to do that either. Please give me links to articles on how to do those things I've just mention if you can. Thanks again for your help.
-
Hi Luc, thanks for replying. I have searched for a way to "put the state inside the button itself" like you have suggested but all I've found were maintaining session state for web apps and not windows form apps. I'm also interested in your suggestion to "hide the state in the Tag property" but I can't find an example of how to do that either. Please give me links to articles on how to do those things I've just mention if you can. Thanks again for your help.
What's so hard about it??
SomeButton.Tag = _stateValue_;
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
Hi Luc, thanks for replying. I have searched for a way to "put the state inside the button itself" like you have suggested but all I've found were maintaining session state for web apps and not windows form apps. I'm also interested in your suggestion to "hide the state in the Tag property" but I can't find an example of how to do that either. Please give me links to articles on how to do those things I've just mention if you can. Thanks again for your help.
And the harder half of the job is getting the state back, which requires a cast as I said:
int myState=(int)myControl.Tag;
And no, I don't have a reference about this subject. Inheriting from a class, or creating a more specialized one, is a fundamental concept in Object-Oriented Programming. There are zillions of articles on the subject, why not try and read Custom Button - Issues with Focus Border, Text Color, etc.[^] (I didn't, it was the first I found using CP search). :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
Hi Luc, thanks for replying. I have searched for a way to "put the state inside the button itself" like you have suggested but all I've found were maintaining session state for web apps and not windows form apps. I'm also interested in your suggestion to "hide the state in the Tag property" but I can't find an example of how to do that either. Please give me links to articles on how to do those things I've just mention if you can. Thanks again for your help.
(Sorry for a garbage sentence meant to fool the CP bugs and cache issues so I can get this posted) And the harder half of the job is getting the state back, which requires a cast as I said:
int myState=(int)myControl.Tag;
And no, I don't have a reference about this subject. Inheriting from a class, or creating a more specialized one, is a fundamental concept in Object-Oriented Programming. There are zillions of articles on the subject, why not try and read Custom Button - Issues with Focus Border, Text Color, etc.[^] (I didn't, it was the first I found using CP search). :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
Hi Luc, thanks for replying. I have searched for a way to "put the state inside the button itself" like you have suggested but all I've found were maintaining session state for web apps and not windows form apps. I'm also interested in your suggestion to "hide the state in the Tag property" but I can't find an example of how to do that either. Please give me links to articles on how to do those things I've just mention if you can. Thanks again for your help.
(Another garbage sentence was needed to get this post accepted amidst bugs and cache issues, sorry for that) And the harder half of the job is getting the state back, which requires a cast as I said:
int myState=(int)myControl.Tag;
And no, I don't have a reference about this subject. Inheriting from a class, or creating a more specialized one, is a fundamental concept in Object-Oriented Programming. There are zillions of articles on the subject, why not try and read Custom Button - Issues with Focus Border, Text Color, etc.[^] (I didn't, it was the first I found using CP search). :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
Hi Luc, thanks for replying. I have searched for a way to "put the state inside the button itself" like you have suggested but all I've found were maintaining session state for web apps and not windows form apps. I'm also interested in your suggestion to "hide the state in the Tag property" but I can't find an example of how to do that either. Please give me links to articles on how to do those things I've just mention if you can. Thanks again for your help.
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.
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()); }
}