1
votes

I have two textboxes that are created dynamically created on load. The user can click the add button which adds two new text boxes to the interface each time. When these textboxes are added they are also added to a list. I am trying to add error handling to my project to make sure every textbox is filled out before the user can click save. If the text boxes are empty a message box should show : Please fill out all message boxes. If the textboxes are filled out a message box should show: Are you sure you want to make this change? If the user clicks yes the changes should be saved and the message box "Changes saved " should show If the user clicks no then the message box should disappear and the changes not saved. The message boxes are not acting the way I expected Here is what I have so far:

Create the text boxes:

 private void ADDUserInfo(int rowCount, string password = "", string username = "")
            var Password = new TextBox();
            Password.Text = password;
            Password.Name = "Password" + rowCount;

            var Username = new TextBox();
            Username.Text = username;
            Username.Name = "Username" + rowCount;

            CaeUsersPanel.Controls.Add(Username);
            CaeUsersPanel.Controls.Add(Password);

            UsernameTextBoxes.Add(Username);
            PasswordTextboxes.Add(Password);
            }

Global declaration of the lists of textboxes:

List<TextBox> UsernameTextBoxes = new List<TextBox>();
List<TextBox> AliasTextBoxes = new List<TextBox>();
List<TextBox> PasswordTextboxes = new List<TextBox>();

The save button:

 for (int i = 0; i < UsernameTextBoxes.Count; i++)
            {
                var userName = UsernameTextBoxes[i];
                var password = PasswordTextboxes[i];
                _config.Environment.Users.User.Add(new EnvironmentUsersUser() { alias = aliasName.Text, userName = userName.Text, password = password.text, toDisplay = true });

            }

            configmgr.Serilize<Config>(_configurationTabData._objectSources.getEnviromentFileName, _config);


            for (int i = 0; i < UsernameTextBoxes.Count; i++)
            {
                var userName = UsernameTextBoxes[i];
                var password = PasswordTextboxes[i];
                if (aliasName.Text == "" || userName.Text == "" || password.Text == "")
                {
                    MessageBox.Show("All fields are required!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                  DialogResult result = MessageBox.Show("Are you sure you want to make these changes", "Warning", MessageBoxButtons.YesNo);
            if (result == DialogResult.Yes)
            {
                configmgr.Serilize<Config>(_configurationTabData._objectSources.getEnviromentFileName, _config);
                MessageBox.Show("Changes Saved")
            }
           }

            }
1

1 Answers

1
votes

Maybe something like this, using LinQ?

if(UsernameTextBoxes.TrueForAll(t => t.Text.Length > 0) &&
    AliasTextBoxes.TrueForAll(t => t.Text.Length > 0) &&
    PasswordTextboxes.TrueForAll(t => t.Text.Length > 0))
{
    //Do stuff when everything is filled
}
else
{
    //Do stuff when one or more is empty 
}

You might want to group your TextBoxes into a class and make that class check if everythings filled. Then you would just need one List with this class as generic.