Clipboard.Gettext Returns Null (Empty String)

Clipboard.GetText returns null (empty string)

You can only access the clipboard from an STA thread. Rick Brewster ran into this with some refactoring of the regular Edit->Paste command, in Paint.NET.

Code:

IDataObject idat = null;
Exception threadEx = null;
Thread staThread = new Thread(
delegate ()
{
try
{
idat = Clipboard.GetDataObject();
}

catch (Exception ex)
{
threadEx = ex;
}
});
staThread.SetApartmentState(ApartmentState.STA);
staThread.Start();
staThread.Join();
// at this point either you have clipboard data or an exception

Code is from Rick. http://forums.getpaint.net/index.php?/topic/13712-/page__view__findpost__p__226140

Update: Jason Heine made a good point of adding () after delegate to fix the ambiguous method error.

Why doesn't Clipboard.GetText work?

You could try:

Clipboard.GetText(System.Windows.Forms.TextDataFormat.Text)

Or take a look here:
Clipboard.GetText returns null (empty string)

http://msdn.microsoft.com/es-en/library/system.windows.forms.clipboard.gettext.aspx

Clipboard is empty after setting text

Got it. Apparently even when Outlook is in HTML for the message format, you can't just paste HTML into it and have it rendered as a table. Instead, you have to convert the plain text into Rich Text Format, then paste it into Outlook. And apparently converting HTML to RTF in .NET doesn't have a great solution, you have to use a WebBrowser control and a RichTextBox from the Windows Forms library. Here's the code:

[STAThread]
public static void Main(string[] args)
{
//string input = "<table><tr><td>Hello, world!</td></tr></table>";
string input = Clipboard.GetText();
ConvertToRTF(input);
string text = Clipboard.GetText(TextDataFormat.Rtf);
Clipboard.SetText(text,TextDataFormat.Rtf);
}

public static void ConvertToRTF(string html)
{
RichTextBox rtbTemp = new RichTextBox();
WebBrowser wb = new WebBrowser();
wb.Navigate("about:blank");
wb.Document.Write(html);
wb.Document.ExecCommand("SelectAll", false, null);
wb.Document.ExecCommand("Copy", false, null);
rtbTemp.SelectAll();
rtbTemp.Paste();
rtbTemp.Copy();
}

Should I always call ContainsData before I get data from the Clipboard?

I really think you need it. Here is why:

If this method cannot find data in the specified format, it attempts
to convert the data to the format. If the data cannot be converted to
the specified format, or if the data was stored with automatic
conversion set to false, this method returns null.

So it's basically telling you that it's because of robust programming. You have to get results consistent with your intents: getting a null from an object when that object isn't might lead to problems.

After further considerations, I realized the problem isn't in the if statement itself, but in the returned value, because the GetData method already return null if the data in the clipboard can't be converted, so it's redundant and might be de-facto unuseful, considering doesn't give any additional information whether the data couldn't be converted or is actually empty.

At this point I would write a method like this:

public bool isClipboardDataValid(out string _data)
{
bool _isValid = false;
if (Clipboard.ContainsData(DataFormats.Text))
{
_data = Clipboard.GetData(DataFormats.Text);
_isValid = true;
}
return _isValid;
}

and then call this method hence getting that extra information.

c# Paste List MyObj return null

At the moment when I tried to individually add variables to the clipboard, it turned out that the problem was serialization. The problem was solved in SolodBrush because its attribute is Color, and only variables without attributes can be serialized.

There i found solution when i found what is real problem

How to get clipboard data in non main thread?

I finally used below method to access Clipboard text.

private string GetClipBoradData()
{
try
{
string clipboardData= null;
Exception threadEx = null;
Thread staThread = new Thread(
delegate ()
{
try
{
clipboardData= Clipboard.GetText(TextDataFormat.Text);
}

catch (Exception ex)
{
threadEx = ex;
}
});
staThread.SetApartmentState(ApartmentState.STA);
staThread.Start();
staThread.Join();
return clipboardData;
}
catch (Exception exception)
{
return string.Empty;
}
}


Related Topics



Leave a reply



Submit