简体   繁体   English

如何从Winforms中的其他表单刷新Usercontrol?

[英]How to refresh the Usercontrol from another form in winforms?

hi programmers, Actually i have 3 forms ie mdiparent form,mdichild and usercontrol. 嗨,程序员,实际上我有3种形式,即mdiparent形式,mdichild和usercontrol。 The usercontrol form has treeview.So i need to refresh the usercontrol from mdichildform. usercontrol窗体具有treeview.so我需要从mdichildform刷新usercontrol。 i create the instance of usercontrol and fire the event to refresh but it do not happen. 我创建了usercontrol实例,并触发了事件以刷新,但它没有发生。 Any suggestions. 有什么建议么。

              My UserControl code goes like this:

             public partial class cHumanResource : UserControl
              {
    Human_Resource_Utility human_Record = new Human_Resource_Utility();
    MyConnection mc = new MyConnection();

    TreeNode tn = new TreeNode();
    TreeNode tn_dept = new TreeNode();

    List<TreeItemInfo> treeViewList = new List<TreeItemInfo>();

    private string LoginUser;
    private List<FeaturesInfo> UserSettings = new List<FeaturesInfo>();

    public cHumanResource()
    {
        InitializeComponent();         


    }
public void LoadEmpDetail()
    {
        tstriptxtSearch.Text = "";
        EmployeeSearch("");
        TreeLoad.CollapseAll();
        Application.DoEvents();
    }
   }

And code of frmchild like: 和frmchild的代码类似:

public partial class frmEmployeeInfo : Form,IChildFormInterface
{

    MyConnection mc = new MyConnection();

    private bool _sect = false;

    string curentrymode="ENTRYDEFAULTMODE";

    public void CommandPass(string key, FeaturesInfo FinalSetting)
    {

        IMdiFormInterface mdiForm = (IMdiFormInterface) this.MdiParent;
        IHRM mdihrm;

        switch (key)
        {
            case "NEW":
                curentrymode = "ENTRYNEWMODE";

                if (CreateNew())
                {
                    mdiForm.CreateActionButtons(curentrymode,FinalSetting);



                }
                break;
            case "SAVE":
                curentrymode = "ENTRYDEFAULTMODE";

                if (SaveBasicInfo())
                {
                    mdiForm.CreateActionButtons(curentrymode,FinalSetting);


                    Application.DoEvents();
                }
                break;
            case "EDIT":
                curentrymode = "ENTRYEDITMODE";
                if (EditBasicInfo())
                {
                    mdiForm.CreateActionButtons(curentrymode,FinalSetting);
                }
                break;
            case "CANCEL":
                curentrymode = "ENTRYDEFAULTMODE";
                mdiForm.CreateActionButtons(curentrymode,FinalSetting);
                CancelBasicInfo();
                break;

            case "REFRESH":
                Refresh_Form();
                break;

            case "CLOSE":
                curentrymode = "ENTRYDEFAULTMODE";
                mdiForm.CreateActionButtons(curentrymode,FinalSetting);

                this.Close();
                this.Dispose();
                break;

            default:
                MessageBox.Show("Other Button Clicked");
                break;
        }
    }

And frmMAin like 和frmMAin喜欢

  public interface IMdiFormInterface
{
    void RemoveActionBar();
    void CreateActionButtons(string mode,FeaturesInfo SETTINGS);
    void ToggleExplorerBar();
    void ToggleExplorerBar(bool Visibility);
}

public interface IChildFormInterface { void CommandPass(string key,FeaturesInfo Settings);}



public partial class frmAttnMain : Form, IMdiFormInterface
{
    Attendance.SystemLogin syslogin = new SystemLogin();
     Utility_Mode.FileHandlingUtility fileHandling = new Attendance.Utility_Mode.FileHandlingUtility();
    string seltext = "100%";
    string selgototxt = "1";
    private static string CurUser=string.Empty;
    private List<FeaturesInfo> menuname =null;
    private int totalpage = 0;
    FeaturesInfo Fmodel = new FeaturesInfo();
    private static frmAttnMain aForm = null;
    private static List<FeaturesInfo> aFavmodel;
    public static frmAttnMain Instance(string usercode,List<FeaturesInfo> fmodel,string Action)
    {
        aFavmodel = fmodel;
       // aForm.loadDatabase();
     CurUser = usercode;
}

As long has you have a reference to the nested objects that allow you "navigate" through them you can access their behavior(in your case refresh a control). 只要您对允许您通过它们“导航”的嵌套对象进行引用,就可以访问它们的行为(在您的情况下,刷新控件)。 you don't have to create a new instance of the user control otherwise you will refresh the Treeview of the new control that you have just created instead of the one on the form. 您不必创建用户控件的新实例,否则您将刷新刚创建的新控件的Treeview而不是窗体上的Treeview。

In General you need to do something like 一般来说,您需要做类似的事情

ParentForm.ChildForm.RefreshUserControl() (which may be a public method that refresh the control on the ChildForm using the reference on that form) ParentForm.ChildForm.RefreshUserControl() (可以是使用该窗体上的引用刷新ChildForm上的控件的公共方法)

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

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