Click here to Skip to main content
15,898,991 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi all,


I have designed one form in windows application, i need to add required field validators for "text box" and "combo box" controls. Means with out enter any data in "text box" error should be display, same as "combo box" also.


How to give the validations ?

Thanks and Regards,
Murali
Posted

Here is the code for validating the TextBox in windows application:

I.Take a Status Strip, Error Provider and Status Label.
On textBox1_Enter write following code:
C#
tssWarning.Text = "Please Enter your Name.";
//tssWarning is the name of Status Label.


II. Now for validating the TextBox create a method:
C#
private int ValidateData()
{
  int flag = 0;
  if(textBox1.Text = "")
  {
    textBox1.Focus();
    errorProvider1.SetError(textBox1, "Please Fill the Name.");
    //errorProvider1 is the name of Error Provider.
    flag = 1;
   }
  return flag;
 }


III. Now button_Click write following code:
C#
if(ValidateData() == 0)
{
  errorProvider1.Clear();
  MessageBox.Show("Submitted");
  tssWarning.Text = "";
 }
else
{
 //your code.
}



I hope it will help you.

--Rajesh
 
Share this answer
 
Hey ,

I have taken One textbox txtInput and ComboBox CmbNumber and Button as btnOK .
On Button Click.

Windows Application don't have Validation Control ,so check if field is empty or not on button Click . And For ComboBox , If any item is not selected , SelectedIndex is -1. Same is for other controls too.

Here I have created One Function CheckIsEmpty() to check if empty and combobox selection is made or not.

VB
Public Function CheckIsEmpty() As String
       Dim Msg As String = ""
       If txtInput.Text.Trim() = "" Then
           Msg = "Text Box is Empty"
           txtInput.Focus()
           Return Msg
       ElseIf cmbNumber.SelectedIndex = -1 Then
           Msg = "Please select one option "
           cmbNumber.Focus()
           Return Msg
       Else
           Return Msg
       End If
   End Function


On Button Click

VB
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
       Dim Msg As String = ""
       Msg = CheckIsEmpty()
       If Msg <> "" Then
           MessageBox.Show(Msg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Information)
       End If
   End Sub


Thanks ,
Vedangi
 
Share this answer
 
Comments
[no name] 18-Sep-12 3:07am    
Thanks a lot! It's working fine!

Thanks to all!!
[no name] 18-Sep-12 3:07am    
:)
Hi...

The below event handlers validates the text boxes and comboboxes in windows forms application.

First drag and drop "ErrorProvider" control from Components section of Toolbox onto your form and give its Id "errorProvider1". Then just copy below code block in your code file. Then in "Validating" event of text box select "TextBox_Validating" event handler and in "Validating" event of combobox select "ComboBox_Validating" event.

Set form's "AutoValidate" property to "Disable".

C#
private void TextBox_Validating(object sender, CancelEventArgs e)
        {
            TextBox objTextBox = (TextBox)sender;

            if (objTextBox.Text.Trim() == string.Empty)
            {
                errorProvider1.SetError(objTextBox, "Required");
                e.Cancel = true;
            }
            else
            {
                errorProvider1.SetError(objTextBox, null);
            }
        }

private void ComboBox_Validating(object sender, CancelEventArgs e)
        {
            ComboBox objComboBox = (ComboBox)sender;

            if (objComboBox.SelectedIndex <= 0)
            {
                errorProvider1.SetError(objComboBox, "Select");
                e.Cancel = true;
            }
            else
            {
                errorProvider1.SetError(objComboBox, null);
            }
        }


Then on button click event, you need to check this:

C#
if (this.ValidateChildren() == true)
                // Your code
            else
                return;



Its done now. :)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900