# Mouse click event at grid

**URL:** <https://www.ruby-forum.com/t/mouse-click-event-at-grid/164320>\
**Category:** wxRuby\
**Created:** [March 31, 2009, 6:43am UTC](https://www.ruby-forum.com/t/mouse-click-event-at-grid/164320 "2009-03-31T06:43:58Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![hendra\_kusuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/5f8ce5/32.png) [@hendra\_kusuma](https://www.ruby-forum.com/u/hendra_kusuma)\
**Post date:** [March 31, 2009, 6:43am UTC](https://www.ruby-forum.com/t/mouse-click-event-at-grid/164320/1 "2009-03-31T06:43:58Z")

</div>

Dear all,

how do we create an mouse click event binding for a grid?  
I look over the docs and cannot find any clue about it.  
google did not help either.

Thank you  
Regards  
Hendra

---

<div class="post-metadata">

**Author:** ![hendra\_kusuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/5f8ce5/32.png) [@hendra\_kusuma](https://www.ruby-forum.com/u/hendra_kusuma)\
**Post date:** [March 31, 2009, 8:55am UTC](https://www.ruby-forum.com/t/mouse-click-event-at-grid/164320/2 "2009-03-31T08:55:19Z")

</div>

Hello Hendra,

You should be able to monitor evt\_left\_down, and evt\_left\_up for a click  
event. The actual detecting of a Click, would require that in  
evt\_left\_down, you store the mouse position, and the time at which the  
event  
was generated, then on evt\_left\_up, take the current time, and subtract  
the  
time at which your program received evt\_left\_down, to see if it’s within  
a  
normal time frame to qualify it as a click, and you would want to make  
sure  
that the mouse position matched the first one, then fire off your method  
you  
want to execute when a click occurs on the grid.

Take a look at the Event sample folder, for a way to do this, through a  
custom event handling system.

hth,

Mario

2009/3/30 hendra kusuma [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![hendra\_kusuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/5f8ce5/32.png) [@hendra\_kusuma](https://www.ruby-forum.com/u/hendra_kusuma)\
**Post date:** [March 31, 2009, 8:45am UTC](https://www.ruby-forum.com/t/mouse-click-event-at-grid/164320/3 "2009-03-31T08:45:56Z")

</div>

hendra kusuma wrote:

> how do we create an mouse click event binding for a grid?  
> I look over the docs and cannot find any clue about it.  
> google did not help either.

evt\_grid\_cell\_left\_click

Documented here: [http://docs.wxwidgets.org/stable/wx\_wxgrid.html#wxgrid](http://docs.wxwidgets.org/stable/wx_wxgrid.html#wxgrid)

Sorry, this appears to be missing from wxRuby docs. If in doubt, it can  
be worth searching the wxWidgets docs as a backup. Just prepend ‘wx’ to  
the class name for searching (eg wxGrid).

With any widget, you can also use the lower-level mouse event handlers,  
eg grid.evt\_left\_up

alex

---

<div class="post-metadata">

**Author:** ![hendra\_kusuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/5f8ce5/32.png) [@hendra\_kusuma](https://www.ruby-forum.com/u/hendra_kusuma)\
**Post date:** [April 1, 2009, 8:33am UTC](https://www.ruby-forum.com/t/mouse-click-event-at-grid/164320/4 "2009-04-01T08:33:39Z")

</div>

On Tue, Mar 31, 2009 at 1:25 PM, Alex F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Sorry, this appears to be missing from wxRuby docs. If in doubt, it can be  
> worth searching the wxWidgets docs as a backup. Just prepend ‘wx’ to the  
> class name for searching (eg wxGrid).
> 
> With any widget, you can also use the lower-level mouse event handlers, eg  
> grid.evt\_left\_up
> 
> alex

Thanks  
It works, with an unexpected behavior thought  
While I can catch the click event now,  
mouse click won’t move the selection to clicked cell  
anyway to fix this?

---

<div class="post-metadata">

**Author:** ![hendra\_kusuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/5f8ce5/32.png) [@hendra\_kusuma](https://www.ruby-forum.com/u/hendra_kusuma)\
**Post date:** [April 2, 2009, 5:46am UTC](https://www.ruby-forum.com/t/mouse-click-event-at-grid/164320/5 "2009-04-02T05:46:59Z")

</div>

On Wed, Apr 1, 2009 at 5:36 PM, Alex F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > It works, with an unexpected behavior thought  
> > While I can catch the click event now,  
> > mouse click won’t move the selection to clicked cell  
> > anyway to fix this?
> 
> If you trap an event like this, you must call “event.skip” in the event  
> handler if you want normal processing of the event (eg moving the selection  
> on a click) to proceed.

you mean something looks like this?  
evt\_grid\_cell\_left\_click() { do\_click() }

def do\_click()  
puts ‘ok’  
event.skip  
end

---

<div class="post-metadata">

**Author:** ![hendra\_kusuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/5f8ce5/32.png) [@hendra\_kusuma](https://www.ruby-forum.com/u/hendra_kusuma)\
**Post date:** [April 1, 2009, 12:46pm UTC](https://www.ruby-forum.com/t/mouse-click-event-at-grid/164320/6 "2009-04-01T12:46:28Z")

</div>

hendra kusuma wrote:

> On Tue, Mar 31, 2009 at 1:25 PM, Alex F. \<[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\> wrote:
> 
> ```
> With any widget, you can also use the lower-level mouse event
> handlers, eg grid.evt_left_up
> 
> ```

> It works, with an unexpected behavior thought  
> While I can catch the click event now,  
> mouse click won’t move the selection to clicked cell  
> anyway to fix this?

If you trap an event like this, you must call “event.skip” in the event  
handler if you want normal processing of the event (eg moving the  
selection on a click) to proceed.

a

---

<div class="post-metadata">

**Author:** ![hendra\_kusuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/5f8ce5/32.png) [@hendra\_kusuma](https://www.ruby-forum.com/u/hendra_kusuma)\
**Post date:** [April 2, 2009, 1:52pm UTC](https://www.ruby-forum.com/t/mouse-click-event-at-grid/164320/7 "2009-04-02T13:52:54Z")

</div>

I believe there are a couple of problems. First, you haven’t actually  
passed the GridEvent to your handler. Second, it seems you only want to  
call event.skip if event.get\_col is 0 (I’ve never used a Grid, I’m  
assuming  
the cols and rows are 0-indexed); It sounds like you want normal event  
processing to stop if they simply check the checkbox in the first  
column, ie  
the cell doesn’t get highlighted afterwards, and then normal event  
processing to continue on any other column. I think the code you want  
should probably look more like this:

evt\_grid\_cell\_left\_click() { |e| do\_click(e) } # pass GridEvent as e

def do\_click(event) # get the event passed earlier  
puts ‘ok’  
event.skip unless event.get\_col == 0 # do normal processing except on  
1st  
col  
end

Also, since evt\_grid\_cell\_left\_click doesn’t take an id parameter, it  
seems  
you’ll probably have to put that entire chunk of code inside of a class  
declaration for the Grid itself (either extending Grid or the variable  
referencing it). Otherwise wxRuby won’t know what control you’re trying  
to  
catch an event on. If there’s some other way around that, someone  
please  
correct me.

Hope this helps,  
–Steve

2009/4/1 hendra kusuma [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![hendra\_kusuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/5f8ce5/32.png) [@hendra\_kusuma](https://www.ruby-forum.com/u/hendra_kusuma)\
**Post date:** [April 2, 2009, 6:47am UTC](https://www.ruby-forum.com/t/mouse-click-event-at-grid/164320/8 "2009-04-02T06:47:45Z")

</div>

On Thu, Apr 2, 2009 at 10:15 AM, hendra kusuma  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> > > With any widget, you can also use the lower-level mouse event  
> > > If you trap an event like this, you must call “event.skip” in the event  
> > > end

Ups, Not working

Can you give me suggestion on my problem?  
This is what I want to do

I fill datagrid with all data from a table  
first column is boolean, an is set with set\_col\_format\_bool (appear as  
checkbox)  
I set all other columns to readonly  
so when I click the first column, it will change the value (true or  
false)  
and update the table (the primary key is at 2nd column)

current state  
I click cell on first column, move focus to clicked-cell  
click the cell again, cell change to textbox, I can change the value  
there

expected behavior  
click cell on first column, and it toggle the value between checked and  
unchecked

Thanks  
Hendra

---

<div class="post-metadata">

**Author:** ![hendra\_kusuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/5f8ce5/32.png) [@hendra\_kusuma](https://www.ruby-forum.com/u/hendra_kusuma)\
**Post date:** [April 3, 2009, 4:21am UTC](https://www.ruby-forum.com/t/mouse-click-event-at-grid/164320/9 "2009-04-03T04:21:30Z")

</div>

2009/4/2 Stephen B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> catch an event on. If there’s some other way around that, someone please  
> correct me.
> 
> Hope this helps,  
> –Steve

Thank you  
This almost help me  
Yes, I can catch the click on 1st column now, but the another problem  
arise  
the pointer does not move to the clicked cell  
This is what in my mind, when the 1st column is clicked

def clicked(event)  
if event.get\_col == 0  
pk = @grid.get\_cell\_value(@grid.get\_grid\_cursor\_row,1)  
val = @grid.get\_cell\_value(@grid.get\_grid\_cursor\_row,0)  
if val == ‘1’  
sql = “update table set val =0 where pk = #{pk}”  
@grid.set\_cell\_value(@grid.get\_grid\_cursor\_row,0, ‘0’)  
else  
sql = “update table set val =1 where pk = #{pk}”  
@grid.set\_cell\_value(@grid.get\_grid\_cursor\_row,0, ‘1’)  
end  
@conn.execute(sql)  
else  
event.skip  
end  
end

but @grid.get\_grid\_cursor\_row  
will return previous row, not clicked row

What to do now?  
also,  
I cannot find the syntax event.get\_col in documentation  
can you point me to the doc page? It would be very helpful

Many Thanks  
Hendra

---

<div class="post-metadata">

**Author:** ![hendra\_kusuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/5f8ce5/32.png) [@hendra\_kusuma](https://www.ruby-forum.com/u/hendra_kusuma)\
**Post date:** [April 3, 2009, 5:31am UTC](https://www.ruby-forum.com/t/mouse-click-event-at-grid/164320/10 "2009-04-03T05:31:51Z")

</div>

On Fri, Apr 3, 2009 at 9:05 AM, hendra kusuma [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > processing to continue on any other column. I think the code you want  
> > Also, since evt\_grid\_cell\_left\_click doesn’t take an id parameter, it  
> > Thank you  
> > sql = “update table set val =0 where pk = #{pk}”

I did it  
I found the doc page (Wx::GridEvent),  
figure out how to use event object  
and finally did it  
Now my grid work just the way I want it

the final source code looks like this  
def clicked(event)  
if event.get\_col == 0  
row = event.get\_row  
@grid.set\_grid\_cursor(row, 0)  
pk = @grid.get\_cell\_value(row,1)  
val = @grid.get\_cell\_value(row,0)  
if val == ‘0’  
@dataset.filter(:no =\> pk).update(:print =\> 1)  
@grid.set\_cell\_value(row,0,‘1’)  
else  
@dataset.filter(:no =\> pk).update(:print =\> 0)  
@grid.set\_cell\_value(row,0,‘0’)  
end  
else  
event.skip  
end  
end

Many thanks for all your help

Regards  
Hendra
