A
A
Alexander Fandeev2021-01-05 22:44:21
Telecom operators
Alexander Fandeev, 2021-01-05 22:44:21

How to correctly handle clicking the "Cancel" button in a dialog box?

There is a small code, below I throw its listing. It is necessary to make it so that when you press the "cancel" or "ok" button, the program displays a message about an incorrect value through MessageBox.Show.

int number;
        string name;

        MessageBoxIcon icon;

        number = Int32.Parse(Interaction.InputBox("Введите число: ", "Число"));

     
     
        switch (number)
        {
            case 1:  //Если ввел 1
                name = "Единица";  //Название числа
                icon = MessageBoxIcon.Information;
                break;       // Завершение блока

            case 2:  //Если ввел 2
                name = "Двойка";  //Название числа
                icon = MessageBoxIcon.Information;
                break;       // Завершение блока

            case 3:  //Если ввел 3
                name = "Тройка";  //Название числа
                icon = MessageBoxIcon.Information;
                break;       // Завершение блока

            case 4:  //Если ввел 4
                name = "Четверка";  //Название числа
                icon = MessageBoxIcon.Information;
                break;       // Завершение блока

            default:
                name = "";
                icon = MessageBoxIcon.Information;
                break;
        }
            MessageBox.Show(name, "Число", MessageBoxButtons.OK, icon);

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Fandeev, 2021-01-05
@alexandrfandeev

He asked, he answered. If someone is interested in the solution, I give it a little below:

MessageBoxIcon icon = MessageBoxIcon.Error;

        question = Interaction.InputBox("Введите число: ", "Число");

        if (question == "")
        {
            MessageBox.Show("Вы не ввели значение!", "Ошибка", MessageBoxButtons.OK, icon);
        }

        
            number = Int32.Parse(question);

Conversion to another data type occurs only when the program checks the operand for elements in it. Otherwise, the user gets an error.

V
Vladimir Korotenko, 2021-01-06
@firedragon

I usually catch the status of any windows through MessageBoxResult and process all the input on a click on the submit. And somewhere at the top there is a common data object in which everything changes. A form is just buttons and handlers.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question