簡體   English   中英

從兩個父母那里打開一個孩子表格

[英]Opening one child form from two parents

好的,我正在嘗試從兩個不同的父母處打開一種表格,但我不知道該如何使用它。 我嘗試對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";
        }
    }

盡管Hasan正確地將兩者之間的內容聯系在一起是正確的,但是您可以簡化一種調用另一種形式的方法,但是不管用戶的類型如何,都可以放一些普通的東西。也許像使用Interface來對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";
   }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM