# Copy & paste event

**URL:** <https://www.ruby-forum.com/t/copy-paste-event/131242>\
**Category:** wxRuby\
**Created:** [March 7, 2008, 7:28am UTC](https://www.ruby-forum.com/t/copy-paste-event/131242 "2008-03-07T07:28:30Z")\
**Posts on this page:** 10\
**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:** [March 7, 2008, 7:28am UTC](https://www.ruby-forum.com/t/copy-paste-event/131242/1 "2008-03-07T07:28:30Z")

</div>

hi. now I have problem about copy and paste text in my application I  
make copy and paste button in toolbar and I have fuction of them but I  
don’t know how to make it can copy only selection text and paste into  
other textCtrl. see my picture for more info. thank you 🙂

---

<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:** [March 7, 2008, 9:54am UTC](https://www.ruby-forum.com/t/copy-paste-event/131242/2 "2008-03-07T09:54:35Z")

</div>

Hi

Pat K. wrote:

> hi. now I have problem about copy and paste text in my application I  
> make copy and paste button in toolbar and I have fuction of them but I  
> don’t know how to make it can copy only selection text and paste into  
> other textCtrl. see my picture for more info. thank you 🙂

If you just want ToolBar buttons that do the “normal” thing for  
cut/copy/paste - i.e. do that action to the current selection in the  
current active widget - just give the Tool the relevant standard id when  
you create it. For example

toolbar.add\_tool(Wx::ID\_COPY, ‘Cut’, bitmap\_1 …)

Or Wx::ID\_CUT, Wx::ID\_PASTE. No additional programming effort should be  
needed

alex

---

<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:** [March 7, 2008, 10:53am UTC](https://www.ruby-forum.com/t/copy-paste-event/131242/3 "2008-03-07T10:53:25Z")

</div>

I use

tb.add\_tool(ID\_COPY, “Copy”, ArtProvider::get\_bitmap(ART\_COPY))

but when I click it can’t copy some text  
what’s wrong :’(

---

<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:** [March 7, 2008, 11:43am UTC](https://www.ruby-forum.com/t/copy-paste-event/131242/4 "2008-03-07T11:43:46Z")

</div>

Pat K. wrote:

> tb.add\_tool(ID\_COPY, “Copy”, ArtProvider::get\_bitmap(ART\_COPY))
> 
> but when I click it can’t copy some text  
> what’s wrong 😢

Add something like

evt\_tool(Wx::ID\_COPY) :on\_copy

then create a method like:

def on\_copy  
text\_box.copy  
end

alex

---

<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:** [March 7, 2008, 12:19pm UTC](https://www.ruby-forum.com/t/copy-paste-event/131242/5 "2008-03-07T12:19:32Z")

</div>

:’(

```
tb.add_tool(ID_COPY, "Copy", ArtProvider::get_bitmap(ART_COPY))
evt_tool(ID_COPY){|event| on_copy()}

def on_copy()
  @textbox2.copy
end

```

then I copy it’s ok but it can copy only @textbox2 and and I have many  
textbox. how can I use it to copy only selected text not dependent  
textbox? I have @textbox1, @textbox2 and @textbox3

---

<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:** [March 7, 2008, 12:55pm UTC](https://www.ruby-forum.com/t/copy-paste-event/131242/6 "2008-03-07T12:55:07Z")

</div>

Pat K. wrote:

> 

IMHO it seems a confusing UI design to have a toolbar button that can  
interact with one of several different widgets depending on  
circumstances. Anyway, it’s up to you to decide the logic by which one  
of the text boxes is chosen to have its selection copied.

Perhaps, you could use evt\_set\_focus to track the most recently focussed  
textbox, or perhaps you could test each textbox in turn with  
get\_selection to see if it has a selection.

Suggest you spend some time trying things out and working through the  
wxruby samples and documentation to see what’s possible.

alex

---

<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:** [March 7, 2008, 1:10pm UTC](https://www.ruby-forum.com/t/copy-paste-event/131242/7 "2008-03-07T13:10:36Z")

</div>

Thank you I use it now!!  
😃

---

<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:** [March 7, 2008, 3:08pm UTC](https://www.ruby-forum.com/t/copy-paste-event/131242/8 "2008-03-07T15:08:26Z")

</div>

now I have problem with paste ☹  
when I copy I use this

```
  a = @textbox1.get_selection()
  b = @textbox2.get_selection()
  c = @textbox3.get_selection()
  if a[1] - a[0] != 0
    @textbox1.copy
  elsif b[1] - b[0] != 0
    @textbox2.copy
  elsif c[1] - c[0] != 0
    @textbox3.copy
  end

```

to check where @textbox was selected but when I want to paste it can’t  
use this I try to think but I don’t know how can I fix it please help me  
:’(

---

<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:** [March 7, 2008, 3:30pm UTC](https://www.ruby-forum.com/t/copy-paste-event/131242/9 "2008-03-07T15:30:43Z")

</div>

Pat K. wrote:

> to check where @textbox was selected but when I want to paste it can’t  
> use this I try to think but I don’t know how can I fix it please help me

[http://wxruby.rubyforge.org/doc/textctrl.html#TextCtrl\_paste](http://wxruby.rubyforge.org/doc/textctrl.html#TextCtrl_paste)

As for which TextCtrl to paste into, I have no idea because you haven’t  
given us any idea of what your app is meant to do.

If you wanted to track the last-focussed TextCtrl, it might be something  
like (untested):

evt\_child\_focus do | event |  
if [@textbox1, @textbox2, @textbox3].any?(event.window)  
@focussed\_textbox = event.window  
end  
end

When posting, it helps a lot if you write clearly what you want to do,  
what you’ve tried, and what you expected to happen. Don’t expect other  
people to guess or do your work for you.

alex

---

<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:** [March 7, 2008, 4:33pm UTC](https://www.ruby-forum.com/t/copy-paste-event/131242/10 "2008-03-07T16:33:19Z")

</div>

sorry, I will explain again 🙂

I have three textbox like this picture, and I have copy, paste, cut on  
toolbar and I want to make when user click at copy it will copy text  
that selected (I can do it now) and then I want to paste text in other  
textbox

example  
first I copy ‘sssss’ on left textbox, it’s ok. then I want to paste it  
in right textbox that I point into it (see picture)

if I use @textbox2.paste in function on\_paste. it’s ok but it can paste  
only right textbox when I point mouse at left textbox and paste it, it  
will appear at riht textbox :’(
