Understanding the Behavior of Windows Forms (C#) Development to Prevent Buttons from Being Pressed When Items Are Null

Asked 2 years ago, Updated 2 years ago, 127 views

I am currently programming in Visual Studio Windows Forms (C#).
The program you want to create is
·Select one of the choices in the combo box
→ Enter a number in the text box
→ Press the button
That's it.

◆What you want to achieve
For both items, if either is Null, prevent the button from being pressed.
(If you haven't already entered everything, you won't be able to press the button.)

◆What do you want to ask
Are the above features feasible?
If so, what code can I write to implement it?

I'm still a beginner at C#, but I appreciate your cooperation.

c# visual-studio winforms

2022-09-29 20:28

1 Answers

That's what I think.

  • Prevents pressing as requested

    • Initially set Button.Enabled=false
    • Register event handlers for both ComboBox and TextBox TextChanged events
    • In those event handlers, it is determined whether the Text properties of both controls are valid data (IsNullOrEmpty/IsNullOrWhiteSpace), and Button.Enabled=true when both are valid data.If either data is invalid, use Button.Enabled=false.
  • Pressable but not working

      <li> At the head of an event handler of a click event of Button, whether or not both Text properties of ComboBox and TextBox are valid data is determined, and when both are valid data, processing is performed when Button is depressed.If either data is invalid, no processing is performed, but an event handler is terminated by performing a warning/guide display or the like.

Make it impossible to press as requested

  • Initially set Button.Enabled=false
  • Register event handlers for both ComboBox and TextBox TextChanged events
  • In those event handlers, it is determined whether the Text properties of both controls are valid data (IsNullOrEmpty/IsNullOrWhiteSpace), and Button.Enabled=true when both are valid data.If either data is invalid, use Button.Enabled=false.

Pressable but not working

    <li> At the head of an event handler of a click event of Button, whether or not both Text properties of ComboBox and TextBox are valid data is determined, and when both are valid data, processing is performed when Button is depressed.If either data is invalid, no processing is performed, but an event handler is terminated by performing a warning/guide display or the like.


2022-09-29 20:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.