Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have 10 textboxes i want to check if user inserts text in 8th textbox it should generate error because he should first fill 7th textbox and so on it can be done using if else but i need to be done using loops to reduce code in C#(windows form).
can you help me please.

What I have tried:

i have written multiple if conditions
Posted
Updated 27-Aug-19 20:24pm
Comments
Patrice T 28-Aug-19 1:08am    
And you plan to show your code ?

Quote:
i need to be done using loops to reduce code

No, you don't. You don't validate "using loops" in WinForms, you respond to events and process things.

What you do is handle events like mobileNumber.TextChanged and in that handler you check if landlineNumber.Text has data. If it doesn't, show the user there is a problem and maybe set the focus to the textbox he needs to fill in.

But ... unlike Console apps, your code shouldn't "force" the user to enter any data or make any decisions in any particular order, unless that data or those choices mean that the kind of data you need is different. And then you separate it so that "common" data and the "choice" are visible together, then when he presses "OK" a different set of data appears for his input based on that choice.

For example, if this was a flight booking system, the first page has name, address, mobile number and a selector for "airport to fly from". He selects the airport and presses OK, so a new page appears with destinations available from that airport.

The idea is not to force the user, but to guide him into making decisions and providing only correct info. That's better, easier and faster for the user, as well as a lot less confusing. Try it! It's not about "reducing code" - code's cheap - but improving the user experience.
 
Share this answer
 
You have to use ErrorProvider Class (System.Windows.Forms) | Microsoft Docs[^] to be able to check if form is valid.
See: How to: Display Error Icons for Form Validation with the Windows Forms ErrorProvider Component | Microsoft Docs[^]

I'd suggest to create a Dictionary<ErrorProvider> for each TextBox, for example:
C#
Dictionary<Control, ErrorProvider> eps = new Dictionary<Control, ErrorProvider>();
eps.Add(textBox1, new ErrorProvider(this));
eps.Add(textBox2, new ErrorProvider(this));
//and so on...


Then you have to define validation rule for each TextBox
C#
private void txtBox1_Validating(object sender, CancelEventArgs e)
{
    if (checking_for_error_logic_here)
        eps[txtBox1].SetError(txtBox1, "Custom error message here");
    else
        eps[txtBox1].Clear();
}


Finally, you have to create method like this:
C#
private bool IsFormValid()
{
    foreach (Control c in eps.Keys)
    {
        if(epc[c].GetError(c) !="")
            return false;
    }
    return true;
}

to check if form is valid.

Good luck!
 
Share this answer
 
Comments
Member 13363321 28-Aug-19 5:00am    
actually i want to validate the textboxes when sequence missing
like i am having 10 textboxes,i am entering 1,3 textbox but validation should fire only for 2nd textbox
Maciej Los 28-Aug-19 5:03am    
So, change the code to your needs. You may add as many validators as you wish. It depends on you and application requirements.

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