Ruby Forum wxRuby > use bitmapButton with mouseEvent?

Posted by Pat Kiatchaipipat (blitzer)
on 19.04.2008 07:36
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?
Posted by Alex Fenton (Guest)
on 19.04.2008 15:45
(Received via mailing list)
Pat Kiatchaipipat 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

a
Posted by Pat Kiatchaipipat (blitzer)
on 19.04.2008 17:45
thank you it work! :D
Posted by Mario Steele (Guest)
on 19.04.2008 22:38
(Received via mailing list)
Hello Pat,

On Sat, Apr 19, 2008 at 12:36 AM, Pat Kiatchaipipat 
<lists@ruby-forum.com>
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>
BitmapButton#set_bitmap_hover()<http://wxruby.rubyforge.org/doc/bitmapbutton.html#BitmapButton_setbitmaphover>
BitmapButton#set_bitmap_selected()<http://wxruby.rubyforge.org/doc/bitmapbutton.html#BitmapButton_setbitmapselected>
BitmapButton#set_bitmap_disable()<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. ;-)
Posted by Mario Steele (Guest)
on 19.04.2008 22:39
(Received via mailing list)