0

好的,我正在尝试从两个不同的父母那里打开一份表格,但我不知道如何让它发挥作用。我尝试对 AdminStartMenu 和 TeacherStartMenu 使用布尔值,如果从一个打开,一个为真,另一个从另一个打开,则为假,但这似乎不起作用。

如何让一个孩子为两个不同的父母工作?

谢谢!

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

1 回答 1

0

尽管 Hasan 认为两者之间的耦合过多是正确的,但您也许可以简化一种调用另一种形式的表单,但无论用户类型如何,都可以放置通用的东西。也许就像使用接口对 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";
   }
}
于 2012-04-26T13:14:00.187 回答