I
I
Ivan2019-06-01 18:02:21
C++ / C#
Ivan, 2019-06-01 18:02:21

How to bulk validate data from a userform?

winforms. there are 20 TextBoxes and 20 Labels nearby (they say what tb means).
All values ​​are entered by the user. All values ​​must be double.
I usually write something like this (probably shitty code):

spoiler
private void TbLnsDist_TextChanged(object sender, EventArgs e)
        {
            string val = tbLnsDist.Text;
            val = val.Replace(",", decimal_sep);
            val = val.Replace(".", decimal_sep);

            double number;
            bool res = double.TryParse(val, out number);
            if (res == false)
            {
                lbLnsDist.Text = "Введите число!";
                lbLnsDist.ForeColor = Color.Red;
                btnLnsCreate.Enabled = false;
            }
            else
            {
                lbLnsDist.Text = "Дистанция";
                lbLnsDist.ForeColor = Color.Black;
                btnLnsCreate.Enabled = true;
            }
        }

However, there are a lot of controls.
What is the correct way to check in such cases? separate class with methods? Or nevertheless in each control to write the check?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
edward_freedom, 2019-06-01
@Iv_and_S

Write your own control that will only accept double and no checks are needed

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question