Retrieves the data associated with the specified data format, using autoConvert to determine whether to convert the data to the format.
[Visual Basic] Overloads Overridable Public Function GetData( _ ByVal format As String, _ ByVal autoConvert As Boolean _ ) As Object [C#] public virtual object GetData( string format, bool autoConvert ); [C++] public: virtual Object* GetData( String* format, bool autoConvert ); [JScript] public function GetData( format : String, autoConvert : Boolean ) : Object;
The data associated with the specified format, or a null reference (in Visual Basic Nothing).
If autoConvert 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 autoConvert set to false, this method returns a null reference (Nothing).
If autoConvert is false, this method returns data in the specified format, or a null reference (Nothing) 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.
Note Data can be converted to another format if it was stored specifying that conversion is allowed, and if the requested format is compatable with the stored format. For example, data stored as Unicode can be converted to text.
The following example retrieves the data stored in a DataObject, using autoConvert to specify whether to convert the data format.
First, a new DataObject is created with text data. Then, we try to retrieve the data, specifying its format as Unicode and no format conversion, that is autoConvert is false. This operation fails because there is no Unicode data in the DataObject.
Next, we try to retrieve the data again, specifying its format as Unicode and with autoConvert set to true. This operation succeeds and the results are displayed in a MessageBox.
This code assumes Button1 has been instantiated.
[C#]
private void GetMyData() { //Create a new data object using a string and the text format. string myString = "My new text string"; DataObject myDataObject = new DataObject(DataFormats.Text, myString); //Print the string in a text box with autoconvert = false. if(myDataObject.GetData(DataFormats.UnicodeText, false) != null) { //Print the string in a text box. textBox1.Text = myDataObject.GetData(DataFormats.UnicodeText, false).ToString() + '\n'; } else textBox1.Text = "Could not convert data to specified format" + '\n'; //Print the string in a text box with autoconvert = true. textBox1.Text += myDataObject.GetData(DataFormats.UnicodeText, true).ToString(); }
DataObject Class | DataObject Members | System.WinForms Namespace | DataObject.GetData Overload List | SetData | GetDataPresent | DataFormats | GetFormats