C#界面多窗体之间切换的方法 - 小众知识

C#界面多窗体之间切换的方法

2015-03-18 15:22:56 苏内容
  标签: C//窗体/界面
阅读:6837

做界面切换时是在同一个form里用panel来放入控件来进行界面切换的 但是当所需界面很多时就form就显得很乱 我现在是想将所需要的多个界面分成多个子窗体,当点击按钮时,子窗体就会嵌入到主窗体中,这应该要怎么做

private void btnNewButtonEvent (object sender, EventArgs e)
{
    ShowForm (sender as NewPictureBox);
}
private void ShowForm (NewPictureBox sender)
{
    try
    {
        this.pan_DAccount.Visible = false;
        this.Cursor = Cursors.WaitCursor;

        Type t = SetUI (sender.ShowFormName);
        if (t == null)
        {
            return;
        }
        Form f = (Form) Activator.CreateInstance (t, true);

        if (f != null)
        {
            NewMethod (f);
        }
    }
    catch (Exception ex)
    {

    }
    finally
    {
        this.Cursor = Cursors.Default;
    }
}
public Type SetUI (string uiName)
{
    try
    {
        Assembly assmbly = Assembly.LoadFrom ("TengePay.exe");
        Type type = assmbly.GetType ("TengePay." + uiName);
        return type;
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

public void NewMethod (Form frmp)
{
    bool isOpen = true;
    foreach (Form frm in this.MdiChildren) //遍历已打开的MDI
    {
        if (frm.Name == frmp.Name)
        {
            frm.Activate();//赋予焦点
            frm.WindowState = FormWindowState.Normal;//设置窗体最大化
            isOpen = false;
            break;
        }
        this.BeginInvoke (new ColseFormHander (delegate()
        {
            if (frm.Name != this.Name)
                frm.Close();
        }) );
    }
    if (frmp.Name == this.Name)
    {
        this.Dispose();
        return;
    }
    if (!isOpen) //如果没有找到相同窗体则打开新窗体
    {
        return;
    }
    if (frmp.Tag != null && frmp.Tag.ToString() == "No")
    {
        this.BeginInvoke (new EventerFromDelegate (delegate
        {
            frmp.Dock = DockStyle.Fill;
            (frmp as BaseForm).ResultPrentForm = this;
            frmp.ShowDialog();
        }) );

    }
    else
    {
        frmp.MdiParent = this;
        frmp.Dock = DockStyle.Fill;
        frmp.Show();
        frmp.Activate();
    }
}

扩展阅读
相关阅读
© CopyRight 2010-2021, PREDREAM.ORG, Inc.All Rights Reserved. 京ICP备13045924号-1