代码之家  ›  专栏  ›  技术社区  ›  Missy

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

  •  -2
  • Missy  · 技术社区  · 8 年前

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

    每当我尝试将返回值添加到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_ItemHas;在TestForm中似乎从未执行过,因为我在那里放置了一个断点。

    2 回复  |  直到 8 年前
        1
  •  1
  •   Patrycjusz Naklicki    8 年前

    据我所知,我想您可以使用应用程序当前资源,在这里您可以保存任何您希望的值-在UWP中,它是这样的:

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

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

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

    现在,您可以随时随地使用该值。

    希望在任何方面都比帮助大。

        2
  •  0
  •   Community CDub    8 年前

    用户类没有问题。它工作得很好。问题是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();
        }
    

    }