使用用户控件的表单不返回值

本文关键字:表单 返回值 控件 用户 | 更新日期: 2023-09-27 17:54:32

我正在编写一个用户控件。我希望它在用户双击客户时返回一个客户号码。我似乎不能使它工作。显示用户控件,在双击时,数据显示在消息框中,但我似乎无法让它更新主表单上的值。

任何时候我试图添加一个返回值到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
    }

}

似乎这应该是足够普遍的,但是,尽管我花了几个小时的研究和调试,我还是无法想出一个解决方案。我确实知道。ItemHasBeenSelected += uc_ItemHasBeenSelected;in TestForm似乎永远不会被执行,因为我在那里放了一个断点。

使用用户控件的表单不返回值

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

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

然后可以将该值强制转换为所需的类型:

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

现在你可以在任何你想要的地方使用这个值

希望能有所帮助。

用户类没有问题。它工作得很好。问题是TestForm需要在没有FindCustomerControl的情况下启动,并在程序中实例化该控件。它将值返回到标签或其他需要的地方。非常感谢Brad Rem和这篇文章:用户操作后usercontrol返回值

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

}