Click here to Skip to main content
14,355,110 members
Rate this:
Please Sign up or sign in to vote.
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
Updated 27-Aug-19 21:24pm
Patrice T 28-Aug-19 1:08am
And you plan to show your code ?
Rate this:
Please Sign up or sign in to vote.

Solution 1

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

Solution 2

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:
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
private void txtBox1_Validating(object sender, CancelEventArgs e)
    if (checking_for_error_logic_here)
        eps[txtBox1].SetError(txtBox1, "Custom error message here");

Finally, you have to create method like this:
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!
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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100