Click here to Skip to main content
Rate this: bad
good
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,
A.Harisankar.
Posted 7-Jan-11 21:13pm
Edited 7-Jan-11 21:27pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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:
 
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.
  Permalink  
Comments
Dalek Dave at 8-Jan-11 8:38am
   
Good answer.
Rate this: bad
good
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
  Permalink  
Comments
Dalek Dave at 8-Jan-11 8:37am
   
Good Answer.
Rate this: bad
good
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.
  Permalink  
Rate this: bad
good
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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 383
1 Richard Deeming 180
2 ChauhanAjay 176
3 Sergey Alexandrovich Kryukov 171
4 Yogesh Kumar Tyagi 165
0 Sergey Alexandrovich Kryukov 6,252
1 OriginalGriff 5,498
2 CPallini 2,473
3 Richard MacCutchan 1,627
4 Abhinav S 1,530


Advertise | Privacy | Mobile
Web03 | 2.8.140821.2 | Last Updated 8 Jan 2011
Copyright © CodeProject, 1999-2014
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