-2

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Missy
  • 1,286
  • 23
  • 52
  • "Anytime I try to add a return value into FindCustomerControl_ItemHasBeenSelected, I get an error. ". [Which error exactly do you get and how do you try to add it](http://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist)? Also if I guessed right what you have tried, then [this](http://stackoverflow.com/questions/1210026/return-a-value-from-an-event-is-there-a-good-practice-for-this) may give you some ideas. – Eugene Podskal Jul 03 '16 at 20:21
  • You say the value is shown in the `MessageBox`, but the value is not shown in the `TestForm.lblCustomer` text box, implying that your `TestForm.uc_ItemHasBeenSelected()` method is never called. Since the message box is shown, the `ItemHasBeenSelected` event is obviously being raised as expected. As I see it, that leaves two possibilities: your `TestForm` is not subscribed to the event, or your `lblCustomer` is in fact being updated, but for some reason the label text isn't visible. – Peter Duniho Jul 03 '16 at 20:48
  • Without a good [mcve] that reliably reproduces the problem, it's not possible with just your question to know what the problem is. Either improve the question so that it's answerable, or do more debugging yourself. Note that your code shows subscribing to the event on _some_ object, but since you didn't show `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. – Peter Duniho Jul 03 '16 at 20:48
  • Doesn't this subscribe the Test Form to the event? uc.ItemHasBeenSelected += uc_ItemHasBeenSelected; Also, I took out the MakeUserControlPrimaryWindow() because it didn't seem to make any difference and I was trying to make my code succinct. – Missy Jul 03 '16 at 21:02

2 Answers2

1

As I understood I guess you could use application current resources, where you can save any value you wish - in UWP it is something like this:

Application.Current.Resources["Name"] = customerNumber

Then you can cast this value to desired type:

(int)Application.Current.Resources["Name"]

Now you can use this value wherever you want.

Hope than helped in any way.

0

There is nothing wrong with the user class. It works fine. The problem is that the TestForm needs to start without the FindCustomerControl on it and instantiate the control within the program. It returns the value into the label or wherever else it needs to. Thanks very much to Brad Rem and this post: Return value from usercontrol after user action

 public partial class TestForm : Form
   {
     public TestForm()
    {
        InitializeComponent();
        ShowSelectFromListWidget();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Close();
    }


    private void ShowSelectFromListWidget()
    {
        var uc = new FindCustomerControl();
        uc.ItemHasBeenSelected += uc_ItemHasBeenSelected;
        this.MakeUserControlPrimaryWindow(uc);
    }

    void uc_ItemHasBeenSelected(object sender, FindCustomerControl.SelectedItemEventArgs e)
    {
        var value = e.SelectedChoice;
        lblCustomer.Text = value;
        lblMerchant.Focus();

        //ClosePrimaryUserControl();
    }

    private void MakeUserControlPrimaryWindow(UserControl uc)
    {
        // my example just puts in in a panel, but for you
        // put your special code here to handle your user control management 
        panel1.Controls.Add(uc);
    }


    private void ClosePrimaryUserControl()
    {
        // put your special code here to handle your user control management 
        panel1.Controls.Clear();
    }

}

Community
  • 1
  • 1
Missy
  • 1,286
  • 23
  • 52