R
raylopez99
I find that I am using bool variables a lot when I code in Forms. I
know how to overload event handlers, and that's great for offloading
code from the 'base' event handler and/or creating helper functions in
the way of delegates and the like, and associating these helper
functions with newly created controls (controls that you create
programmatically, rather than before runtime using the Design
Wizard). However, boolean variables are very quick and in some
circumstances, like doing stuff in Paint handler as a consequence of
what went on in OnKeyDown or MouseDown, there appears to be no
substitute (other than writing a complicated Delegate that accepts
PaintEventArgs and MouseEventArgs as well as KeyEventArgs, which I
won't even know how to do or even how to trigger, and I'm not sure is
even possible since C# doesn't allow multiple inheritance from several
base classes, not counting an interface).
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
know how to overload event handlers, and that's great for offloading
code from the 'base' event handler and/or creating helper functions in
the way of delegates and the like, and associating these helper
functions with newly created controls (controls that you create
programmatically, rather than before runtime using the Design
Wizard). However, boolean variables are very quick and in some
circumstances, like doing stuff in Paint handler as a consequence of
what went on in OnKeyDown or MouseDown, there appears to be no
substitute (other than writing a complicated Delegate that accepts
PaintEventArgs and MouseEventArgs as well as KeyEventArgs, which I
won't even know how to do or even how to trigger, and I'm not sure is
even possible since C# doesn't allow multiple inheritance from several
base classes, not counting an interface).
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