Läs på engelska Redigera

Dela via


IDataObject.GetData Method

Definition

Retrieves the data associated with the specified data format.

Overloads

GetData(String)

Retrieves the data associated with the specified data format.

GetData(Type)

Retrieves the data associated with the specified class type format.

GetData(String, Boolean)

Retrieves the data associated with the specified data format, using a Boolean to determine whether to convert the data to the format.

GetData(String)

Source:
IDataObject.cs
Source:
IDataObject.cs
Source:
IDataObject.cs

Retrieves the data associated with the specified data format.

C#
public object GetData(string format);
C#
public object? GetData(string format);

Parameters

format
String

The format of the data to retrieve. See DataFormats for predefined formats.

Returns

The data associated with the specified format, or null.

Examples

This example uses the DataObject class, which implements IDataObject, to demonstrate the use of the GetData method. The method is used to retrieve the data stored in myDataObject, which is associated with the Text format. The example assumes that you have already created a Form named Form1 and a TextBox named textBox1.

C#
private void GetData1() 
{
    // Creates a new data object using a string and the text format.
    string myString = "My text string";
    DataObject myDataObject = new DataObject(DataFormats.Text, myString);

    // Displays the string in a text box.
    textBox1.Text = myDataObject.GetData(DataFormats.Text).ToString();
}

Remarks

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, this method returns null.

To determine whether data is associated with, or can be converted to, a format, call GetDataPresent before calling GetData. Call GetFormats for a list of valid formats for the data stored in this instance.

Anteckning

Data can be converted to another format if it was stored specifying that conversion is allowed, and if the requested format is compatible with the stored format. For example, data stored as Unicode can be converted to text.

For an implementation of this method, see DataObject.GetData.

See also

Applies to

.NET Framework 4.8.1 och andra versioner
Produkt Versioner
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

GetData(Type)

Source:
IDataObject.cs
Source:
IDataObject.cs
Source:
IDataObject.cs

Retrieves the data associated with the specified class type format.

C#
public object GetData(Type format);
C#
public object? GetData(Type format);

Parameters

format
Type

A Type representing the format of the data to retrieve. See DataFormats for predefined formats.

Returns

The data associated with the specified format, or null.

Examples

This example uses the DataObject class, which implements IDataObject, to demonstrate the use of the GetData method. The method is used to retrieve the data stored in myObject, which is associated with a specific type, myType. The type of the retrieved data is displayed in a message box. The example assumes that you have already created a Form named Form1.

C#
       private void GetData2() 
       {
           // Creates a component.
           Component myComponent = new Component();

           // Creates a data object, and assigns it the component.
           DataObject myDataObject = new DataObject(myComponent);

           // Creates a type, myType, to store the type of data.
           Type myType = myComponent.GetType();

           // Retrieves the data using myType to represent its type.
           Object myObject = myDataObject.GetData(myType);
           if(myObject != null)
               MessageBox.Show("The data type stored in the data object is " +
                   myObject.GetType().Name + ".");
           else
               MessageBox.Show("Data of the specified type was not stored " +
                   "in the data object.");
       }

Remarks

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, this method returns null.

To determine whether data is associated with, or can be converted to, a format, call GetDataPresent before calling GetData. Call GetFormats for a list of valid formats for the data stored in this instance.

Anteckning

Data can be converted to another format if it was stored specifying that conversion is allowed, and if the requested format is compatible with the stored format. For example, data stored as Unicode can be converted to text.

For an implementation of this method, see DataObject.GetData.

See also

Applies to

.NET Framework 4.8.1 och andra versioner
Produkt Versioner
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

GetData(String, Boolean)

Source:
IDataObject.cs
Source:
IDataObject.cs
Source:
IDataObject.cs

Retrieves the data associated with the specified data format, using a Boolean to determine whether to convert the data to the format.

C#
public object GetData(string format, bool autoConvert);
C#
public object? GetData(string format, bool autoConvert);

Parameters

format
String

The format of the data to retrieve. See DataFormats for predefined formats.

autoConvert
Boolean

true to convert the data to the specified format; otherwise, false.

Returns

The data associated with the specified format, or null.

Examples

This example uses the DataObject class, which implements IDataObject, to demonstrate the use of the GetData method. The example retrieves the data stored in a DataObject, using the autoConvert parameter to specify whether or not to convert the data format. First, myDataObject is created with text data. Then the example tries twice to retrieve the data. In the first trial, it specifies its format as a string and sets the autoConvert parameter to false. This trial fails, and the result is displayed in a message box labeled "Message #1." In the second trial, the example retrieves the same data with the autoConvert parameter set to true. This trial succeeds, and the result is displayed in a message box labeled "Message #2." The example assumes that you have created a Form named Form1.

C#
       private void GetData3() 
       {
           // Creates a new data object using a text string.
           string myString = "Hello World!";
           DataObject myDataObject = new DataObject(DataFormats.Text, myString);

           // Displays the string with autoConvert equal to false.
           if (myDataObject.GetData("System.String", false) != null) 
           {
               // Displays the string in a message box.
               MessageBox.Show(myDataObject.GetData("System.String", false).ToString() + ".", "Message #1");
           } 
           else
           {
               // Displays a not found message in a message box.
               MessageBox.Show("Could not find data of the specified format.", "Message #1");
           }

           // Displays the string in a text box with autoConvert equal to true.
           string myData = "The data is " + myDataObject.GetData("System.String", true).ToString() +".";
           MessageBox.Show(myData,"Message #2");
       }

Remarks

If the autoConvert parameter is true and 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 the autoConvert parameter set to false, this method returns null.

If the autoConvert parameter is false, this method returns data in the specified format, or null if no data in this format can be found.

To determine whether data is associated with, or can be converted to, a format, call GetDataPresent before calling GetData. Call GetFormats for a list of valid formats for the data stored in this instance.

Anteckning

Data can be converted to another format if it was stored specifying that conversion is allowed, and if the requested format is compatible with the stored format. For example, data stored as Unicode can be converted to text.

For an implementation of this method, see DataObject.GetData.

See also

Applies to

.NET Framework 4.8.1 och andra versioner
Produkt Versioner
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10