Google
 

Saturday, March 31, 2007

Use System.Windows.Forms.Clipboard in C#

  1. The Clipboard class is in the name space:System.Windows.Forms

  2. using  System;
    using System.Collections.Generic;
    using  System.Text;
    using System.Windows.Forms;

    namespace  Clipboard_Manipulation
    {
        
    class Program
        {
            [STAThread]
            
    static void Main(string [] args)
            {

                
    string clipboardContent =  string.Empty;
                
    if (Clipboard.ContainsText (TextDataFormat.CommaSeparatedValue))
                {
                    clipboardContent 
    = Clipboard.GetText(TextDataFormat.CommaSeparatedValue );
                    Clipboard.Clear();
                }
                
    else
                {
                    Clipboard.SetText(
    "This,is,CSV,Data,Just,Placed,On,The,Clipboard",
                        TextDataFormat.CommaSeparatedValue);
                    clipboardContent 
    =  "<There was no CSV data found on the clipboard>";
                }

                 Console.WriteLine(clipboardContent);

            }
        }
    }

  3. using  System;
    using System.Windows.Forms;

    namespace  ClipboardManipulation
    {
        
    /// <summary>
        
    /// Summary description for Class1.
        
    ///  </summary>
        class  Class1
        {
            
    /// <summary>
            
    /// The main entry point for the application.
            
    ///  </summary>
            [STAThread]
            
    static void Main(string [] args)
            {
                Console.WriteLine(GetClipboardContent());
                SetClipboardContent(
    "Kevin ");
                Console.WriteLine(
    "***************************** ");
                Console.WriteLine(GetClipboardContent());

            }

            
    public  static string GetClipboardContent()
            {
                IDataObject iData  
    = Clipboard.GetDataObject();
                
    string  sResult;

                
    if (iData.GetDataPresent(DataFormats.Text)) 
                {
                    sResult 
    = iData.GetData(DataFormats.Text).ToString();
                }
                
    else
                {
                    sResult 
    = "Could not retrieve data off the clipboard. ";
                }
                
    return sResult;
            }

            
    public static  void SetClipboardContent(string s)
            {
                 Clipboard.SetDataObject(s,
    true);
            }
        }
    }
  4. a

--
Happy day, happy life!

No comments: