All pastes #342745 Raw Edit

Miscellany

public text v1 · immutable
·published 2007-02-06 14:36 UTC
rendered paste body
see class DataFormat, which implements IDataObject interface

class Clipboard
Clipboard = new Clipboard();	
IDataObject iData = System.WinForms.Clipboard.GetDataObject();	

// ************************************************************************

SetDataObject()
-> to copy or cut data on the Clipboard

public static void SetDataObject(Object); 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- one parameter of Object type. That object could be any Data. For example:
string str = "Mahesh writing data to the Clipboard";
Clipboard.SetDataObject(str);	
You can put any kind of data on the clipboard by using this method.


public static void SetDataObject(Object data, bool copy);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The copy paramter specify whether the data should remain on the Clipboard after the application exists.
string str = "Mahesh writing data to the Clipboard using Copy bool";
Clipboard.SetDataObject(str, true);

// ************************************************************************

public static IDataObject GetDataObject();
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-> retrieves current data from the Clipboard. It returns IDataObject.

Example:
IDataObject dtObj = Clipboard.GetDataObject();	

// ************************************************************************
// copy to clipboard
private void CutCopyData() 
{
        Clipboard.SetDataObject(textBox1.SelectedText);
 }

// paste from clipboard
private void PasteData()
{
   IDataObject iData = Clipboard.GetDataObject();
   //Determine whether the data is in a format you can use.
   if(iData.GetDataPresent(DataFormats.Text))
  {
       string str = (String)iData.GetData(DataFormats.Text); 
   } 
}

// ************************************************************************
// paste from clipboard
IDataObject data = Clipboard.GetDataObject();
if (data.GetDataPresent(DataFormats.Text))
textBox1.Text = data.GetData(DataFormats.Text).ToString();

// ************************************************************************

private void button1_Click(object sender, System.EventArgs e) 
{
	// Takes the selected text from a text box and puts it on the clipboard.
      if(textBox1.SelectedText != "")
      	Clipboard.SetDataObject(textBox1.SelectedText);
      else
      	textBox2.Text = "No text selected in textBox1";
}
             
private void button2_Click(object sender, System.EventArgs e) 
{
	// Determines whether the data is in a format you can use.
      if(Clipboard.GetDataPresent(DataFormats.UnicodeText)) 
      {
      	// Yes it is, so display it in a text box.
            textBox2.Text = (String)Clipboard.GetData(DataFormats.UnicodeText); 
      }
      else 
      {
      	// No it is not.
            textBox2.Text = "Could not retrieve data off the clipboard.";
      }
}

// ************************************************************************

class System.Windows.Forms.Clipboard:
	GetDataObject
	SetDataObject

// ************************************************************************