0

I have a button created automatically, so in method events I can call values easily as:

private void ComboEmployee_SelectedValueChanged(object sender, EventArgs e)
        {
          var employeeComboBox = sender as ComboBox;
          var currentTextCombo = employeeComboBox.Text
        }

But now I have close form with button method like:

 private void btnClose_Click(object sender, EventArgs e)
        {
            var employeeComboBox = sender as ComboBox;
            var currentEmployeeComboBoxText = employeeComboBox.Text;
            SaveTechniciansToNotify(currentEmployeeComboBoxText);
            this.Close();
        }

Problem is var currentEmployeeComboBoxText = employeeComboBox.Text; always come null, it don't get current value of comboBox, how can I do to get current combobox value in this method? Regards

Pepe
  • 57
  • 4
  • You could use a `private string` field inside your form class and save the updated value to that on the `SelectedValueChanged` event, then just pass that to `SaveTechniciansToNotify` – Ryan Wilson Sep 05 '18 at 18:46
  • I think on it, but is this the best solution to solve this? @RyanWilson – Pepe Sep 05 '18 at 18:49
  • Is it the best? I really can not say if it is the best, "best" can be a matter of opinion. Will it work? Yes. Another way would be is if your control has a specific name and it's contained inside your form, you can access it like so (https://stackoverflow.com/questions/1536739/get-a-windows-forms-control-by-name-in-c-sharp), with this option, you could accomplish your goal without the private field. – Ryan Wilson Sep 05 '18 at 18:53

2 Answers2

1

An easy to implement solution is to create a private field of type string inside your form class and pass that to your method SaveTechniciansToNotify:

//Make sure this is part of your class and not local to a method
private string _comboVal;

//Set your private field inside the SelectedValueChanged event of your combo box
private void ComboEmployee_SelectedValueChanged(object sender, EventArgs e)
{
     var employeeComboBox = sender as ComboBox;
     _comboVal = employeeComboBox.Text
}

//Finally Pass private field value to method SaveTechniciansToNotify
 private void btnClose_Click(object sender, EventArgs e)
 {
            //pass in your private field value
            SaveTechniciansToNotify(_comboVal);
            this.Close();
 }
Ryan Wilson
  • 10,223
  • 2
  • 21
  • 40
0

You're running into this issue because the object sender in your btnClose_Click method is not the ComboBox (it is more than likely the Button btnClose).

By the looks of your ComboEmployee_SelectedValueChanged event handler the name of your ComboBox is ComboEmployee.

You can access the Text property of the ComboBox in your btnClose_Click handler by referencing it by name as laid out below:

private void btnClose_Click(object sender, EventArgs e)
{
    var currentEmployeeComboBoxText = ComboEmployee.Text;
    SaveTechniciansToNotify(currentEmployeeComboBoxText);
    this.Close();
}
JEllery
  • 304
  • 2
  • 13