2012-04-26 63 views
0

好吧,我試圖從兩個不同的父母打開一個表格,我無法弄清楚如何讓它工作。我試着用AdminStartMenu和TeacherStartMenu的布爾值來使一個true,如果從一個打開,另一個打開false,但這似乎不起作用。從兩個父母打開一個孩子的形式

我如何讓一個孩子爲兩個不同的父母工作?

謝謝!

public partial class EditUser : Form 
{ 
    AdminStartMenu pf; 
    TeacherStartMenu tp; 
    public bool first = true; 
    public int st = 0; 
    public bool Editting; 
    public bool Adding; 
    public bool Viewing; 

    public bool AdminParent; 
    public bool TeacherParent; 

    public EditUser() 
    { 
     InitializeComponent(); 
    } 

    public EditUser(AdminStartMenu Parent) 
    { 

     pf = Parent; 
     InitializeComponent(); 
     EditFunction(); 
     if (pf.Adding == true) 
     { 
      BlankForm(); 
      SaveButton.Text = "Save"; 
     } 
     if (pf.Editting == true) 
     { 
      FillFormVariables(); 
      SaveButton.Text = "Save"; 
     } 
    } 

    public EditUser(TeacherStartMenu TParent) 
    { 
     tp = TParent; 
     InitializeComponent(); 
     EditFunction(); 
     if (tp.Adding == true) 
     { 
      BlankForm(); 
      SaveButton.Text = "Save"; 
     } 
     if (tp.Editting == true) 
     { 
      FillFormVariables(); 
      SaveButton.Text = "Save"; 
     } 
    } 
+0

是否有必要使用一種表單?爲什麼不使用兩種不同的形式;每個父表格一個。也許你可以創建一個通用的表單,並讓它繼承兩個子表單,以節省時間。 – annonymously 2012-04-26 02:51:55

+0

經過進一步的檢查,我認爲你需要給我們更多的信息。這段代碼有什麼問題?從我看到的那裏看來,似乎沒有任何明顯的錯誤。 – annonymously 2012-04-26 02:55:43

+0

它只是顯然不工作。有很多比這更多的代碼,這只是不起作用的部分。我可以製作第二份表格,但我寧願把它全部從這個工作中解放出來。 當我嘗試從TeacherStartMenu打開編輯用戶時,它不起作用。讓我知道如果你需要任何其他信息 – zipzapzoop45 2012-04-26 02:57:11

回答

0

儘管哈桑是正確的關於具有連接東西兩間太多,你也許能簡化一種形式調用別的,乃是把共性的東西,無論用戶的類型..也許像使用接口分類您的StartMenus。我看他們有共同的元素,讓我對你宣佈的接口,如:

public interface IMyCommonParent 
{ 
    bool Adding { get; set; } 
    bool Editing { get; set; } 
    bool Viewing { get; set; } 
} 

public class AdminStartMenu : DerivedFromSomeOtherClass, IMyCommonParent 
{ // being associated with IMyCommonParent, this class MUST have a declaration 
    // of the "Adding", "Editing", "Viewing" boolean elements 
} 

public class TeacherStartMenu : DerivedFromSomeOtherClass, IMyCommonParent 
{ // same here } 

它們都支持「IMyCommonParent」界面中,你的孩子可能形式接受支持該接口的對象。然後,您不必具體瞭解哪一個用於啓動子表單,並且可以將其保留爲表單中的屬性

public class ChildForm : Form 
{ 
    private IMyCommonParent whichMenu 
    private bool IsAdminMode; 

    public ChildForm(IMyCommonParent UnknownParent) 
    { 
     whichMenu = UnknownParent; 
     // then a flag to internally detect if admin mode or not based on 
     // the ACTUAL class passed in specifically being the admin parent instance 
     IsAdminMode = (UnknownParent is AdminStartMenu); 

     // the rest is now generic. 
     InitializeComponent(); 
     EditFunction(); 

     if(whichMenu.Adding) 
     BlankForm(); 
     else if(whichMenu.Editing) 
     FillFormVariables(); 

     SaveButton.Text = "Save"; 
    } 
} 
相關問題