次の方法で共有


ConvertEventArgs.DesiredType プロパティ

必要な値のデータ型を取得します。

Public ReadOnly Property DesiredType As Type
[C#]
public Type DesiredType {get;}
[C++]
public: __property Type* get_DesiredType();
[JScript]
public function get DesiredType() : Type;

プロパティ値

必要な値の Type

解説

DesiredType プロパティを使用して、変換後の値となるプロパティの型を確認できます。

使用例

[Visual Basic, C#, C++] DesiredType プロパティを使用して、ある型から別の型への変換を続行できるかどうかを判断する例を次に示します。 DecimalToCurrencyString メソッドは、 DesiredType が文字列かどうかをテストします。文字列でない場合は、コードでこのメソッドを終了します。同様に、 CurrencyStringToDecimal メソッドは、 DesiredTypeDecimal かどうかをテストします。 true でない場合は、このメソッドを終了します。

 
Private Sub DecimalToCurrencyString(sender As Object, cevent As ConvertEventArgs)
   ' The method converts only to string type. 
   If Not cevent.DesiredType Is GetType(String) Then
      Return
   End If 
   cevent.Value = CDec(cevent.Value).ToString("c")
End Sub 
 
 
Private Sub CurrencyStringToDecimal(sender As Object, cevent As ConvertEventArgs)
   ' The method converts only to decimal type.
   If Not cevent.DesiredType Is GetType(Decimal) Then
      Return
   End If 
 cevent.Value = Decimal.Parse(cevent.Value.ToString, _
 NumberStyles.Currency, nothing)
End Sub 

[C#] 
private void DecimalToCurrencyString(object sender, ConvertEventArgs cevent)
{
   // The method converts only to string type. 
   if(cevent.DesiredType != typeof(string)) return;

   cevent.Value = ((decimal) cevent.Value).ToString("c");
}

private void CurrencyStringToDecimal(object sender, ConvertEventArgs cevent)
{   
   // The method converts only to decimal type.
   if(cevent.DesiredType != typeof(decimal)) return;

   cevent.Value = Decimal.Parse(cevent.Value.ToString(),
   NumberStyles.Currency, null);
}


[C++] 
private:
void DecimalToCurrencyString(Object* /*sender*/, ConvertEventArgs* cevent)
{
   // The method converts only to string type. 
   if(cevent->DesiredType != __typeof(String)) return;

   cevent->Value = (dynamic_cast<Decimal*> (cevent->Value))->ToString(S"c");
}

private:
void CurrencyStringToDecimal(Object* /*sender*/, ConvertEventArgs* cevent)
{   
   // The method converts only to decimal type.
   if(cevent->DesiredType != __typeof(Decimal)) return;

   cevent->Value = __box(Decimal::Parse(cevent->Value->ToString(),
   NumberStyles::Currency, 0));
}

[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 ファミリ, .NET Compact Framework - Windows CE .NET

参照

ConvertEventArgs クラス | ConvertEventArgs メンバ | System.Windows.Forms 名前空間