Jump to content
uniGUI Discussion Forums
dunham

Copy & Paste Selected text in TUniMemo

Recommended Posts

Hello,

 

i'm searching to copy selected text from a UniMemo1 to clipboard

 

and paste text from clipboard to Unimemo2.

 

can you give me a solution please ?

 

Thank you;

Share this post


Link to post
Share on other sites

Hi,

 

I think, first of all, you need to analyze for example the following link:

http://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript

 

Or, if I understand correctly you, try the following solution:

procedure TMainForm.UniButton1Click(Sender: TObject);
begin
  UniSession.AddJS(
    'var _input = '+ UniMemo1.JSName +'.inputEl.dom;'+
    UniMemo2.JSName + '.setValue('+ UniMemo2.JSName +'.getValue() + _input.value.substring(_input.selectionStart, _input.selectionEnd))'
  );
end;

Best regards.

Share this post


Link to post
Share on other sites

Hi,

 

I think, first of all, you need to analyze for example the following link:

http://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript

 

Or, if I understand correctly you, try the following solution:

procedure TMainForm.UniButton1Click(Sender: TObject);
begin
  UniSession.AddJS(
    'var _input = '+ UniMemo1.JSName +'.inputEl.dom;'+
    UniMemo2.JSName + '.setValue('+ UniMemo2.JSName +'.getValue() + _input.value.substring(_input.selectionStart, _input.selectionEnd))'
  );
end;

Best regards.

 

this code works for internet explorer , actually not working with chrome/firefox

procedure TMainForm.BtnMemo1CopyClick(Sender: TObject);
begin
// copy to clipboard from Memo1
    UniSession.AddJS(
                     'var _input = '+ UniMemo1.JSName +'.inputEl.dom;' +
                     'var_text ='  + '_input.value.substring(_input.selectionStart, _input.selectionEnd);' +

                     //this code is working with internet explorer
                     'if( window.clipboardData && clipboardData.setData )'+
                     '{'+
                     'clipboardData.setData("Text", var_text);'+
                     ' } else { '+
                     //this code is not working with firefox/chrome
                     'unsafeWindow.netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");  '+
                     'const clipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"].getService(Components.interfaces.nsIClipboardHelper); '+
                     'clipboardHelper.copyString(var_text);'+
                     '}'

                     );
end;

here is my full code ( working only with Internet Explorer)


procedure TMainForm.BtnMemo1CopyClick(Sender: TObject);
begin
// copy to clipboard from Memo1
    UniSession.AddJS(
                     'var _input = '+ UniMemo1.JSName +'.inputEl.dom;' +
                     'var_text ='  + '_input.value.substring(_input.selectionStart, _input.selectionEnd);' +

                     //this code is working with internet explorer
                     'if( window.clipboardData && clipboardData.setData )'+
                     '{'+
                     'clipboardData.setData("Text", var_text);'+
                     ' } else { '+
                     //this code is not working with firefox/chrome
                     'unsafeWindow.netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");  '+
                     'const clipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"].getService(Components.interfaces.nsIClipboardHelper); '+
                     'clipboardHelper.copyString(var_text);'+
                     '}'

                     );
end;

procedure TMainForm.BtnMemo1PasteClick(Sender: TObject);
begin
// Paste from Clipboard to Memo1  (actually working only with internet explorer)
    UniSession.AddJS(
        'if( window.clipboardData && clipboardData.setData )'+
        '{'+
         UniMemo1.JSName +
         '.setValue(' +
         UniMemo1.JSName + '.getValue()'+
         '+"\n"+' +
         'window.clipboardData.getData("Text"));'+
         '}');
end;

procedure TMainForm.BtnMemo2CopyClick(Sender: TObject);
begin
// copy to clipboard from Memo2
    UniSession.AddJS('var _input = '+ UniMemo2.JSName +'.inputEl.dom;' +
                     'var_text ='  + '_input.value.substring(_input.selectionStart, _input.selectionEnd);' +

                     'if( window.clipboardData && clipboardData.setData )'+
                     '{'+
                     'clipboardData.setData("Text", var_text);'+
                     '}'+
                     'else'+
                     '{'+
                     'document.execCommand(''copy'');'+
                     '}');
end;

procedure TMainForm.BtnMemo2PasteClick(Sender: TObject);
begin
// Paste from Clipboard to Memo2  (actually working only with internet explorer)
    UniSession.AddJS(
        'if( window.clipboardData && clipboardData.setData )'+
        '{'+
         UniMemo2.JSName +
         '.setValue(' +
         UniMemo2.JSName + '.getValue()'+
         '+"\n"+' +
         'window.clipboardData.getData("Text"));'+
         '}');
end;

Share this post


Link to post
Share on other sites

does someone can help me please to get this code working for firefox ?

 

 

Hi,

 

I think, first of all, you need to analyze for example the following link:

http://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript

 

Or, if I understand correctly you, try the following solution:

procedure TMainForm.UniButton1Click(Sender: TObject);
begin
  UniSession.AddJS(
    'var _input = '+ UniMemo1.JSName +'.inputEl.dom;'+
    UniMemo2.JSName + '.setValue('+ UniMemo2.JSName +'.getValue() + _input.value.substring(_input.selectionStart, _input.selectionEnd))'
  );
end;

Best regards.

 

Hi,

 

Can you check again, this code works fine on FF.

 

Best regards

Share this post


Link to post
Share on other sites

Hi,

 

Can you check again, this code works fine on FF.

 

Best regards

 

 

Thank you for the reply, 

 

this code works for copy and paste between two memos but this is not what i need.

 

i'm searching to copy a memo 1 to windows clipboard 

 

if the user need later to paste the content from clipboard : paste content from clipboard into a memo 

Share this post


Link to post
Share on other sites

hello dunham..
probably u still need the answer.

please try this simple way. I tried , and working well

Uses Clipbrd;

 

// and use this
Clipboard.AsText := 'whatever string you want';

Share this post


Link to post
Share on other sites

hello dunham..

probably u still need the answer.

 

please try this simple way. I tried , and working well

 

Uses Clipbrd;

 

// and use this

Clipboard.AsText := 'whatever string you want';

 

This uses the clipboard on server side!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×