如何在winforms中从另一个表单刷新Usercontrol?

时间:2012-03-19 10:06:06

标签: c# winforms user-controls

程序员,                   实际上我有3种形式,即mdiparent形式,mdichild和usercontrol。 usercontrol表单有treeview.So我需要从mdichildform刷新usercontrol。我创建usercontrol的实例并触发事件以刷新,但它不会发生。任何建议。

              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();
    }
   }

和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;
        }
    }

和frmMin一样

  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;
}

1 个答案:

答案 0 :(得分:0)

只要您拥有对允许“导航”它们的嵌套对象的引用,您就可以访问它们的行为(在您的情况下刷新控件)。 您不必创建用户控件的新实例,否则您将刷新刚刚创建的新控件的Treeview而不是表单上的那个。

总的来说,你需要做一些像

这样的事情

ParentForm.ChildForm.RefreshUserControl()(可能是使用该表单上的引用刷新ChildForm控件的公共方法)