I am writing a user control. I want it to return a customer number when the user double clicks on the customer. I can't seem to get it to work. The user control is displayed and, on the double click, the data shows in the messagebox but I can't seem to get it to update the value on the main form.
Anytime I try to add a return value into FindCustomerControl_ItemHasBeenSelected, I get an error. It's like it hangs out in the user control and I can't leave it with a return value. So far, this is what I have:
In my main window:
public partial class TestForm : Form { public TestForm() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { // one close button on the form Close(); } private void ShowSelectFromListWidget() { // show the user control var uc = new FindCustomerControl(); uc.ItemHasBeenSelected += uc_ItemHasBeenSelected; MakeUserControlPrimaryWindow(uc); } void uc_ItemHasBeenSelected(object sender, FindCustomerControl.SelectedItemEventArgs e) { // once it has been selected, change a label on the screen to show the customer number var value = e.SelectedChoice; lblCustomer.Text = value; } } In my user control:
public partial class FindCustomerControl : UserControl { public class SelectedItemEventArgs : EventArgs { public string SelectedChoice { get; set; } } public event EventHandler<SelectedItemEventArgs> ItemHasBeenSelected; public FindCustomerControl() { InitializeComponent();} DataTable dt = new DataTable(); public void btnFind_Click(object sender, EventArgs e) { var dt = GetData(); dataGridView1.DataSource = dt; } //Query database public DataTable GetData() { UtilitiesClass ru = new UtilitiesClass(); string connectionString = ru.getConnectionString(); DataTable dt = new DataTable(); SqlConnection myConnection = new SqlConnection(connectionString); myConnection.Open(); SqlCommand cmd = new SqlCommand("FindCustomer", myConnection); cmd.Parameters.AddWithValue("@customer", txtCustomer.Text.Trim()); cmd.CommandType = CommandType.StoredProcedure; SqlDataAdapter ta = new SqlDataAdapter(cmd); ta.Fill(dt); myConnection.Close(); return (dt); } private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { var handler = ItemHasBeenSelected; string choice = dataGridView1[0, e.RowIndex].Value.ToString(); // this shows it MessageBox.Show("Chosen: " + e.SelectedChoice); if (handler != null) handler(this, new SelectedItemEventArgs { SelectedChoice = choice }); // I WOULD LIKE TO RETURN A VALUE HERE } } It seems as though this should be common enough but, in spite of my hours of research and debugging, I have been unable to come up with a solution. I do know that uc.ItemHasBeenSelected += uc_ItemHasBeenSelected; in TestForm doesn't seem to ever get executed because I put a breakpoint there.
MessageBox, but the value is not shown in theTestForm.lblCustomertext box, implying that yourTestForm.uc_ItemHasBeenSelected()method is never called. Since the message box is shown, theItemHasBeenSelectedevent is obviously being raised as expected. As I see it, that leaves two possibilities: yourTestFormis not subscribed to the event, or yourlblCustomeris in fact being updated, but for some reason the label text isn't visible.MakeUserControlPrimaryWindow(), it's not possible to confirm you've subscribed to the event on the correct object. For all we know you've got another instance of the user control that is actually accepting user input.