Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,
How to show Mandatory fields in C# windows form? Can any one suggest me?

Thanks and Regards,
A.Harisankar.
Posted
Updated 7-Jan-11 21:27pm
v2

Hello,

What I usually do is either add a * at the end of the field prompt, or change the background colour of the textbox. Usually the customer tells you what they prefer.

Also I use the ErrorProvider control to validate errors in the form.

for example:

C#
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    if (textBox1.Text == "")
    {
        errorProvider1.SetError(textBox1, "This is a mandatory field");
        e.Cancel = true;
    }
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
    errorProvider1.Clear();
}


By setting e.Cancel to true you prevent the form from closing.
By using the SetError method of the errorProvider1 object, you will make a little error warning flash next to the field in error.

You can add as many errorproviders as necessary.

This is very simple to use and very effective.

Valery.
 
Share this answer
 
Comments
Dalek Dave 8-Jan-11 8:38am    
Good answer.
Are you asking for representing mandatory fields in C# winforms or displaying message while submitting the data.

To mark field mandatory you can use color coding or astrick(*) after field name or change font of the field caption to mark those field mandatory.

To display message about mandatory field you can write a boolean function (eg. isAllDataValid()) to iterate through all the mandatory field and check their values and show the message and only submit when all the mandatory control have valid data.

for example.

use derived controls with property such as

[property] bool IsMandatory
[property] bool IsValidData
[property] string ValidationString (may be Regular expression string)

there you go
 
Share this answer
 
Comments
Dalek Dave 8-Jan-11 8:37am    
Good Answer.
What do you mean by mandatory here ?

It's upto you whether you want to keep field a mandatory or not.

If you see some field are very much necessary for one to fill for achieving further complication raised if it is not filled then you can put that field mandatory.

Mandatory field can further be validated by many validates are around.

or I am not getting your question properly ? or show us in what scenario what you want to achieve you can share code snippets too.
 
Share this answer
 
If you mean showing "*" which is generally used to show a mandatory field, you can either have an image next to controls or have this added in the label text of the fields.

If you are looking to validate the fields, you can do this whenever the data from the form is processes (may be something like save button click).

Do not validate mandatory fields when the user leaves them (Validating event handler) since it may be annoying for some.
 
Share this answer
 

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