Textbox validation in a Windows Form

后端 未结 3 1449
不思量自难忘°
不思量自难忘° 2021-01-18 05:52

I want to put a validation that the user always enters a value in the textbox before submiting the form. But the check that I have put allows user to enter white spaces and

3条回答
  •  悲哀的现实
    2021-01-18 06:16

    It can be easily be done using error provider here is the code.Error Provider you can find in your toolbox.

        private void btnsubmit_Click(object sender, EventArgs e)
                {
    
                    if (string.IsNullOrEmpty(txtname.Text))
                    {
    
                        txtname.Focus();
                        errorProvider1.SetError(txtname, "Please Enter User Name");
                    }
    
                    if (string.IsNullOrEmpty(txtroll.Text)) {
                        txtroll.Focus();
                        errorProvider1.SetError(txtroll, "Please Enter Student Roll NO");
                    }
    }
    

    Here is output image

提交回复
热议问题