1

I'm working on a Windows application where I need to use clipboard data. I am trying to copy text from clipboard by the code below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace MultiValuedClipBoard
{
    class Class1
    {

        public String SwapClipboardHtmlText(String replacementHtmlText)
        {
            String returnHtmlText = "hello";
            if (Clipboard.ContainsText(TextDataFormat.Html))
            {
                returnHtmlText = Clipboard.GetText(TextDataFormat.Html);
                Clipboard.SetText(replacementHtmlText, TextDataFormat.Html);
            }
            return returnHtmlText;
        }
    }
}

Calling the above function by:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Media;

namespace MultiValuedClipBoard
{
    class Program
    {
        static void Main(string[] args)
        {
            Class1 aas = new Class1();
            string a = aas.SwapClipboardHtmlText("chetan");
            Console.WriteLine(a);
            Console.ReadLine();
        }
    }
}

When running this code it gives the output "Hello" which is the default value, not clipboard data.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hot Cool Stud
  • 1,145
  • 6
  • 25
  • 50

2 Answers2

2

Your code will not work because of two reasons:

[1] When you say:

if (Clipboard.ContainsText(TextDataFormat.Html))

Here you are basically assuming that the clipboard already contains a text and that too in HTML format, but depending on the values you are setting in the clipboard it doesn't look like you are intending to use the pre-existing clipboard value anywhere in your program. So, this if condition should not be there.

[2] Secondly, you are further trying to set the string "chetan" to the clipboard which is definitely not in HTML format. So,

            Clipboard.SetText(replacementHtmlText, TextDataFormat.Html);

becomes

            Clipboard.SetText(replacementHtmlText, TextDataFormat.Text);

Hence, effectively, your new code becomes something like this:

        String returnHtmlText = "hello";
        //if (Clipboard.ContainsText(TextDataFormat.Html))
        //{
            returnHtmlText = Clipboard.GetText(TextDataFormat.Text);
            Clipboard.SetText(replacementHtmlText, TextDataFormat.Text);
        //}
        return returnHtmlText;
Prahlad Yeri
  • 3,567
  • 4
  • 25
  • 55
  • i tried code suggested by you . But Clipboard.GetText(TextDataFormat.Text) returns null . It means clipboard doesn't have any value but i copied text before running this app . – Hot Cool Stud Mar 02 '13 at 07:59
  • Sometimes you run into problems related to blocked clipboards, as some other application might be keeping the clipboard locked. See this link: http://stackoverflow.com/questions/930219/how-to-handle-blocked-clipboard-and-other-oddities – Prahlad Yeri Mar 02 '13 at 08:04
  • It could also be an issue with your Windows-7 securities/permissions. Try this on an XP machine if you have one. – Prahlad Yeri Mar 02 '13 at 08:06
  • Thanx yeri for your valuable info but don't we have any solution for windows 7 .... – Hot Cool Stud Mar 02 '13 at 08:33
  • Try adding the [STAThread] attribute above your main function. See second-last answer in this SO thread: http://stackoverflow.com/questions/518701/clipboard-gettext-returns-null-empty-string – Prahlad Yeri Mar 02 '13 at 09:09
1

Clearly Clipboard.ContainsText(TextDataFormat.Html) evaluates to false. Which means that the clipboard in fact does not contain text in the format you specify.

I changed your program to prove the point:

[STAThread]
static void Main(string[] args)
{
    Clipboard.SetText("boo yah!", TextDataFormat.Html);
    Class1 aas = new Class1();
    string a = aas.SwapClipboardHtmlText("chetan");
    Console.WriteLine(a);
    Console.WriteLine(Clipboard.GetText(TextDataFormat.Html));
    Console.ReadLine();
}

Output:

boo yah!
chetan
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • your code works fine but whenever i tried to copy anything from windows clipboard without SetText() it returns null and before this i copied some text in clipboard . My App runs thread which automatically copy content from windows clipboard . – Hot Cool Stud Mar 02 '13 at 17:17
  • I don't know what that means. – David Heffernan Mar 02 '13 at 17:27
  • I need whenever i copy some text with cntrl+c i can retrieve that with Clipboard.GetText(TextDataFormat.text) is it possible ??? – Hot Cool Stud Mar 02 '13 at 17:34
  • yeah i also think so but it doesn't . Try above code without Clipboard.SetText("boo yah!", TextDataFormat.Html); . You will feel like me. – Hot Cool Stud Mar 02 '13 at 17:46
  • No, you are quite wrong. If the clipboard contains `TextDataFormat.Html` then it will be returned. You code is telling you that the clipboard does not contain `TextDataFormat.Html`. It is accurate when it does so. Read the first paragraph of my answer once more. That describes what is happening. – David Heffernan Mar 02 '13 at 17:48
  • Finally i m done with my question thanks for making me correct . – Hot Cool Stud Mar 02 '13 at 17:54