Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Do you use bool a lot in Windows Forms? Is there a better way?

5 views
Skip to first unread message

raylopez99

unread,
Aug 4, 2008, 1:22:47 PM8/4/08
to
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

Nicholas Paldino [.NET/C# MVP]

unread,
Aug 4, 2008, 1:43:39 PM8/4/08
to
Ray,

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...

Ignacio Machin ( .NET/ C# MVP )

unread,
Aug 4, 2008, 4:25:07 PM8/4/08
to
On Aug 4, 1:22 pm, raylopez99 <raylope...@yahoo.com> wrote:
> I find that I am using bool variables a lot when I code in Forms.

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)

raylopez99

unread,
Aug 4, 2008, 6:07:20 PM8/4/08
to
On Aug 4, 1:25 pm, "Ignacio Machin ( .NET/ C# MVP )"
<ignacio.mac...@gmail.com> wrote:

> > 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

michel.d...@gmail.com

unread,
Aug 5, 2008, 3:56:14 AM8/5/08
to
I'm pretty sure there is some constraint in your question I've
overlooked, but I think you could rid yourself of those delegates and
bools altogether by checking mouse and keyboard state in realtime :

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 -

raylopez99

unread,
Aug 5, 2008, 5:18:05 AM8/5/08
to

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

raylopez99

unread,
Aug 5, 2008, 1:14:03 PM8/5/08
to
On Aug 5, 2:18 am, raylopez99 <raylope...@yahoo.com> wrote:
> On Aug 5, 12:56 am, michel.desang...@gmail.com wrote:
>
>
>
> > I'm pretty sure there is some constraint in your question I've
> > overlooked, but I think you could rid yourself of those delegates and
> > bools altogether by checking mouse and keyboard state in realtime :
>
> > 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
>
> > On 5 août, 00:07,raylopez99<raylope...@yahoo.com> wrote:
> > > - 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


michel.d...@gmail.com

unread,
Aug 5, 2008, 1:53:58 PM8/5/08
to
> RL- Masquer le texte des messages précédents -

>
> - Afficher le texte des messages précédents -

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

0 new messages