However, my question in this post is different: suppose you want to
Paint some figures when a user presses a key plus right clicks--you're
not creating a new Form--but painting on the same form (of course you
can also create a new Form, and pass information to the new form, but
that's another subject)--I find that I use boolean variables a lot to
trigger events such as Paint. For example, before the form Form1 is
constructed, you create 'global' (local to the Form1) variables that
are boolean and set to false by default, then set them to true if
certain combinations like keyboard + mouse click (left or right) are
triggered. Thereafter, when the program gets into OnPaint, you can
run certain blocks of code that are hidden by if/else statements, if
these boolean variables are true. Am I clear here? Sorry Jon, no
code. :-)
My question then: anybody else program like this, and, if so, is
using the bool variable the 'fastest' or 'best' way, or should I do
something funky like set a Byte or some such as a "Flag" and then
check bits on the byte, or some such, as is sometimes done in C? Even
for simple programs I'm finding I have a half dozen such boolean
variables, but maybe it's just me and the particularities of my
programming style (perhaps I should use child forms, subforms, split
containers and so forth more, rather than putting stuff on the main
form, though I do that too).
Another way of asking the same question: aside from using bools as
above, how to trigger something in Paint event handler using
MouseEventArgs as well as KeyEventArgs, when they must occur in tandem
(user pushes on a key then clicks on a mouse)? The way I do it now:
user pushes on a key, and OnKeyDown sets a boolean variable to true,
and when user clicks on a mouse (say e.Button == MouseButtons.Left;)
another variable is set to true, and finally, in another method, like
Paint, the combination of both booleans allows Paint to execute some
code.
Is this conventional?
Thanks in advance
RL
I would say that generally, what you are doing is the right thing. I
don't know about using booleans for everything, but I wouldn't say to use a
byte and set flags if it doesn't make sense to do so.
For example, if you had a control key pushed down, then I would store
that state somewhere. Having a field that has control state which is
boolean makes sense, since it is a true/false value.
Then, if you needed the current location of the mouse (or of the mouse
button) you would store that somewhere as well (coordinates, mouse button
state, etc, etc).
It might make some sense to have classes that represent your various
states, which have properties which you would set in your event handlers
(button state, key state, etc, etc) and then have the logic evaluated when
you set those properties or when you query the state in your redraw. It
would help to clean up your code, I am sure, and make the maintinence
better.
As for the bit flags, I would advise against it, unless you have
specific profiling information that shows this is a bottleneck in your code
(which I doubt it is).
--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com
"raylopez99" <raylo...@yahoo.com> wrote in message
news:e1562fbc-2b07-4dca...@2g2000hsn.googlegroups.com...
I use bool too, but I donot consider that I use them more than other
type of vars.
> However, my question in this post is different: suppose you want to
> Paint some figures when a user presses a key plus right clicks--you're
> not creating a new Form--but painting on the same form (of course you
> can also create a new Form, and pass information to the new form, but
> that's another subject)--I find that I use boolean variables a lot to
> trigger events such as Paint. For example, before the form Form1 is
> constructed, you create 'global' (local to the Form1) variables that
> are boolean and set to false by default, then set them to true if
> certain combinations like keyboard + mouse click (left or right) are
> triggered. Thereafter, when the program gets into OnPaint, you can
> run certain blocks of code that are hidden by if/else statements, if
> these boolean variables are true. Am I clear here? Sorry Jon, no
> code. :-)
I think that in this case it's ok, unless you have a big amount (or a
dynamic set) of possibles keys combination; in that case you should
implement another solution, like having a list of delegates that you
can call in the onpaint event, in this case you do not have to ask if
you have to execute each command but just execute the one that are in
a collection (this also allows you to customize the commands as
needed)
> > However, my question in this post is different: suppose you want to
> > Paint some figures when a user presses a key plus right clicks--you're
> > not creating a new Form--but painting on the same form (of course you
> > can also create a new Form, and pass information to the new form, but
> > that's another subject)--I find that I use boolean variables a lot to
> > trigger events such as Paint. For example, before the form Form1 is
> > constructed, you create 'global' (local to the Form1) variables that
> > are boolean and set to false by default, then set them to true if
> > certain combinations like keyboard + mouse click (left or right) are
> > triggered. Thereafter, when the program gets into OnPaint, you can
> > run certain blocks of code that are hidden by if/else statements, if
> > these boolean variables are true. Am I clear here? Sorry Jon, no
> > code. :-)
>
> I think that in this case it's ok, unless you have a big amount (or a
> dynamic set) of possibles keys combination; in that case you should
> implement another solution, like having a list of delegates that you
> can call in the onpaint event, in this case you do not have to ask if
> you have to execute each command but just execute the one that are in
> a collection (this also allows you to customize the commands as
> needed)
>
OK, thank you both, Nicolas and Ignacio.
I am tempted to ask how you can set up a delegate or list of delegates
that you can call in the Paint event, that requires keyboard entry.
can you do:
public delegate void MyKeysPlusMouseInsideOfPaint (object sender,
PaintEventArgs e, MouseEventArgs f, KeyEventArgs g)? ? ?
That would be too much for me... but I am happy to learn I'm not doing
things improperly with bool.
RL
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (MouseButtons == MouseButtons.Left)
Debug.WriteLine("LMB clicked");
if (Cursor.Position.X > 50)
Debug.WriteLine("Mouse X is > 50");
if (ModifierKeys == Keys.Shift)
Debug.WriteLine("Shift pressed");
}
(MouseButtons, Cursor and ModifierKeys are inherited from Control).
Michel
> RL- Masquer le texte des messages précédents -
>
> - Afficher le texte des messages précédents -
This is incredible, thanks Michel!
I did not think you could check for keyboard and mouse events from
inside of Paint by using override as you do. So you can use the base
for 'pure' Graphics and the override method that inherits from the
base, for checking mouse and keyboard... interesting. I will check
and report back if this doesn't work.
RL
Thanks, I did this and found some interesting stuff (for a newbie like
me) which I'll post in a separate thread.
RL
Er, I don't think you *have* to override OnPaint, that's just how I
typed it in Visual Studio to avoir typing errors ;) and I was
following your "when the program gets into OnPaint, you can run
certain blocks of code" idea. Normally, you should be able to use
those three properties pretty much anywhere, provided it's inside any
Control (including a Form). So you could use it in the base Paint
method of a derived Control, for instance, or any event handler.
ModifierKeys and MouseButtons are static (as they are the same system-
wide), while Cursor is an instance property, but it can be replaced by
MousePosition which is static too (but both need PointToClient
conversions to give the relative coordinates, I didn't see a
difference between the two versions other than their 'staticity').
Glad I could help !
Michel
as