2012-03-19 62 views
0

嗨程序員, 其實我有3種形式,即mdiparent形式,mdichild和用戶控件。 usercontrol窗體有treeview.So我需要從mdichildform刷新usercontrol。我創建了用戶控件的實例並激發了事件刷新,但不會發生。有什麼建議麼。如何從winforms中的其他窗體刷新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 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; 
} 
+0

你能後的你一些示例代碼到目前爲止做了什麼? – aleroot 2012-03-19 10:08:00

+0

您正在創建一個用戶控件的新實例來添加永遠不會工作的樹,使用您在mdiChild窗體上添加的用戶控件的相同實例來重新創建:D – BreakHead 2012-03-19 10:11:10

回答

0

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

而且frmMAin只要有你通過他們必須嵌套對象,讓你「導航」的引用您可以訪問他們的行爲(在你的情況下刷新一個控件)。 您不必創建用戶控件的新實例,否則將刷新剛剛創建的新控件的Treeview,而不是窗體上的控件。

一般情況下,你需要做的是這樣

ParentForm.ChildForm.RefreshUserControl()(這可能是刷新使用該表格上的基準上ChildForm控制的公共方法)

相關問題