DataFormats (Clase)
Actualización: noviembre 2007
Proporciona nombres de formato de Clipboard predefinidos y static. Pueden utilizarse para identificar el formato de los datos almacenados en un objeto IDataObject.
Ensamblado: System.Windows.Forms (en System.Windows.Forms.dll)
Las clases IDataObject y DataObject también utilizan la lista de formatos static para determinar el tipo de datos que se recuperan del Portapapeles Clipboard del sistema o que se transfieren en una operación de arrastrar y colocar.
El método GetFormat permite:
-
Obtener un objeto DataFormats.Format predefinido para un nombre de formato o número de identificador.
-
Agregar un nuevo par nombre/número de identificador de formato a la lista estática static de esta clase, así como registrar el formato en el Registro de Windows como formato de tipo Clipboard al pasarlo en el nombre de formato.
Se puede obtener el número Id o el nombre Name del formato a partir de la propiedad correspondiente en la instancia de DataFormats.Format.
En el ejemplo de código siguiente se crea un nuevo formato de datos denominado myFormat. A continuación, el código crea un objeto MyNewObject y lo almacena en otro objeto DataObject. El objeto DataObject se copia en el Portapapeles (Clipboard).
Después, se recupera DataObject de Clipboard y también se recupera MyNewObject. El valor de MyNewObject se imprime en un cuadro de texto. En este fragmento de código se supone que se ha creado textBox1 y se ha colocado en un formulario.
using System; using System.Windows.Forms; public class MyClass : Form { protected TextBox textBox1; public void MyClipboardMethod() { // Creates a new data format. DataFormats.Format myFormat = DataFormats.GetFormat("myFormat"); /* Creates a new object and stores it in a DataObject using myFormat * as the type of format. */ MyNewObject myObject = new MyNewObject(); DataObject myDataObject = new DataObject(myFormat.Name, myObject); // Copies myObject into the clipboard. Clipboard.SetDataObject(myDataObject); // Performs some processing steps. // Retrieves the data from the clipboard. IDataObject myRetrievedObject = Clipboard.GetDataObject(); // Converts the IDataObject type to MyNewObject type. MyNewObject myDereferencedObject = (MyNewObject)myRetrievedObject.GetData(myFormat.Name); // Prints the value of the Object in a textBox. textBox1.Text = myDereferencedObject.MyObjectValue; } } // Creates a new type. [Serializable] public class MyNewObject : Object { private string myValue; // Creates a default constructor for the class. public MyNewObject() { myValue = "This is the value of the class"; } // Creates a property to retrieve or set the value. public string MyObjectValue { get { return myValue; } set { myValue = value; } } }
import System.*;
import System.Windows.Forms.*;
public class MyClass extends Form
{
protected TextBox textBox1;
public void MyClipboardMethod()
{
// Creates a new data format.
DataFormats.Format myFormat = DataFormats.GetFormat("myFormat");
/* Creates a new object and stores it in a DataObject using myFormat
as the type of format.
*/
MyNewObject myObject = new MyNewObject();
DataObject myDataObject = new DataObject(myFormat.get_Name(),myObject);
// Copies myObject into the clipboard.
Clipboard.SetDataObject(myDataObject);
// Performs some processing steps.
// Retrieves the data from the clipboard.
IDataObject myRetrievedObject = Clipboard.GetDataObject();
// Converts the IDataObject type to MyNewObject type.
MyNewObject myDereferencedObject =
(MyNewObject)(myRetrievedObject.GetData(myFormat.get_Name()));
// Prints the value of the Object in a textBox.
textBox1.set_Text(myDereferencedObject.get_MyObjectValue());
} //MyClipboardMethod
} //MyClass
// Creates a new type.
/** @attribute Serializable()
*/
public class MyNewObject extends Object
{
private String myValue;
// Creates a default constructor for the class.
public MyNewObject()
{
myValue = "This is the value of the class";
} //MyNewObject
// Creates a property to retrieve or set the value.
/** @property
*/
public String get_MyObjectValue()
{
return myValue;
} //get_MyObjectValue
/** @property
*/
public void set_MyObjectValue(String value)
{
myValue = value;
} //set_MyObjectValue
} //MyNewObject
import System; import System.Windows.Forms; public class MyClass extends Form { protected var textBox1 : TextBox; public function MyClipboardMethod() { // Create a new data format. var myFormat : DataFormats.Format = DataFormats.GetFormat("myFormat"); /* Create a new object and store it in a DataObject import the myFormat * as the type of format. */ var myObject : MyNewObject = new MyNewObject(); var myDataObject : DataObject = new DataObject("myFormat", myObject); // Copy myObject into the clipboard. Clipboard.SetDataObject(myDataObject); // Perform some processing steps. // Retrieve the data from the clipboard. var myRetrievedObject : IDataObject = Clipboard.GetDataObject(); // Convert the IDataObject type to MyNewObject type. var myDereferencedObject : MyNewObject = MyNewObject(myRetrievedObject.GetData("myFormat")); // Print the value of the Object in a textBox. textBox1.Text = myDereferencedObject.MyObjectValue; } } // Create a new type. Serializable public class MyNewObject extends Object { private var myValue : String; // Create a default constructor for the class. public function MyNewObject() { myValue = "This is the value of the class"; } // Create a property to retrieve or set the value. public function get MyObjectValue() : String { return myValue; } public function set MyObjectValue(value : String) { myValue = value; } }
Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile para Smartphone, Windows Mobile para Pocket PC
.NET Framework y .NET Compact Framework no admiten todas las versiones de cada plataforma. Para obtener una lista de las versiones compatibles, vea Requisitos de sistema de .NET Framework.