使用用户控制的表单不会返回值

时间:2016-07-03 19:45:02

标签: c# events delegates

我正在编写用户控件。我希望它在用户双击客户时返回客户编号。我似乎无法让它发挥作用。将显示用户控件,并在双击时,数据显示在消息框中,但我似乎无法更新主窗体上的值。

无论何时我尝试在FindCustomerControl_ItemHasBeenSelected中添加返回值,我都会收到错误消息。它就像是在用户控件中挂起,我不能给它留下返回值。到目前为止,这就是我所拥有的:

在我的主窗口中:

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;
    }

}

在我的用户控件中:

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
    }


}

似乎这应该是足够普遍的,但是,尽管经过了数小时的研究和调试,我还是无法提出解决方案。我知道uc.ItemHasBeenSelected + = uc_ItemHasBeenSelected;在TestForm中似乎没有被执行,因为我在那里放了一个断点。

2 个答案:

答案 0 :(得分:1)

据我了解,我猜你可以使用应用程序当前资源,在那里你可以保存你想要的任何值 - 在UWP中它是这样的:

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

然后您可以将此值转换为所需的类型:

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

现在您可以在任何地方使用此值。

希望不要以任何方式帮助。

答案 1 :(得分:0)

用户类没有任何问题。它工作正常。问题是TestForm需要在没有FindCustomerControl的情况下启动并在程序中实例化控件。它将值返回到标签或其他任何需要的位置。非常感谢Brad Rem和这篇文章: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();
    }

}