Clipboard.GetText() not working

Created at 01 May 2025, 16:34
EY

eynt

Joined 08.05.2020

Clipboard.GetText() not working
01 May 2025, 16:34


Hello

I'm trying to use Clipboard.GetText() on an indicator but all I get is an empty string.

Please help, thank you


@eynt
Replies

firemyst
02 May 2025, 00:30

stackoverflow . com /questions/4648718/why-doesnt-clipboard-gettext-work


@firemyst

eynt
02 May 2025, 05:50

RE: Clipboard.GetText() not working

Hi

 

This is the print I got from the code below after copying some text to the clipboard. The a and b prints are working well but not the one of the Clipboard.GetText():

 

02/05/2025 05:46:32.491 | Info | Indicator instance [CopyText, EURUSD, h1] loaded.
02/05/2025 05:46:35.257 | Info | a
02/05/2025 05:46:35.272 | Info | 
02/05/2025 05:46:35.272 | Info | b
 

namespace cAlgo
{
    [Indicator(IsOverlay = true, AccessRights = AccessRights.FullAccess)]
    public class CopyText : Indicator
    {
        [STAThread]
        protected override void Initialize()
        {
            Print("a");
            string text = System.Windows.Forms.Clipboard.GetText();
            Print(text);
            Print("b");
        }

        public override void Calculate(int index)
        {
        }
    }
}

@eynt

firemyst
02 May 2025, 05:55

RE: RE: Clipboard.GetText() not working

eynt said: 

Hi

 

This is the print I got from the code below after copying some text to the clipboard. The a and b prints are working well but not the one of the Clipboard.GetText():

 

02/05/2025 05:46:32.491 | Info | Indicator instance [CopyText, EURUSD, h1] loaded.
02/05/2025 05:46:35.257 | Info | a
02/05/2025 05:46:35.272 | Info | 
02/05/2025 05:46:35.272 | Info | b
 

namespace cAlgo{    [Indicator(IsOverlay = true, AccessRights = AccessRights.FullAccess)]    public class CopyText : Indicator    {        [STAThread]        protected override void Initialize()        {            Print("a");            string text = System.Windows.Forms.Clipboard.GetText();            Print(text);            Print("b");        }        public override void Calculate(int index)        {        }    }}

The Clipboard might not even be available in bots. They do have limitations. Not sure why you'd want access to the clipboard anyway for an indicator?


@firemyst

eynt
02 May 2025, 05:58

RE: RE: RE: Clipboard.GetText() not working

I create a button which once clicked gets the text from the clipboard and runs code based on it


@eynt

firemyst
02 May 2025, 06:04

RE: RE: RE: RE: Clipboard.GetText() not working

eynt said: 

I create a button which once clicked gets the text from the clipboard and runs code based on it

 

And that text can't be written to a text file instead and read from there? 

Or copied into a parameter text input field and then the parameter's value read? 

Or set in a global static variable?


@firemyst

eynt
02 May 2025, 07:54

RE: RE: RE: RE: RE: Clipboard.GetText() not working

These are all too complex, I don't see a reason why a simple copy-paste shouldn't wotk


@eynt

firemyst
02 May 2025, 08:17

RE: RE: RE: RE: RE: RE: Clipboard.GetText() not working

eynt said: 

These are all too complex, I don't see a reason why a simple copy-paste shouldn't wotk

 

Setting a global static C# variable is took complex? 

All you do is assign it or read it like any other variable. If that's too complex, I'm not sure you should be coding anything.

//To create a "global variable", it should be public and static, and declared in a public static class. 
//In .NET it's a common way to declare constants (e.g. Math.PI), but they're not variables!

public static class EveryoneCanSeeMe
{
    public static string EveryOneCanReadAndModifyMe;
}

 

And as for reading/writing files, the first method below uses just as many lines of code as you need to read/write to the clipboard:

//Method #1 -----------------------
// Writing the string to the file 
File.WriteAllText(yourSavedFilePath, createText); 
  
// Reading the contents of the file 
string readText = File.ReadAllText(yourSavedFilePath); 

//Method #2 -----------------------
//WRite file
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(yourSavedFilePath, false))
{
	sw.Flush();
}

//Read file
if (System.IO.File.Exists(yourSavedFilePath)) 
{
    inputString = System.IO.File.ReadAllText(yourSavedFilePath));
    System.IO.File.Delete(yourSavedFilePath);
}

 

Again, nothing complex about them when you actually try instead of immediately dismissing because it's not how you would like to do it.

 


@firemyst