データを指定形式に変換するかどうかを判断するための自動変換パラメータを使用して、このインスタンスに格納されているデータが指定したデータ形式に関連付けられているかどうかを確認します。
Overloads Public Overridable Function GetDataPresent( _
ByVal format As String, _ ByVal autoConvert As Boolean _) As Boolean Implements IDataObject.GetDataPresent
[C#]
public virtual bool GetDataPresent(stringformat,boolautoConvert);
[C++]
public: virtual bool GetDataPresent(String* format,boolautoConvert);
[JScript]
public function GetDataPresent(
format : String,autoConvert : Boolean) : Boolean;
パラメータ
- format
チェック対象となる形式。定義済みの形式については、 DataFormats のトピックを参照してください。 - autoConvert
インスタンスに格納されているデータを指定した形式に変換できるかどうかを確認する場合は true 。データが指定した形式かどうかを確認する場合は false 。
戻り値
データが指定した形式である場合や、その形式に変換できる場合は true 。それ以外の場合は false 。
実装
解説
GetData を呼び出す前に、書式が指定されているかどうかを確認する場合は、このメソッドを呼び出します。インスタンスで使用できる形式を取得するには、 GetFormats を呼び出します。
このメソッドは、次の場合に true を返します。
- autoConvert パラメータが true に設定されていて、データが適切な形式に変換できる場合。
- autoConvert パラメータが false に設定されていて、データが適切な形式である場合。
このメソッドは、次の場合に false を返します。
- autoConvert パラメータが true に設定されていて、このメソッドが指定した形式のデータを見つけることができず、データを指定形式に変換できない場合、またはデータの格納時に自動変換に false が設定されていた場合。
- autoConvert パラメータが false に設定されていて、指定した形式のデータがこのインスタンスに存在しない場合。
メモ 形式変換を有効にしてデータが格納されている場合、要求した形式が、格納されている形式と互換性があれば、そのデータを別の形式に変換できます。たとえば、Unicode として格納されたデータをテキストに変換できます。
メモ データを取得できない場合、例外はスローされません。代わりに false が返されます。
使用例
[Visual Basic, C#, C++] 現在 DataObject に格納されているデータが、指定された形式に関連付けられているかどうかを確認する例を次に示します。まず、新しいインスタンスを文字列で初期化します。文字列の形式をテキストに指定します。
[Visual Basic, C#, C++] 次に、 autoConvert パラメータを false に指定して、テキスト形式に関連付けられているデータを DataObject に問い合わせます。このクエリの結果はテキスト ボックスに表示されます。
[Visual Basic, C#, C++] 次に、 autoConvert パラメータを true に指定して、文字列形式に関連付けられているデータを DataObject に問い合わせます。結果はテキスト ボックスに表示されます。このコードは、 textBox1
が作成されていることを前提にしています。
Private Sub GetIfPresent3()
' Creates a new data object using a string and the text format.
Dim myDataObject As New DataObject(DataFormats.Text, "Another string")
' Prints the string in a text box with autoconvert = false.
If myDataObject.GetDataPresent("System.String", False) Then
' Prints the string in a text box.
textBox1.Text = myDataObject.GetData("System.String", False).ToString() & ControlChars.Cr
Else
textBox1.Text = "Could not convert data to specified format" & ControlChars.Cr
End If
' Prints the string in a text box with autoconvert = true.
textBox1.Text &= "With autoconvert = true, you can convert text to string format. " & _
"String is: " & myDataObject.GetData("System.String", True).ToString()
End Sub 'GetIfPresent3
[C#]
private void GetIfPresent3() {
// Creates a new data object using a string and the text format.
DataObject myDataObject = new DataObject(DataFormats.Text, "Another string");
// Prints the string in a text box with autoconvert = false.
if(myDataObject.GetDataPresent("System.String", false)) {
// Prints the string in a text box.
textBox1.Text = myDataObject.GetData("System.String", false).ToString() + '\n';
} else
textBox1.Text = "Could not convert data to specified format" + '\n';
// Prints the string in a text box with autoconvert = true.
textBox1.Text += "With autoconvert = true, you can convert text to string format. " +
"String is: " + myDataObject.GetData("System.String", true).ToString();
}
[C++]
private:
void GetIfPresent3() {
// Creates a new data object using a string and the text format.
DataObject* myDataObject = new DataObject(DataFormats::Text, S"Another string");
// Prints the string in a text box with autoconvert = false.
if(myDataObject->GetDataPresent(S"System.String", false)) {
// Prints the string in a text box.
textBox1->Text = String::Concat( myDataObject->GetData(S"System.String",false), S"\n");
} else
textBox1->Text = S"Could not convert data to specified format\n";
// Prints the string in a text box with autoconvert = true.
textBox1->Text = String::Format( S"{0}With autoconvert = true, you can convert text to string format. String is: {1}",
textBox1->Text, myDataObject->GetData(S"System.String",true) );
}
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ
参照
DataObject クラス | DataObject メンバ | System.Windows.Forms 名前空間 | DataObject.GetDataPresent オーバーロードの一覧 | SetData | GetData | DataFormats | GetFormats