8

ユーザーが下部の計算ボタンを押す前に、特定のフィールドに値を入力するように強制したいWindowsフォームプロジェクトがあります。フィールドには、3組のラジオボタン、5つのテキストボックス、および1つのコンボボックスが含まれます。したがって、基本的に、これらすべてのフィールドには、計算を実行するための値が含まれている必要があります。さらに、テキストボックスには数値(任意のdouble値)を含める必要があります。さらに、これらのテキストボックスのほとんどに設定された最大値を設定したいユーザーが超えることはできません。これを実現する最も簡単な方法を教えてください。ASP.Netで利用可能なようなwinformプロジェクトのフィールド検証コントロールが表示されません。注意してください、私は.net3.5に取り組んでいます。現在、私はこれをユーザーに伝えるためにメッセージボックスを使用しています。つまり、ユーザーが計算を押すたびに、現在空の必須フィールドの名前を示すメッセージボックスを表示します。

4

6 に答える 6

4

すべてのカスタム検証を実装する最も簡単な方法は、ボタンクリックイベント内に一連のif条件を設定することだと思います。

private void Calculate_button_Click(object sender, EventArgs e)
{
    if(textBox1.Text == string.Empty)
    {
        MessageBox.Show("Please enter a value to textBox1!");
        return;
    }
    else if(!radioButton1.Checked && !radioButton2.Checked)
    {
        MessageBox.Show("Please check one radio button!");
        return;
    }
    else if(comboBox1.SelectedIndex == -1)
    {
        MessageBox.Show("Please select a value from comboBox!");
        return;
    }
    else
    {
        // Program logic...
    }
}

同様に、範囲も確認できます。

于 2012-11-23T12:08:18.407 に答える
4

これを試すことができます:

private void Calculate_button_Click(object sender, EventArgs e)
{
    RadioButton[] newRadioButtons = { radiobutton1, radiobutton2, radiobutton3 };
    for (int inti = 0; inti < newRadioButtons.Length; inti++)
    {
        if (newRadioButton[inti].Checked == false)
        {
            MessageBox.Show("Please check the radio button");
            newRadioButtons[inti].Focus();
            return;
        }
    }
    TextBox[] newTextBox = { txtbox1, txtbox2, txtbox3, txtbox4, txtbox5 };
    for (int inti = 0; inti < newRadioButtons.Length; inti++)
    {
        if (newTextBox[inti].text == string.Empty)
        {
            MessageBox.Show("Please fill the text box");
            newTextBox[inti].Focus();
            return;
        }
    }
}

コントロールをループして、塗りつぶされているかどうかを見つけることができます。塗りつぶされていない場合は、メッセージ ボックスに表示され、特定のコントロールがフォーカスされます。

于 2012-11-23T13:12:38.763 に答える
0

コントロールの Validating イベントを使用して、そこに必要な検証をコード化してみてください。

ValidateEmail 関数を使用してテキストが電子メール アドレスであるかどうかを確認し、一致しない場合は背景を (赤?) 色に設定し、一致するまでユーザーが制御を離れないようにする例を次に示します。

Private Sub VoucherEmail_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Textbox.Validating
    If Not ValidateEmail(sender.Text) Then
        sender.backcolour = Validation.ErrorBackgrounColor
        e.Cancel = True
    End If
End Sub
于 2012-11-23T11:53:44.317 に答える
0

私はこれが古いスレッドであることを知っています。

しかし、私は答える価値があると思います。

計算ボタンのクリック機能に追加

if(!this.ValidateChildren())
            {
                return;
            }

検証関数を

編集

申し訳ありませんが、これは .NET 4.0 以降で動作します

于 2019-09-14T09:28:23.213 に答える
-2
'You can try this code----- 
'Function for null validate for particular type of controls in your form
Function NullValidate() As Boolean
    NullValidate = True
    For Each ctrl As Control In Me.Controls
        If TypeOf ctrl Is TextBox Then
            If ctrl.Text = "" Then
                MessageBox.Show("Invalid input for " & ctrl.Name)
                NullValidate = False
               Exit Function
            Else
               NullValidate = True
            End If
         End If
    Next
End Function
'Function for numeric validate for particular type of controls in your form
 Function NumericValidate() As Boolean
     NumericValidate = True
    For Each ctrl As Control In Me.Controls
         If TypeOf ctrl Is TextBox Then
             If NumericValidate = IsNumeric(ctrl.text) Then
                   MessageBox.Show("Invalid input for " & ctrl.Name)
                  NumericValidate = False
                  Exit Function
             Else
                  NumericValidate = True
             End If
         End If
     Next
   End Function

Private Sub cmdCalculate_Click(sender As Object, e As EventArgs) Handles cmdSave.Click
     If NullValidate() = True Then
         If NumericValidate() = True Then
             'your statement is here
         End If
     End If
End Sub
于 2013-12-16T15:28:55.810 に答える