2012-11-12 104 views
-4

這是我的表結構2列速率圖表和qty:動態文本框驗證

rate chart qty 
1  -5  1 
6  -10  2 
11 -20  3 

我有窗口形式

  • 1組合框
  • 1文本框
  • 1按鈕3個字段

我獲取ratechart列值通過組合框,我有該代碼,但我的要求是,如果用戶從組合框中選擇1-5,然後文本框,他不能在文本框中輸入超過6。

相同的,如果用戶從下拉框中選擇6-10,那麼他就不能在文本框

,當他點擊Save按鈕數量更多,他得到一個錯誤輸入超過12 ..提前

THX 。請幫我編碼

+1

網站或WinForms的? –

+0

windows form ........ – VINNI

+1

您的表格結構顯示它有三列費率,圖表和數量。 –

回答

0

假設cmbRateChart.SelectedValue包含與RangeChart相關的數值。

private void textBox_Validating(object sender, CancelEventArgs e) 
{ 
    bool cancel = false; 
    int number = -1; 

    if (int.TryParse(this.textBox.Text, out number)) 
    { 
     var validRange = Convert.ToInt32(cmbRateChart.SelectedValue) * 6; 
     if (number <= validRange) 
      cancel = false; //passed validation. 
     else 
      cancel = true; //failed validation, number is not in valid range 

    } 
    else 
     cancel = true;//failed validation: text box is not a number 
    e.Cancel = cancel; 
} 

用法:調用此函數檢查驗證。

this.ValidateChildren(ValidationConstraints.Enabled); 

參考:Validation in Windows Forms

+0

我在哪裏寫代碼和你* 6的代碼,如果用戶選擇6-10 ...在組合框? – VINNI

+0

您可以爲此段代碼勾選TextBox的驗證事件。範圍6-10有一個數量2,它乘以6,結果2 * 6 = 12 –

+0

我的回答對你有幫助嗎? –

0
private void saveButton_Click(object sender, EventArgs e) 
    { 
     // Get value from textBox 
     int number = Int32.Parse(textBox1.Text); 

     // Get value from combobox 
     int selcetedComboValue = Int32.Parse(comboBox1.SelectedItem.ToString()); 

     // Validate Values 
     if (selcetedComboValue <= 5) 
     { 
      if (number <= 6) 
      { 
       // Valid Number 
      } 
      else 
      { 
       // Invalid Number 
      } 
     } 
     else if (selcetedComboValue <= 10) 
     { 
      if (number <= 12) 
      { 
       // Valid Number 
      } 
      else 
      { 
       // Invalid Number 
      } 
     } 
    } 
+0

錯誤:輸入字符串的格式不正確int selcetedComboValue = Int32.Parse(comboBox1.SelectedItem.ToString()); – VINNI

+0

其工作完美。我認爲你的組合框值不是數字。 –