which control on userform HASE FOCUS

erik.van.geit

MrExcel MVP
Joined
Feb 1, 2003
Messages
17,832
Hello, gurus!

Here a selection of a tertbox is copied to the clipboard.
Code:
Private Sub copy_btn_Click()
Dim MyData As Object
Set MyData = New DataObject
MyData.SetText txt_tb.SelText
MyData.PutInClipboard
MsgBox MyData.GetText
End Sub
Now the clipboard should be pasted into the selected textbox.(after the cursor).
Code:
Private Sub paste_btn_Click()
Dim MyData As Object
Set MyData = New DataObject
MyData.GetFromClipboard
''''''' paste in the active textbox (after cursor) '''''''''''''' ????????
End Sub

who can help me ?
kind regards,
Erik
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Ciao Erik, I think the problem is that when you click onto the paste_btn, the active control becomes the pushbutton itself.
In my opinion is therefore necessary to use a static object which stores the last textbox which has lost the focus, and static variable which stores the cursor position.

To achieve your goal, copy the code below. I have called the destination text box (last active text box) as TextBox2 and used the event routine TextBox2_Exit. This routine is necessary for each potential destination textbox.

<font face=Courier New><SPAN style="color:#00007F">Public</SPAN> LastActiveTextBox <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>
<SPAN style="color:#00007F">Public</SPAN> ActTextBoxCurPos <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>

<SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> copy_btn_Click()
<SPAN style="color:#00007F">Dim</SPAN> MyData <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>
<SPAN style="color:#00007F">Set</SPAN> MyData = <SPAN style="color:#00007F">New</SPAN> DataObject
    MyData.SetText txt_tb.SelText
    MyData.PutInClipboard
    MsgBox MyData.GetText
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>


<SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> TextBox2_Exit(<SPAN style="color:#00007F">ByVal</SPAN> Cancel <SPAN style="color:#00007F">As</SPAN> MSForms.ReturnBoolean)
<SPAN style="color:#00007F">Set</SPAN> LastActiveTextBox = TextBox2
ActTextBoxCurPos = TextBox2.SelStart
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>


<SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> paste_btn_Click()
<SPAN style="color:#00007F">Dim</SPAN> MyData <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>
<SPAN style="color:#00007F">Set</SPAN> MyData = <SPAN style="color:#00007F">New</SPAN> DataObject
MyData.GetFromClipboard

<SPAN style="color:#007F00">''''''' paste in the active textbox (after cursor) '''''''''''''' ????????</SPAN>
<SPAN style="color:#00007F">Select</SPAN> <SPAN style="color:#00007F">Case</SPAN> ActTextBoxCurPos
    <SPAN style="color:#00007F">Case</SPAN> 0
        LastActiveTextBox.Text = MyData.GetText(1)
    <SPAN style="color:#00007F">Case</SPAN> Len(LastActiveTextBox)
        LastActiveTextBox.Text = LastActiveTextBox.Text & MyData.GetText
    <SPAN style="color:#00007F">Case</SPAN> <SPAN style="color:#00007F">Else</SPAN>
        LastActiveTextBox.Text = Left(LastActiveTextBox.Text, ActTextBoxCurPos) & MyData.GetText & _
            Right(LastActiveTextBox.Text, Len(LastActiveTextBox.Text) - ActTextBoxCurPos)
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Select</SPAN>

<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>


Post for feedback

:biggrin: :biggrin:
 
Upvote 0
Chiello,

Thanks for your response.
This will work!

I think the problem is that when you click onto the paste_btn, the active control becomes the pushbutton itself.

In the code below there is no problem of the kind: when clicking the COPY-button the programm still "knows" which text was selected. So it looks possible to me to do the "inverse" and paste in the activetextbox too.
Private Sub copy_btn_Click()
Dim MyData As Object
Set MyData = New DataObject
MyData.SetText txt_tb.SelText
MyData.PutInClipboard
MsgBox MyData.GetText
End Sub


it should make the code lighter
that's what I'm still hoping

kind regards,
Erik
 
Upvote 0

Forum statistics

Threads
1,203,101
Messages
6,053,536
Members
444,670
Latest member
laurenmjones1111

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top