简体   繁体   English

将用户控件中选择的值传递给父级

[英]Pass Value Selected From a User Control to a Parent

I have a Winform dialog that contains several user controls - all of them are some sort of Datagridview . 我有一个Winform对话框,其中包含多个用户控件-所有这些都是某种Datagridview The main parent has details about a user, and the user controls each have additional details on that person. 主要父母有关于用户的详细信息,并且用户控件每个都有关于该人的其他详细信息。 When my Dialog first loads all of the UserControls work but I am trying to figure out how to update the UserControl2 based on a position change in UserControl1. 当我的对话框第一次加载所有UserControls时,但是我试图根据UserControl1中的位置变化来弄清楚如何更新UserControl2。

So, I am trying to select a row in UserControl1 and have the data in UserControl2 update based on a value that I just selected. 因此,我试图在UserControl1中选择一行,并根据我刚刚选择的值更新UserControl2中的数据。

I have tried using MouseDownEvents on the UserControl1 and BindingSourcePositionChanged but I can't figure out how to get the value selected back to my parent form and then use that value to refresh the other datagrids? 我试过在UserControl1和BindingSourcePositionChanged上使用MouseDownEvents ,但是我不知道如何将选择的值返回到我的父窗体,然后使用该值刷新其他数据网格?

I looked at delegates and events but I guess the lack of sleep is making it incredibly hard to comprehend. 我看了看代表和活动,但我想缺乏睡眠使人们难以理解。 I understand that I need to create my delegate and event on the UserControl1 and then somehow call it on my mainform but that's where I get stuck and have no clue where to start. 我知道我需要在UserControl1上创建我的委托和事件,然后以某种方式在我的主窗体上调用它,但这就是我遇到的麻烦,不知道从哪里开始。

Is this the right direction? 这是正确的方向吗? Or is there another way to get this done? 还是有另一种方法可以做到这一点? Can anyone offer up any suggestions on how this works? 谁能提供有关此工作原理的任何建议?

Yes this is the correct approach something like the following will provide an event handler that you can use to the retrieve a public property from the UserControl: 是的,这是正确的方法,类似于以下内容,它将提供一个事件处理程序,您可以使用该事件处理程序从UserControl中检索公共属性:

public class SomeClass : BaseControl
{
    public event EventHandler PersonSelected;

    public string Name{get;set;}

    protected void FindUser()
    {
        var find = new Button {ID = (ToString() + "search"), Text = "Search"};
            find.Click += delegate(object sender, EventArgs e)
                              {
                                  if (PersonSelected!= null)
                                  {
                                      //forward this event to the page's event handler
                                      PersonSelected(this, e);
                                  }
                              }; 
     }
}

public class SomeOtherClass : Page
{
    public void Main()
    {

       var sp = (SomeClass)Control;
                        sp.PersonSelected += BtnClick;
     }

    public void BtnClick(object sender, EventArgs e)
    {
        //Get some value from the (SomeClass)Control here
     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM