简体   繁体   中英

How to refresh the Usercontrol from another form in winforms?

hi programmers, Actually i have 3 forms ie mdiparent form,mdichild and usercontrol. The usercontrol form has treeview.So i need to refresh the usercontrol from mdichildform. i create the instance of usercontrol and fire the event to refresh but it do not happen. 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:

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

  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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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