Click here to Skip to main content
12,749,046 members (39,001 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#
Dear All,
How to show Mandatory fields in C# windows form? Can any one suggest me?

Thanks and Regards,
Posted 7-Jan-11 22:13pm
Updated 7-Jan-11 22:27pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 4


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:

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)

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.

Dalek Dave 8-Jan-11 8:38am
Good answer.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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
Dalek Dave 8-Jan-11 8:37am
Good Answer.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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.

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

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170215.1 | Last Updated 8 Jan 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100