Button control questions

C

Chris Dunaway

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

Mick Doherty

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 said:
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.
 
C

Chris Dunaway

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

Mick Doherty

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/mdaudi100/alternate/buttons.html

--
Mick Doherty
http://dotnetrix.co.uk/nothing.html


Chris Dunaway said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top