简体   繁体   中英

Empty String Textbox.Text inside lambda expression event listener

feel like I'm missing something simple here. Have a form that looks like this:

public partial class UpdateCustomerForm : Form
    {
        public UpdateCustomerForm(UpdateCustomer customer)
        {
            InitializeComponent();      

            updateCustomerBttn.Click += (sender, e) => HandleID(customer);

        }

        private void updateCustomerBttn_Click(object sender, EventArgs e)
        {
            string customerName = nameTb.Text; // This works
            this.Close();

        }

        private void HandleID(UpdateCustomer customer)
        {
            //all below values are empty

            UpdateCustomer customerToUpdate = new UpdateCustomer()
            {
                CustomerID = customer.CustomerID,
                CustomerName = nameTb.Text,
                Address1 = addressTb.Text,
                Address2 = address2Tb.Text,
                Phone = phoneTb.Text,
                City = cityTB.Text,
                Country = countryTb.Text

            };

            Customer.UpdateCustomer(customerToUpdate);

            CustomerForm CustomerForm = (CustomerForm)Application.OpenForms["CustomerForm"];
            CustomerForm.PopulateDGV();

            
        }

    }

I don't understand why the.text values of all the textboxes are not represented in the HandleID method. All come in as empty strings. Is there something I need to do to be able to access the values? Thanks!

For those who have an issue similar to this one:

There are two methods subscribed to the Click event here. The first one is:

updateCustomerBttn.Click += (sender, e) => HandleID(customer);

and the second one is:

private void updateCustomerBttn_Click(object sender, EventArgs e)

which is subscribed in InitializeComponent() .

Now, the problem here is that because updateCustomerBttn gets subscribed before HandleID , it also gets executed before HandleID when the event is called. Which means the Form is closed before HandleID gets executed, which is causing the problem.

You can of course change the order in which you subscribe the two methods, but you can already see that this can get really messy over time. So the better option is to move this.Close() to HandleID , or - if you wanna be really clean - add it to the lambda in the constructor:

public partial class UpdateCustomerForm : Form
{
    public UpdateCustomerForm(UpdateCustomer customer)
    {
        InitializeComponent();      
        updateCustomerBttn.Click += (sender, e) => {
            HandleID(customer);
            this.Close();
        };
    }

    private void HandleID(UpdateCustomer customer)
    {
        UpdateCustomer customerToUpdate = new UpdateCustomer()
        {
            CustomerID = customer.CustomerID,
            CustomerName = nameTb.Text,
            Address1 = addressTb.Text,
            Address2 = address2Tb.Text,
            Phone = phoneTb.Text,
            City = cityTB.Text,
            Country = countryTb.Text
        };

        Customer.UpdateCustomer(customerToUpdate);

        CustomerForm CustomerForm = (CustomerForm)Application.OpenForms["CustomerForm"];
        CustomerForm.PopulateDGV();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM