Different actions on clicking the same command button twice
Hi all,
Similar to a toggle button, I need to execute the following functionality in C#.NET -
Button1 is a simple command button.
If Button1 is clicked once - Do action1
If Button1 is clicked second time - Do action2.
If Button1 is clicked a third time - do action1 again
and repeat this for all subsequent clicks.
How do I know how many times Button1 was clicked during running the application?
Thanks in advance!
# 3 Re: Different actions on clicking the same command button twice
Have a boolean in your class.
This will switch whenever your click event happens e.g.
class MyClass : public <whatever>
{
private bool m_fToggle = true;
private void <the event handler>
{
if (m_fToggle)
{
// do this first time
}
else
{
// do this second time
}
m_fToggle = !m_fToggle;
}
}
Points of note :
(1) The toggle flag invert is done outside of the if statment code.
Why ? It's more stable - you can't forget to do it in one branch of the toggle 'if' statement.
(2) First test case is true.
I always prefer to test for true cases first, and false cases second. At the least this is more efficient.
It also sets a coding standard which is always good - you always know the first block after an 'if' statement is the true block and the 'else' is the false block.
Get the idea ?
Oh, and about the m_f - that's hungarian notation for booleans (the 'f' prefix for boolean and 'm_' denotes member) and I still keep true to that for C# - I don't see any reason why not (C++.NET throws up some interesting conflictions with the notation, but there you go).
Darwen.