Jump to content


Photo

Copy & Paste Selected text in TUniMemo

unimemo memo

  • Please log in to reply
8 replies to this topic

#1 dunham

dunham

    Member

  • uniGUI Subscriber
  • PipPip
  • 29 posts

Posted 15 December 2015 - 01:54 PM

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;


  • 0

#2 Delphi Developer

Delphi Developer

    Advanced Member

  • Moderators
  • 5165 posts

Posted 15 December 2015 - 07:08 PM

Hi,

 

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

http://stackoverflow...d-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.


  • 0

#3 dunham

dunham

    Member

  • uniGUI Subscriber
  • PipPip
  • 29 posts

Posted 20 January 2016 - 10:12 AM

Hi,

 

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

http://stackoverflow...d-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;


  • 0

#4 dunham

dunham

    Member

  • uniGUI Subscriber
  • PipPip
  • 29 posts

Posted 20 January 2016 - 10:17 AM

here is the test -demo project 

 

www.solidfiles.com/d/e522ff67cc/

http://www.solidfiles.com/d/e522ff67cc/

  • 0

#5 dunham

dunham

    Member

  • uniGUI Subscriber
  • PipPip
  • 29 posts

Posted 20 January 2016 - 02:56 PM

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


  • 0

#6 Delphi Developer

Delphi Developer

    Advanced Member

  • Moderators
  • 5165 posts

Posted 20 January 2016 - 03:31 PM

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...d-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


  • 0

#7 dunham

dunham

    Member

  • uniGUI Subscriber
  • PipPip
  • 29 posts

Posted 21 January 2016 - 08:20 AM

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 


  • 0

#8 Agus Widodo

Agus Widodo

    Newbie

  • uniGUI Subscriber
  • Pip
  • 3 posts

Posted 20 December 2017 - 03:05 AM

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';


  • 0

#9 Oliver Morsch

Oliver Morsch

    Advanced Member

  • uniGUI Subscriber
  • PipPipPipPip
  • 269 posts
  • LocationGermany

Posted 20 December 2017 - 10:15 AM

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!


  • 0





Also tagged with one or more of these keywords: unimemo, memo

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users