Search code examples
c#winformscomboboxtextbox

Checking multiple comboboxes and textboxes for content


I'm trying to check a couple of comboboxes and textboxes if they have anything written or selected in them (via a Button click). Is there a compact solution, so that I don't have to write messy code? Below is a picture of the interface in question.

enter image description here


Solution

  • For plain WinForms:

    enter image description here

    You could handle the click-Event:

    public partial class Form1 : Form
    {
    public Form1()
    {
      InitializeComponent();
    }
    
    private void button1_Click(object sender, EventArgs e)
    {
      var canExecute =
        !string.IsNullOrWhiteSpace(this.textBox1.Text) &&
        this.comboBox1.SelectedItem != null;
    
      if (canExecute)
      {
        // do stuff
      }
      else
      {
        MessageBox.Show("Input missing!");
      }
    
    }
    }