Compartilhar via


Como: List the Data Formats in a Data Object

Os exemplos a seguir mostram como usar as sobrecargas do método GetFormats para obter uma array de strings indicando cada formato de dados que está disponível em um objeto de dados.

Exemplo

Descrição

O seguinte exemplo de código usa a sobrecarga GetFormats para obter um array de strings que indica todos os formatos de dados disponíveis em um objeto de dados (nativo e autoconversível).

Código

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;
    }
}

Exemplo

Descrição

O seguinte exemplo de código usa a sobrecarga GetFormats para obter uma array de strings indicando somente formatos de dados disponíveis em um objeto de dados (formatos de dados autoconversíveis são filtrados).

Código

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;
    }
}

Consulte também

Conceitos

Visão geral sobre arrastar e soltar

Referência

IDataObject

Outros recursos

Arrastar e Soltar exemplos