By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,934 Members | 1,527 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,934 IT Pros & Developers. It's quick & easy.

Button control questions

P: n/a
I have derived a class from the System.Windows.Forms.Button class in order
to draw the button differently.

Among other things, I paint the background of the button in the OnPaing
override.

Next I wanted the background color to change when the button was clicked so
I overrided the OnMouseDown and OnMouseUp events like so: (pseudocode)
Protected Overrides Sub OnMouseDown(...)
MyBase.OnMouseDown(mevent)
bClicked = True
Me.Invalidate()
End Sub

Protected Overrides Sub OnMouseUp(...)
MyBase.OnMouseUp(mevent)
bClicked = False
Me.Invalidate()
End Sub

Elswhere in the paint event, I check the value of the boolean variable to
decide what color to paint:

Protected Overrides Sub OnPaint(...)
MyBase.OnPaint(pevent)

If bClicked Then
'Paint CLICKED color here
Else
'Paint UNCLICKED color here
End If

End Sub
This all seems to work correctly except for one thing. When I test the
button using a MsgBox in the buttons click event handler, the button
changes color when I click and the message box shows but the button's color
stays in the "clicked" state until I clear the MsgBox.

When I use a normal button, the visual state of the button immediately
reverts to the unclicked state before the MsgBox appears.

I wish to duplicate that in my class.

For example, I want to click the button have it change color to indicate
the click and then revert back to the unclicked state when I release the
mouse button. It seems that the Click event handler is getting called
before the OnMouseUp executes.

What am I doing wrong?

Chris
--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
It's not you, it's the button control. It has one or two strange features
which, offhand, I can't remember. You may like to look at my button source
and see if that fixes your problem. If I remember correctly, one bug was
that if a button with focus contains the cursor, and you press and hold the
pace bar, move the cursor away from the button and release the spacebar, you
will find that the button recieves a click event when a mouse button is
clicked outside the button. The solution to this was to call OnLostFocus()
in the OnClick() and OnDoubleClick() methods.
My Button source can be found at http://dotnetrix.co.uk/buttons.html

--
Mick Doherty
http://dotnetrix.co.uk/nothing.html
"Chris Dunaway" <"dunawayc[[at]_lunchmeat_sbcglobal[dot]]net"> wrote in
message news:yr*****************************@40tude.net...
I have derived a class from the System.Windows.Forms.Button class in order
to draw the button differently.

Among other things, I paint the background of the button in the OnPaing
override.

Next I wanted the background color to change when the button was clicked
so
I overrided the OnMouseDown and OnMouseUp events like so: (pseudocode)
Protected Overrides Sub OnMouseDown(...)
MyBase.OnMouseDown(mevent)
bClicked = True
Me.Invalidate()
End Sub

Protected Overrides Sub OnMouseUp(...)
MyBase.OnMouseUp(mevent)
bClicked = False
Me.Invalidate()
End Sub

Elswhere in the paint event, I check the value of the boolean variable to
decide what color to paint:

Protected Overrides Sub OnPaint(...)
MyBase.OnPaint(pevent)

If bClicked Then
'Paint CLICKED color here
Else
'Paint UNCLICKED color here
End If

End Sub
This all seems to work correctly except for one thing. When I test the
button using a MsgBox in the buttons click event handler, the button
changes color when I click and the message box shows but the button's
color
stays in the "clicked" state until I clear the MsgBox.

When I use a normal button, the visual state of the button immediately
reverts to the unclicked state before the MsgBox appears.

I wish to duplicate that in my class.

For example, I want to click the button have it change color to indicate
the click and then revert back to the unclicked state when I release the
mouse button. It seems that the Click event handler is getting called
before the OnMouseUp executes.

What am I doing wrong?

Chris
--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.737 / Virus Database: 491 - Release Date: 11/08/2004
Nov 21 '05 #2

P: n/a
On Fri, 13 Aug 2004 15:15:46 +0100, Mick Doherty wrote:
My Button source can be found at http://dotnetrix.co.uk/buttons.html


I tried connecting to your site but it would not come up. Is there another
site I could find the code?
--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
Nov 21 '05 #3

P: n/a
The link works here, but I know that there may be a problem going directly
to the page which is caused by the site being Framed by my Host, the same
reason why you wont find it via any search engine.

The page's real location is:
http://homepage.ntlworld.com/mdaudi1...e/buttons.html

--
Mick Doherty
http://dotnetrix.co.uk/nothing.html
"Chris Dunaway" <"dunawayc[[at]_lunchmeat_sbcglobal[dot]]net"> wrote in
message news:16******************************@40tude.net.. .
On Fri, 13 Aug 2004 15:15:46 +0100, Mick Doherty wrote:
My Button source can be found at http://dotnetrix.co.uk/buttons.html


I tried connecting to your site but it would not come up. Is there
another
site I could find the code?
--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.737 / Virus Database: 491 - Release Date: 11/08/2004
Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.