Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 表单上的用户控件之间的通信_C#_Winforms_User Controls - Fatal编程技术网

C# 表单上的用户控件之间的通信

C# 表单上的用户控件之间的通信,c#,winforms,user-controls,C#,Winforms,User Controls,当我的用户控件父项为表单时,我想通过单击按钮从另一个调用usercontrol函数/方法 形式上;我加载并添加用户控件: //Controls enum holding names of all our user controls public enum ControlsEnum { SPLASH_CONTROL, MAIN_CONTROL, CATEGORY_CONTROL, } public partial class MainForm : Form { //

当我的用户控件父项为表单时,我想通过单击按钮从另一个调用usercontrol函数/方法

形式上;我加载并添加用户控件:

//Controls enum holding names of all our user controls
public enum ControlsEnum
{
    SPLASH_CONTROL,
    MAIN_CONTROL,
    CATEGORY_CONTROL,
}
public partial class MainForm : Form
{
    //Dictionary that holds all our instantiated user controls.
    private IDictionary<ControlsEnum, Control> controls = new Dictionary<ControlsEnum, Control>();

    public MainForm()
    {
        InitializeComponent();
        //When the program runs for the first time, lets call the ShowControl method that
        //will show the first control
        ShowControl(ControlsEnum.SPLASH_CONTROL);
    }

    public void ShowControl(ControlsEnum ctrl)
    {
        Control new_ctrl = null;

        //If our dictionary already contains instance of
        if (controls.ContainsKey(ctrl))
        {
            new_ctrl = controls[ctrl];
        }
        else
        {
            new_ctrl = CreateControl(ctrl);
            controls[ctrl] = new_ctrl;
        }

        new_ctrl.Parent = this;
        new_ctrl.Dock = DockStyle.Fill;
        new_ctrl.BringToFront();
        new_ctrl.Show();
    }

    private Control CreateControl(ControlsEnum ctrl)
    {
        switch (ctrl)
        {
            case ControlsEnum.SPLASH_CONTROL:
                return new Splash_uc();
            case ControlsEnum.MAIN_CONTROL:
                return new Main_uc();
            case ControlsEnum.CATEGORY_CONTROL:
                return new Categoty_uc();
            default:
                return null;
        }
    }
主要是:

private void datagridview_Refresh()
{
  //rebind datagridview datasource
  // i want call this function from "category_uc" when click on Save Button
}

请帮帮我!tnx

您可以将事件添加到控件中:

class Categoty_uc
{
  public event EventHandler ButtonClicked;

  protected OnButtonClicked()
  {
    var tmp = ButtonClicked;

    if(tmp != null)
    {
      tmp(this, EventArgs.Empty);
    }
  }

  private void btn_categorySave_Click(object sender, EventArgs e)
  {
    OnButtonClicked();
  }
}
然后以你的主要形式:

private Main_uc main_uc = null;

private Control CreateControl(ControlsEnum ctrl)
{
  Control ret = null;

  switch (ctrl)
  {
    case ControlsEnum.CATEGORY_CONTROL:
    {
      if(main_uc != null)
      {
        ret = new Categoty_uc();
        ((Categoty_uc)ret).ButtonClicked += (sender, e) => 
                          {main_uc.datagridview_Refresh();}
      }
      else
      {
        throw new Exception("Create Main_uc first!");
      }
    }
    break;
    case ControlsEnum.MAIN_CONTROL:
    {
      if(main_uc == null)
      {
        main_uc = new Main_uc();
      }
      ret = main_uc;
    }
  }

  return ret;
}

谢谢你的回答。我运行您的代码,但出现错误:datagridview\u刷新不存在。我想从category_uc()调用datagridview_Refresh。这个函数存在于main_uc上……如果您想这样做,请将您的Category_uc对象传递给main_uc对象。但是你为什么不想像我上面建议的那样从MainForm调用函数呢?你需要做的是在main上创建public方法,比如
public void RefreshDataGridView()
并调用它,而不是从MainForm调用
datagridview\u Refresh()
。我运行了你的代码,但是
public void RefreshDataGridView()
Main\u uc
中的
Category\u uc
上不可用,VS2012将显示错误:
名称RefreshDataGridView在当前上下文中不存在
。请帮忙!tnx.将代码中的
private void datagridview\u Refresh()
更改为
public void datagridview\u Refresh()
。看看我是如何修改我的答案的。
private Main_uc main_uc = null;

private Control CreateControl(ControlsEnum ctrl)
{
  Control ret = null;

  switch (ctrl)
  {
    case ControlsEnum.CATEGORY_CONTROL:
    {
      if(main_uc != null)
      {
        ret = new Categoty_uc();
        ((Categoty_uc)ret).ButtonClicked += (sender, e) => 
                          {main_uc.datagridview_Refresh();}
      }
      else
      {
        throw new Exception("Create Main_uc first!");
      }
    }
    break;
    case ControlsEnum.MAIN_CONTROL:
    {
      if(main_uc == null)
      {
        main_uc = new Main_uc();
      }
      ret = main_uc;
    }
  }

  return ret;
}