473,231 Members | 1,879 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

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

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
Aug 4 '08 #1
7 1373
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]
- mv*@spam.guard.caspershouse.com
"raylopez99" <ra********@yahoo.comwrote in message
news:e1**********************************@2g2000hs n.googlegroups.com...
>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

Aug 4 '08 #2
On Aug 4, 1:22 pm, raylopez99 <raylope...@yahoo.comwrote:
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)
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
Aug 4 '08 #3
On Aug 4, 1:25*pm, "Ignacio Machin ( .NET/ C# MVP )"
<ignacio.mac...@gmail.comwrote:
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
Aug 4 '08 #4
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.comwrote:
On Aug 4, 1:25*pm, "Ignacio Machin ( .NET/ C# MVP )"

<ignacio.mac...@gmail.comwrote:
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- Masquer le texte des messages précédents -

- Afficher le texte des messages précédents -
Aug 5 '08 #5
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.comwrote:
- 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
Aug 5 '08 #6
On Aug 5, 2:18*am, raylopez99 <raylope...@yahoo.comwrote:
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.comwrote:
- 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
Aug 5 '08 #7
On 5 août, 11:18, raylopez99 <raylope...@yahoo.comwrote:
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.comwrote:
- 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- 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
Aug 5 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Hamed | last post by:
Hello I have employed as a developer in a software company that its team uses FoxPro / VB 6.0 / VC++ 6.0 as the developing tools and newly is going to migrate to VS.NET. There is a project...
4
by: Bilo | last post by:
I have a Windows Forms Class MainGUI I have declared MainGUI maingui; public System.ComponentModel.Container components = new Container(); in the Class I call another class MediaDriver with...
2
by: John Granade | last post by:
I'm looking for the best way to make a dataset available from multiple Windows forms. The dataset is created from an XML file. I have a main form (frmMain) that loads the dataset and reads the...
8
by: Alison | last post by:
Hi, Al I am trying to design a user interface which provides both menus and toolbars for some users to click on whatever they want to do, at the same time, I would like to have a console window...
5
by: Fredrik Melin | last post by:
2005 uses the Partial Class & <form>.Designer.vb for its "windows forms generated code" Anyone know if its faster/better then old 2003 way? Also, when upgrading a 2003 project it did not...
4
by: Rainer Queck | last post by:
Hi NG, in a dynamically created tabpage I have a checkbox, which with its "Checked" is bound (DataBindings.Add) to a bool property of one of my classes. I realized, that the bool property of my...
4
by: Stephen Plotnick | last post by:
In a data grid using Access as the data source.. The da.update is failing with "object reference not set to an instance of anobject" It looks like everything is ok and I was wondering if there...
19
by: rbrowning1958 | last post by:
Hello, I am confused by dispose etc. and hope someone can set me right. 1. The Dispose(Bool) the IDE generates for a form has nothing to do with IDisposable, right? 2. So when is this called?...
1
by: Allen | last post by:
How to force System.Windows.Forms.Screen.AllScreens to update when attaching an additional monitor? Form1 is Loaded and runs the following upon detection of additional monitor: Dim Screens()...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.