次の方法で共有


方法 : 特定のデータ形式でデータを取得する

更新 : 2007 年 11 月

指定された形式でデータ オブジェクトからデータを取得する方法を次の例に示します。

説明

次のコード例では、GetDataPresent(String) オーバーロードを使用して、指定したデータ形式が (ネイティブで、または自動変換により) 使用可能かどうかを最初に確認します。指定した形式が使用可能な場合は、GetData(String) メソッドを使用してデータを取得します。

コード

DataObject dataObject = new DataObject("Some string data to store...");

string desiredFormat = DataFormats.UnicodeText;
byte[] data = null;

// Use the GetDataPresent method to check for the presence of a desired data format.
// This particular overload of GetDataPresent looks for both native and auto-convertible 
// data formats.
if (dataObject.GetDataPresent(desiredFormat))
{
    // If the desired data format is present, use one of the GetData methods to retrieve the
    // data from the data object.
    data = dataObject.GetData(desiredFormat) as byte[];
}

説明

次のコード例では、GetDataPresent(String, Boolean) オーバーロードを使用して、指定したデータ形式がネイティブで使用可能かどうか (自動変換可能なデータ形式はフィルタ処理されます) を最初に確認します。指定した形式が使用可能な場合は、GetData(String) メソッドを使用してデータを取得します。

コード

DataObject dataObject = new DataObject("Some string data to store...");

string desiredFormat = DataFormats.UnicodeText;
bool noAutoConvert = false;
byte[] data = null;

// Use the GetDataPresent method to check for the presence of a desired data format.
// The autoconvert parameter is set to false to filter out auto-convertible data formats,
// returning true only if the specified data format is available natively.
if (dataObject.GetDataPresent(desiredFormat, noAutoConvert))
{
    // If the desired data format is present, use one of the GetData methods to retrieve the
    // data from the data object.
    data = dataObject.GetData(desiredFormat) as byte[];
}

参照

概念

ドラッグ アンド ドロップの概要

参照

IDataObject

その他の技術情報

ドラッグ アンド ドロップのサンプル