Binding.Format 이벤트

정의

컨트롤의 속성이 데이터 값에 바인딩되면 발생합니다.

public:
 event System::Windows::Forms::ConvertEventHandler ^ Format;
public event System.Windows.Forms.ConvertEventHandler Format;
public event System.Windows.Forms.ConvertEventHandler? Format;
member this.Format : System.Windows.Forms.ConvertEventHandler 
Public Custom Event Format As ConvertEventHandler 

이벤트 유형

예제

다음 코드 예제에서는 Binding, 추가 ConvertEventHandler 둘 다에 대리자를 ParseFormat 이벤트 추가 BindingBindingsCollectionTextBox 를 통해 제어할는 DataBindings 속성입니다. DecimalToCurrencyString 이벤트 대리자를 추가 합니다 Format 이벤트에 바인딩된 값의 서식을 지정 (를 Decimal 형식) 통화를 사용 하 여를 ToString 메서드. CurrencyStringToDecimal 이벤트 대리자를 추가 합니다 Parse 이벤트를 컨트롤에 의해 표시 되는 값을 변환 다시는 Decimal 형식.

이 예제에서는 라는 ds가 있다고 가정합니다DataSet.

private:
   void DecimalToCurrencyString( Object^ /*sender*/, ConvertEventArgs^ cevent )
   {
      // The method converts only to string type. Test this using the DesiredType.
      if ( cevent->DesiredType != String::typeid )
      {
         return;
      }
      
      // Use the ToString method to format the value as currency ("c").
      cevent->Value = ( (Decimal)(cevent->Value) ).ToString( "c" );
   }

   void CurrencyStringToDecimal( Object^ /*sender*/, ConvertEventArgs^ cevent )
   {
      // The method converts back to decimal type only. 
      if ( cevent->DesiredType != Decimal::typeid )
      {
         return;
      }
      
      // Converts the string back to decimal using the static Parse method.
      cevent->Value = Decimal::Parse( cevent->Value->ToString(),
         NumberStyles::Currency, nullptr );
   }

   void BindControl()
   {
      // Creates the binding first. The OrderAmount is a Decimal type.
      Binding^ b = gcnew Binding(
         "Text",ds,"customers.custToOrders.OrderAmount" );
      
      // Add the delegates to the event.
      b->Format += gcnew ConvertEventHandler( this, &Form1::DecimalToCurrencyString );
      b->Parse += gcnew ConvertEventHandler( this, &Form1::CurrencyStringToDecimal );
      text1->DataBindings->Add( b );
   }
private void DecimalToCurrencyString(object sender, ConvertEventArgs cevent)
{
   // The method converts only to string type. Test this using the DesiredType.
   if(cevent.DesiredType != typeof(string)) return;

   // Use the ToString method to format the value as currency ("c").
   cevent.Value = ((decimal) cevent.Value).ToString("c");
}

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

   // Converts the string back to decimal using the static Parse method.
   cevent.Value = Decimal.Parse(cevent.Value.ToString(),
   NumberStyles.Currency, null);
}

private void BindControl()
{
   // Creates the binding first. The OrderAmount is a Decimal type.
   Binding b = new Binding
      ("Text", ds, "customers.custToOrders.OrderAmount");
   // Add the delegates to the event.
   b.Format += new ConvertEventHandler(DecimalToCurrencyString);
   b.Parse += new ConvertEventHandler(CurrencyStringToDecimal);
   text1.DataBindings.Add(b);
}
Private Sub DecimalToCurrencyString(sender As Object, cevent As _
ConvertEventArgs)
   ' The method converts only to string type. Test this using the DesiredType.
   If cevent.DesiredType IsNot GetType(String) Then
      Exit Sub
   End If

   ' Use the ToString method to format the value as currency ("c").
   cevent.Value = CType(cevent.Value, Decimal).ToString("c")
End Sub

Private Sub CurrencyStringToDecimal(sender As Object, cevent As _
ConvertEventArgs)
   ' The method converts back to decimal type only.
   If cevent.DesiredType IsNot GetType(Decimal) Then
      Exit Sub
   End If

   ' Converts the string back to decimal using the static ToDecimal method.
   cevent.Value = Decimal.Parse(cevent.Value.ToString, _
   NumberStyles.Currency, nothing)
End Sub

Private Sub BindControl
   ' Creates the binding first. The OrderAmount is a Decimal type.
   Dim b As Binding = New Binding _
      ("Text", ds, "customers.custToOrders.OrderAmount")
   ' Add the delegates to the event
   AddHandler b.Format, AddressOf DecimalToCurrencyString
   AddHandler b.Parse, AddressOf CurrencyStringToDecimal
   text1.DataBindings.Add(b)
End Sub

설명

Format 이벤트는 데이터가 데이터 원본에서 컨트롤로 푸시될 때 발생합니다. 이벤트를 처리 Format 하여 형식이 지정되지 않은 데이터를 데이터 원본에서 서식이 지정된 데이터로 변환하여 표시할 수 있습니다. 데이터가 컨트롤에서 데이터 원본으로 끌어오면 표시된 값의 Parse 서식을 해제하기 위해 이벤트가 발생합니다. 그러면 Format 이벤트가 발생하여 표시할 데이터의 서식을 다시 지정합니다. 이렇게 하면 사용자가 컨트롤에 서식이 지정되거나 서식이 지정되지 않은 데이터를 입력하는지 여부에 관계없이 바인딩된 컨트롤에 올바른 형식의 데이터가 표시됩니다.

합니다 FormatParse 이벤트를 통해 데이터를 표시 하기 위한 사용자 지정 형식을 만들 수 있습니다. 예를 들어 테이블의 데이터가 형식Decimal인 경우 의 속성을 ConvertEventArgs 이벤트의 형식이 지정된 값 Format 으로 설정 Value 하여 현지 통화 형식으로 데이터를 표시할 수 있습니다. 에 표시 된 값을 때는 결과적으로 해야 합니다는 Parse 이벤트입니다.

이벤트는 Format 값이 Current 변경 될 때마다 발생합니다. 여기에는 다음이 BindingManagerBase 포함됩니다.

  • 속성이 처음 바인딩된 경우입니다.

  • 언제든지 변경합니다 Position .

  • 데이터 바인딩된 목록이 정렬되거나 필터링될 때마다 이 작업은 에서 DataView 목록을 제공할 때 수행됩니다.

이벤트도 Format 이벤트 후에 Parse 발생합니다. 예를 들어 컨트롤에 포커스가 끊어지면 해당 내용이 구문 분석됩니다. 그 직후 새 데이터가 컨트롤 Format 로 푸시되면 이벤트가 발생하여 새 콘텐츠의 형식을 지정할 수 있습니다.

이벤트 처리에 대한 자세한 내용은 이벤트 처리 및 발생 을 참조하십시오.

적용 대상

추가 정보