表格基于所选菜单项

时间:2013-05-19 09:09:01

标签: c# winforms

我制作了一个侧边栏,我停靠在应用程序的左侧。

现在我想知道根据他们从侧边栏中选择的菜单选项显示表单的最佳方法是什么。

这基本上就是我想做的事情:

http://www.dreamincode.net/forums/topic/176782-building-an-application-poscash-register-part-one/

左侧是我的菜单栏,右侧我希望根据左侧点击的选项提供表单。

我已经研究过MDI,但是当我这样做时,我总是得到一个ControlBox,即使我已经在子表单中禁用了它。

更新:似乎这也有效:

看起来您也可以创建自己的用户控件来执行此操作:

User controls

3 个答案:

答案 0 :(得分:0)

首先在表单中创建一个面板,用于保存当前表单的内容(将其添加到表单中,但暂时将其留空)。

Panel active = new Panel();
this.Controls.Add(active);

然后创建一个面板,其中包含要显示的每个表单的控件。

Panel firstFormPanel = new Panel();
firstFormPanel.Add(new Button());
Panel secondFormPanel = new Panel();
secondFormPanel.Add(new Button());

现在默认分配您想要的面板:

active = firstFormPanel;

然后,当您想要更改为新表单(单击侧栏中的事件)时,将其中一个面板分配给活动面板,如下所示:

active.Visible = false;
active = secondFormPanel;
active.Visible = true;

答案 1 :(得分:0)

如果您想要第二个不允许您访问主表单,除非它已关闭。然后使用此...

second_form_name    sfn = new second_form_name();
sfn.ShowDialog();

如果您希望第二次允许您访问主表单,除非它已关闭。然后使用

 second_form_name    sfn = new second_form_name();
  sfn.Show();

答案 2 :(得分:0)

这是我的游戏侧边栏的一个小例子,我可以将新的子菜单滑入视觉范围。我使用计时器来控制移动,子菜单列表也是一个索引,用于确定要显示的内容。至于我到目前为止只有一个,所以不是一个主要的例子。

    public List<UserControl> Submenus = new List<UserControl>();
    Multiplayer_Menu MPM;
    enum At { Left, Right }
    At Current = At.Right;
    At Go_to = At.Right;
    int Submenu_Index = 0;
    bool done = false;

    public void Load_Submenus()
    {
        Multiplayer_Menu MM = new Multiplayer_Menu(this);
        MainMenu.Controls.Add(MM);
        MM.Location = new Point(MainMenu.Size.Width, 0);
        MM.Visible = false;
        Submenus.Add(MM);
        PictureBox PB = new PictureBox();
        MainMenu.Controls.Add(PB);
        PB.Location = new Point(MainMenu.Size.Width, 0);
        PB.Size = new System.Drawing.Size(924, 736);
        PB.SizeMode = PictureBoxSizeMode.StretchImage;
        PB.ImageLocation = "http://www.taloreal.com/Earth%20Rotation/Rotating.gif";
        PB.Visible = true;
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Load_Submenus();
    }

    public void MML_Multiplayer_Click(object sender, EventArgs e)
    {
        Submenus[Submenu_Index].Visible = false;
        if (Current == At.Left)
            Go_to = At.Right;
        if (Current == At.Right)
            Go_to = At.Left;
        ShowHideMenus.Enabled = true;
        Submenu_Index = 0;
    }

    private void ShowHideMenus_Tick(object sender, EventArgs e)
    { 
        Point P = new Point(MainMenu.Location.X, MainMenu.Location.Y);
        Size S = new Size(MainMenu.Size.Width, MainMenu.Size.Height);
        if (Go_to == At.Left)
        {
            P = new Point(P.X - 30, P.Y);
            S = new Size(S.Width + 30, S.Height);
            if (P.X == 0)
            {
                Submenus[Submenu_Index].Visible = true;
                ShowHideMenus.Enabled = false;
                Current = Go_to;
            }
        }
        if (Go_to == At.Right)
        {
            P = new Point(P.X + 30, P.Y);
            S = new Size(S.Width - 30, S.Height);
            if (P.X == 930)
            {
                ShowHideMenus.Enabled = false;
                Current = Go_to;
            }
        }
        Reposition_Menu(P, S);
    }

    void Reposition_Menu(Point P, Size S)
    {
        MainMenu.Location = P;
        MainMenu.Size = S;
    }
相关问题