0 von 1 fanden dies hilfreich - Dieses Thema bewerten.

Control.CausesValidation-Eigenschaft

Ruft einen Wert ab, der angibt, ob das Steuerelement bei Erhalt des Fokus eine Validierung für alle eine Validierung erfordernden Steuerelemente veranlasst, oder legt diesen fest.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public bool CausesValidation { get; set; }
/** @property */
public boolean get_CausesValidation ()

/** @property */
public void set_CausesValidation (boolean value)

public function get CausesValidation () : boolean

public function set CausesValidation (value : boolean)

Eigenschaftenwert

true, wenn das Steuerelement eine Validierung für alle eine Validierung erfordernden Steuerelemente veranlasst, wenn es den Fokus erhält, andernfalls false. Der Standardwert ist true.

Wenn die CausesValidation-Eigenschaft auf false festgelegt ist, wird das Validating-Ereignis und das Validated-Ereignis unterdrückt.

Der CausesValidation-Eigenschaftenwert wird für Steuerelemente, z. B. die Schaltfläche Hilfe, i. d. R. auf false festgelegt.

Im folgenden Codebeispiel wird ein Windows Form erstellt, das die in Textfeldern eingegebenen Zahlen hinzufügt. Die Textfelder werden vor dem Anzeigen des Ergebnisses überprüft.

private void AddHandlers()
{
   // Add the Validating and Validated handlers for textboxes.
   myTextBox1.Validating +=
      new System.ComponentModel.CancelEventHandler(myTextBox1_Validating);
   myTextBox1.Validated += 
      new System.EventHandler(myTextBox1_Validated);
   myTextBox2.Validating += 
      new System.ComponentModel.CancelEventHandler(myTextBox2_Validating);
   myTextBox2.Validated += 
      new System.EventHandler(myTextBox2_Validated);
   myTextBox1.CausesValidationChanged += 
      new System.EventHandler(myTextBox1_CausesValidationChanged);
   myTextBox2.CausesValidationChanged += 
      new System.EventHandler(myTextBox2_CausesValidationChanged);
   if(myTextBox1.CausesValidation == true && myTextBox2.CausesValidation == true) 
   {
      button1.Text = "Disable Validation";
      myLabel.Text = "Validation Enabled"; 
      this.Focus(); 
   }
}
private void myTextBox1_Validating(object sender,System.ComponentModel.CancelEventArgs e)
{        
   if(!CheckIfTextBoxNumeric(myTextBox1))
   {
      myLabel.Text =  "Has to be numeric";
      e.Cancel = true;
   }
}
private void myTextBox1_Validated(object sender,System.EventArgs e)
{
   myLabel.Text = "Validated first control";          
}
private void myTextBox2_Validating(object sender,System.ComponentModel.CancelEventArgs e)
{
   if(!CheckIfTextBoxNumeric(myTextBox2))
   {
      myLabel.Text =  "Has to be numeric";
      e.Cancel = true;
   }
}
private void myTextBox2_Validated(object sender,System.EventArgs e)
{
   myLabel.Text = "Validated second control";
}
private void myTextBox1_CausesValidationChanged(object sender,System.EventArgs e)
{  
   myLabel.Text = "CausesValidation property was changed for First Textbox";
}
private void myTextBox2_CausesValidationChanged(object sender,System.EventArgs e)
{  
   myLabel.Text = "CausesValidation property was changed for Second Textbox";
}
private bool CheckIfTextBoxNumeric(TextBox myTextBox1)
{ 
   bool isValid = true;
   if(myTextBox1.Text == "")
   {
      isValid = false;
   }
   else
   {
      for(int i=0; i< myTextBox1.Text.Length;i++)
      {
         if(!(System.Char.IsNumber(myTextBox1.Text[i]))) 
         {
            myTextBox1.Text = ""; 
            isValid = false;
            break;
         }
      }
   }
   return isValid;
}
private void myButtonAdd_Click(object sender, System.EventArgs e)
{  
   try
   {
      int result = Convert.ToInt32(myTextBox1.Text) + Convert.ToInt32(myTextBox2.Text);
      myLabel.Text =  result.ToString();
   }
   catch(Exception myException)
   {
      myLabel.Text = "Exception : " + myException.Message;
   }
}

private void button1_Click(object sender, System.EventArgs e)
{  
   if(myTextBox1.CausesValidation == false && myTextBox2.CausesValidation == false) 
   {
      myTextBox1.CausesValidation = true;
      myTextBox2.CausesValidation = true;
      button1.Text = "Disable Validation"; 
      myLabel.Text = "Validation Enabled"; 
   }
   else if(myTextBox1.CausesValidation == true && myTextBox2.CausesValidation == true) 
   {
      myTextBox1.CausesValidation = false;
      myTextBox2.CausesValidation = false;
      button1.Text = "Enable Validation"; 
      myLabel.Text = "Validation Disabled"; 
   }
}

private void AddHandlers()
{
    // Add the Validating and Validated handlers for textboxes.
    myTextBox1.add_Validating(new System.ComponentModel.
        CancelEventHandler(myTextBox1_Validating));
    myTextBox1.add_Validated(new System.EventHandler(myTextBox1_Validated));
    myTextBox2.add_Validating(new System.ComponentModel.
        CancelEventHandler(myTextBox2_Validating));
    myTextBox2.add_Validated(new System.EventHandler(myTextBox2_Validated));
    myTextBox1.add_CausesValidationChanged(new System.
        EventHandler(myTextBox1_CausesValidationChanged));
    myTextBox2.add_CausesValidationChanged(new System.
        EventHandler(myTextBox2_CausesValidationChanged));
    if (myTextBox1.get_CausesValidation() == true && myTextBox2.
        get_CausesValidation() == true) {
        button1.set_Text("Disable Validation");
        myLabel.set_Text("Validation Enabled");
        this.Focus();
    }
} //AddHandlers

private void myTextBox1_Validating(Object sender, 
    System.ComponentModel.CancelEventArgs e)
{
    if (!(CheckIfTextBoxNumeric(myTextBox1))) {
        myLabel.set_Text("Has to be numeric");
        e.set_Cancel(true);
    }
} //myTextBox1_Validating

private void myTextBox1_Validated(Object sender, 
    System.EventArgs e)
{
    myLabel.set_Text("Validated first control");
} //myTextBox1_Validated

private void myTextBox2_Validating(Object sender, 
    System.ComponentModel.CancelEventArgs e)
{
    if (!(CheckIfTextBoxNumeric(myTextBox2))) {
        myLabel.set_Text("Has to be numeric");
        e.set_Cancel(true);
    }
} //myTextBox2_Validating

private void myTextBox2_Validated(Object sender, System.EventArgs e)
{
    myLabel.set_Text("Validated second control");
} //myTextBox2_Validated

private void myTextBox1_CausesValidationChanged(Object sender, 
    System.EventArgs e)
{
    myLabel.set_Text("CausesValidation property was changed for "
        + "First Textbox");
} //myTextBox1_CausesValidationChanged

private void myTextBox2_CausesValidationChanged(Object sender, 
    System.EventArgs e)
{
    myLabel.set_Text("CausesValidation property was changed for "
        + "Second Textbox");
} //myTextBox2_CausesValidationChanged

private boolean CheckIfTextBoxNumeric(TextBox myTextBox1)
{
    boolean isValid = true;
    if (myTextBox1.get_Text().Equals("")) {
        isValid = false;
    }
    else {
        for (int i = 0; i < myTextBox1.get_Text().get_Length(); i++) {
            if (!(System.Char.IsNumber(myTextBox1.get_Text().
                get_Chars(i)))) {
                myTextBox1.set_Text("");
                isValid = false;
                break;
            }
        }
    }
    return isValid;
} //CheckIfTextBoxNumeric

private void myButtonAdd_Click(Object sender, System.EventArgs e)
{
    try {
        int result = Convert.ToInt32(myTextBox1.get_Text()) 
            + Convert.ToInt32(myTextBox2.get_Text());
        myLabel.set_Text(System.Convert.ToString(result));
    }
    catch (System.Exception myException) {
        myLabel.set_Text("Exception : " + myException.get_Message());
    }
} //myButtonAdd_Click

private void button1_Click(Object sender, System.EventArgs e)
{
    if (myTextBox1.get_CausesValidation() == false && myTextBox2.
        get_CausesValidation() == false) {
        myTextBox1.set_CausesValidation(true);
        myTextBox2.set_CausesValidation(true);
        button1.set_Text("Disable Validation");
        myLabel.set_Text("Validation Enabled");
    }
    else {
        if (myTextBox1.get_CausesValidation() == true && myTextBox2.
            get_CausesValidation() == true) {
            myTextBox1.set_CausesValidation(false);
            myTextBox2.set_CausesValidation(false);
            button1.set_Text("Enable Validation");
            myLabel.set_Text("Validation Disabled");
        }
    }
} //button1_Click

Fanden Sie dies hilfreich?
(1500 verbleibende Zeichen)
Microsoft führt eine Onlineumfrage durch, um Ihre Meinung zur MSDN-Website zu erfahren. Wenn Sie sich zur Teilnahme entscheiden, wird Ihnen die Onlineumfrage angezeigt, sobald Sie die MSDN-Website verlassen.

Möchten Sie an der Umfrage teilnehmen?