共用方式為


HOW TO:列出資料物件中的資料格式

更新:2007 年 11 月

下列範例顯示如何使用 GetFormats 方法多載,取得代表資料物件中每個可用資料格式之字串的陣列。

範例

說明

下列範例程式碼會使用 GetFormats 多載取得字串陣列,這個字串陣列表示資料物件中所有可用的資料格式 (包括原生格式和可自動轉換的格式)。

程式碼

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

// Get an array of strings, each string denoting a data format
// that is available in the data object.  This overload of GetDataFormats
// returns all available data formats, native and auto-convertible.
string[] dataFormats = dataObject.GetFormats();

// Get the number of data formats present in the data object, including both
// auto-convertible and native data formats.
int numberOfDataFormats = dataFormats.Length;

// To enumerate the resulting array of data formats, and take some action when
// a particular data format is found, use a code structure similar to the following.
foreach (string dataFormat in dataFormats)
{
    if (dataFormat == DataFormats.Text)
    {
        // Take some action if/when data in the Text data format is found.
        break;
    }
    else if(dataFormat == DataFormats.StringFormat)
    {
        // Take some action if/when data in the string data format is found.
        break;
    }
}

範例

說明

下列範例程式碼使用 GetFormats 多載,取得僅代表資料物件中可用資料格式之字串的陣列 (可自動轉換的資料格式會篩選掉)。

程式碼

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

// Get an array of strings, each string denoting a data format
// that is available in the data object.  This overload of GetDataFormats
// accepts a Boolean parameter inidcating whether to include auto-convertible
// data formats, or only return native data formats.
string[] dataFormats = dataObject.GetFormats(false /* Include auto-convertible? */);

// Get the number of native data formats present in the data object.
int numberOfDataFormats = dataFormats.Length;

// To enumerate the resulting array of data formats, and take some action when
// a particular data format is found, use a code structure similar to the following.
foreach (string dataFormat in dataFormats)
{
    if (dataFormat == DataFormats.Text)
    {
        // Take some action if/when data in the Text data format is found.
        break;
    }
}

請參閱

概念

拖放概觀

參考

IDataObject

其他資源

拖放範例