Click here to Skip to main content
15,894,291 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

i´ve been searching for a way to check if all textboxes from a form a empty and if not show a message. Some of them are not visible.
I have the code below but it´s not working somehow, when i click ok i get an error because a textbox is empty.

Can anyone help please ?

Thanks in advance

What I have tried:

C#
foreach (Control c in Controls)
{
    if (c is TextBox && c.Visible && string.IsNullOrEmpty(c.Text))
    {
        MessageBox.Show($"TextBox {c.Name} is empty");
    }
}
Posted
Updated 18-Nov-16 5:13am
v2
Comments
Philippe Mori 18-Nov-16 10:14am    
Try to improve your question formatting and fix spelling errors.
Marc-IT 18-Nov-16 11:13am    
Thanks for the tip, i think it´s much clearer now.
Philippe Mori 18-Nov-16 11:22am    
Well, it is far better... However, it is not clear exactly what you want to do and what is the problem you have. It is easy to understand that the code above would show a message box for each text box that is empty...

But in your question, it seems that you want to display a message if not all textboxes are empty (that is some of them do contain some text).

Well, usually one should stop looping after the first error as otherwise it would be very annoying to respond to many message box before fixing the first problem. Also, it is a good idea to put focus on the (first) control that is not valid.

Thus in your code, I would add c.Focus(); break; after displaying the message to the user.
Marc-IT 18-Nov-16 11:26am    
all textboxes have to be checked, this is only to prevent te application from crashing.
if one textbox has no information the app crashes because of a formula.
some of them are hidden and are only visible if the user selects certain options on the first form.
The simple version is, all textboxes must be checked.
Marc-IT 18-Nov-16 11:31am    
its strange i think the code is ok but for some reason it´s not loading.
the app works but this condition wont apply

1 solution

Hope I understand correctly but why not use the controls visible state to determine whether to test the box or not;

C#
if (larg.Text == string.Empty)
{
    if (larg.Visible == False)
    {
      MessageBox.Show("info missing", "Warning");
      return;
    }
}


Etcetera.
 
Share this answer
 
v3
Comments
Marc-IT 18-Nov-16 9:26am    
Thanks for the answer, i tried this solution but get error in Hidden and False.
'TextBox' does not contain a definition for 'Hidden' and no extension method 'Hidden' accepting a first argument of type 'TextBox' could be found (are you missing a using directive or an assembly reference?)
and
Name False does not exist in the current context.

any ideas ?
Marc-IT 18-Nov-16 10:01am    
i changed it to
if (larg.Visible == true)
it accepts the code but does nothing
Michael_Davies 18-Nov-16 10:09am    
what do you mean "does nothing", walk through with the debugger to see what is happening.

Show your altered 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