# Use bitmapButton with mouseEvent?

**URL:** <https://www.ruby-forum.com/t/use-bitmapbutton-with-mouseevent/135208>\
**Category:** wxRuby\
**Created:** [April 19, 2008, 7:36am UTC](https://www.ruby-forum.com/t/use-bitmapbutton-with-mouseevent/135208 "2008-04-19T07:36:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![blitzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecae2f/32.png) [@blitzer](https://www.ruby-forum.com/u/blitzer)\
**Post date:** [April 19, 2008, 7:36am UTC](https://www.ruby-forum.com/t/use-bitmapbutton-with-mouseevent/135208/1 "2008-04-19T07:36:01Z")

</div>

hi. I want to make button that can change picture when mouse is entering  
and can click to use function. and this is my code.

```
@button2 = BitmapButton.new(@window, -1, @picturebox1_bmp,

```

Point.new(248,89), Size.new(53, 53))  
@button2.evt\_mouse\_events{ |event| evtmouse2(event) }  
evt\_button(@button2.get\_id()) { |event| evtbtn2()}

```
def evtmouse2(event)
  if event.entering == true
    @button2.set_bitmap_label(@picturebox2_bmp)
  end
  if event.leaving == true
    @button2.set_bitmap_label(@picturebox1_bmp)
  end
end

def evtbtn2()
  #mycode
end

```

It work normally when I use mouse enter the button it can change  
picture. but when I click on it. it can’t use evtbtn2. what I doing  
wrong?? I try to use event.left\_down to capture when mouse was clicked.  
It work but the button show was not pushed. how can I solve it?

---

<div class="post-metadata">

**Author:** ![blitzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecae2f/32.png) [@blitzer](https://www.ruby-forum.com/u/blitzer)\
**Post date:** [April 19, 2008, 3:45pm UTC](https://www.ruby-forum.com/t/use-bitmapbutton-with-mouseevent/135208/2 "2008-04-19T15:45:02Z")

</div>

Pat K. wrote:

> ```
> def evtmouse2(event)
> if event.entering == true
> @button2.set_bitmap_label(@picturebox2_bmp)
> end
> if event.leaving == true
> @button2.set_bitmap_label(@picturebox1_bmp)
> end
> 
> ```

Add ‘event.skip’ here

> It work but the button show was not pushed.

This is a common gotcha with event handling. The explanation is here:  
[http://wxruby.rubyforge.org/doc/event.html#Event\_skip](http://wxruby.rubyforge.org/doc/event.html#Event_skip)

a

---

<div class="post-metadata">

**Author:** ![blitzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecae2f/32.png) [@blitzer](https://www.ruby-forum.com/u/blitzer)\
**Post date:** [April 19, 2008, 5:45pm UTC](https://www.ruby-forum.com/t/use-bitmapbutton-with-mouseevent/135208/3 "2008-04-19T17:45:35Z")

</div>

thank you it work! 😃

---

<div class="post-metadata">

**Author:** ![blitzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecae2f/32.png) [@blitzer](https://www.ruby-forum.com/u/blitzer)\
**Post date:** [April 19, 2008, 10:39pm UTC](https://www.ruby-forum.com/t/use-bitmapbutton-with-mouseevent/135208/4 "2008-04-19T22:39:08Z")

</div>

Minor change  
[http://wxruby.rubyforge.org/doc/bitmapbutton.html#BitmapButton\_setbitmapselected](http://wxruby.rubyforge.org/doc/bitmapbutton.html#BitmapButton_setbitmapselected)

> BitmapButton#set\_bitmap\_disable()[http://wxruby.rubyforge.org/doc/bitmapbutton.html#BitmapButton\_setbitmapdisable](http://wxruby.rubyforge.org/doc/bitmapbutton.html#BitmapButton_setbitmapdisable)

Should be:

BitmapButton#set\_bitmap\_disabled()[http://wxruby.rubyforge.org/doc/bitmapbutton.html#BitmapButton\_setbitmapdisabled](http://wxruby.rubyforge.org/doc/bitmapbutton.html#BitmapButton_setbitmapdisabled)

My mistake.

L8ers

---

<div class="post-metadata">

**Author:** ![blitzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecae2f/32.png) [@blitzer](https://www.ruby-forum.com/u/blitzer)\
**Post date:** [April 19, 2008, 10:38pm UTC](https://www.ruby-forum.com/t/use-bitmapbutton-with-mouseevent/135208/5 "2008-04-19T22:38:33Z")

</div>

Hello Pat,

On Sat, Apr 19, 2008 at 12:36 AM, Pat K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> def evtmouse2(event)  
> #mycode  
> end
> 
> ## It work normally when I use mouse enter the button it can change picture. but when I click on it. it can’t use evtbtn2. what I doing wrong?? I try to use event.left\_down to capture when mouse was clicked. It work but the button show was not pushed. how can I solve it?

Another, easier way to implement this type of functionality, is to use  
the  
following given methods:

BitmapButton#set\_bitmap\_focus()[http://wxruby.rubyforge.org/doc/bitmapbutton.html#BitmapButton\_setbitmapfocus](http://wxruby.rubyforge.org/doc/bitmapbutton.html#BitmapButton_setbitmapfocus)  
BitmapButton#set\_bitmap\_hover()[http://wxruby.rubyforge.org/doc/bitmapbutton.html#BitmapButton\_setbitmaphover](http://wxruby.rubyforge.org/doc/bitmapbutton.html#BitmapButton_setbitmaphover)  
BitmapButton#set\_bitmap\_selected()[http://wxruby.rubyforge.org/doc/bitmapbutton.html#BitmapButton\_setbitmapselected](http://wxruby.rubyforge.org/doc/bitmapbutton.html#BitmapButton_setbitmapselected)  
BitmapButton#set\_bitmap\_disable()[http://wxruby.rubyforge.org/doc/bitmapbutton.html#BitmapButton\_setbitmapdisable](http://wxruby.rubyforge.org/doc/bitmapbutton.html#BitmapButton_setbitmapdisable)

Though, the way you have now, gives you an idea of how to do things,  
this  
sort of thing is already implemented, just so ya know. 😉
