Click here to Skip to main content
15,911,711 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
using vsstudio 2010 (c#)..

i hv a form.. i want to check first all empty controls like combobox, textbox, etc. but my code works only on a textbox.. need help.. TIA..

What I have tried:

foreach (Control t in this.Controls)
            {
                if (t is TextBox)
                {
                    TextBox textBox = t as TextBox;
                    if (textBox.Text == string.Empty)
                    {
                        MessageBox.Show("Please fill up all fields!!".ToString());
                    }
                    else
                    {
                        //saved
                    }
                }
            }
Posted
Updated 19-Apr-17 3:11am
v2
Comments
CHill60 19-Apr-17 9:03am    
This has nothing to do with SQL or SQL-Server. If you tag your question incorrectly in future you could end up not getting the answers you need. Only tag the languages that the specific question is about (or the code is written in)

1 solution

You can try with below code -
C#
foreach (Control c in this.Controls)
{
    String value = String.Empty;
    switch (c.GetType().Name.ToString())
    {
        case "TextBox":
            //read textbox control value and check
            value = c.Text;
            MessageBox.Show(value);
            break;
        case "CheckBox":
            //read checkbox control value and check
            value = (c as CheckBox).Checked.ToString();
            MessageBox.Show(value);
            break;
        //you can add your futher controls need validation
    }
 
Share this answer
 
v3
Comments
akosisugar 19-Apr-17 8:50am    
thank u for the solution.. assume i hv only 2 controls to check, combobox and textbox. where should i put the save code in your proposed sample code?.. im not good in switch case..
[no name] 19-Apr-17 8:56am    
In the question, you mention as you need to handle the empty control you can add what other controls you need to handle for empty data scenario.

Now, what other stuff you want to do with our code after this you need to share the details without that no one should be able to helo on it.
akosisugar 19-Apr-17 8:59am    
nvr mind sir.. thnk u again for the code..

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