winforms C#子窗体未显示在面板中

whhtz7ly  于 2023-03-24  发布在  C#
关注(0)|答案(1)|浏览(218)

我正在使用C# winforms开发一个简单的应用程序用户界面。
问题是在我单击按钮后,子窗体似乎没有显示在面板上。
我基于我在网上看到的一个教程,这里是下面的代码:

using Microsoft.VisualBasic.ApplicationServices;

using System.Drawing;
using System.Windows.Forms;
using System;
using IronPython.Hosting;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;
using System.Runtime.InteropServices;
using Iris_0._1.Forms;
using FontAwesome.Sharp;
using System.Windows.Media;
using Color = System.Drawing.Color;

namespace Iris_0._1
{
    public partial class Form1 : Form
    {
        //Fields
        private IconButton currentBtn;
        private Panel leftBorderBtn;

        private Form currentChildForm;

        public Form1()
        {
            InitializeComponent();
            leftBorderBtn = new Panel();
            leftBorderBtn.Size = new Size(7, 50);
            panelMenu.Controls.Add(leftBorderBtn);

            this.Text= string.Empty;
            this.ControlBox = false;
            this.DoubleBuffered = true;
            this.MaximizedBounds = Screen.FromHandle(this.Handle).WorkingArea; 

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

        }

        // CREATION OF TITLE BAR
        [DllImport("user32.DLL", EntryPoint = "ReleaseCapture")]
        private extern static void ReleaseCapture();
        
        [DllImport("user32.DLL", EntryPoint = "SendMessage")]
        private extern static IntPtr SendMessage(System.IntPtr hWnd, int wMsg, int wParam, int lParam);

        private void panelTitleBar_MouseDown(object sender, MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(this.Handle, 0x112, 0xf012, 0);
        }

        //LEFT MENU Highlight ACTIVATED BUTTON

        private void ActivateButton(Object  senderBtn, Color color)
        { 
            if (senderBtn != null)
            {
                DisableButton();
                currentBtn = (IconButton)senderBtn;
                currentBtn.BackColor = Color.FromArgb(37, 36, 81);
                currentBtn.ForeColor = color;
                currentBtn.TextAlign = ContentAlignment.MiddleCenter;
                currentBtn.IconColor = color;
            }
        }
        private void DisableButton()
        {
            if (currentBtn != null)
            {
                currentBtn.BackColor = Color.FromArgb(51, 51, 76);
                currentBtn.ForeColor = Color.Gainsboro;
                currentBtn.TextAlign = ContentAlignment.MiddleLeft;
                currentBtn.IconColor = Color.Gainsboro;
            }
        }

        //LEFT MENU BUTTON CONTROL
        private void OpenChildForm(Form childForm)
        {
            //open only form
            if (currentChildForm != null)
            {
                currentChildForm.Close();
            }
            currentChildForm = childForm;
            //End
            childForm.TopLevel = false;
            childForm.FormBorderStyle = FormBorderStyle.None;
            childForm.Dock = DockStyle.Fill;
            panelDesktop.Controls.Add(childForm);
            panelDesktop.Tag = childForm;
            childForm.BringToFront();
            childForm.Show();
            //lblTitleChildForm.Text = childForm.Text;
        }

        private void BtnHome_Click(object sender, EventArgs e)
        {
            ActivateButton(sender, Color.FromArgb(255, 255,255));
            OpenChildForm(new formHome());
        }

        private void btnRegisterFace_Click(object sender, EventArgs e)
        {
            ActivateButton(sender, Color.FromArgb(255, 255, 255));
            OpenChildForm(new formRegisterFace());
        }

        private void btnRecords_Click(object sender, EventArgs e)
        {
            ActivateButton(sender, Color.FromArgb(255, 255, 255));
            OpenChildForm(new formRecords());
        }

        private void btnAnalytics_Click(object sender, EventArgs e)
        {
            ActivateButton(sender, Color.FromArgb(255, 255, 255));
            OpenChildForm(new formAnalytics());
        }
    }
}

由于某些原因,我无法调用/打开我在应用程序上选择的面板上的childForm:
这是App的样子:

基本上,当我运行BtnHome_Click事件时,这个方法应该打开选定的子窗体:
x一个一个一个一个x一个一个二个x
这是其中一个表单的样子,例如:

希望有人能帮我。
最好的,

6xfqseft

6xfqseft1#

我认为您应该使用一个TabControl并添加多个TabPage对象,每个表单对应一个对象

总体上添加一个SplitControl将内容与菜单分开,并使菜单成为TableLayoutControl,每行包含一个RadioButton,显示样式为按钮和图像。
在每个单选按钮上单击,您将显示正确的TabPage

private void homeRadioButton_CheckedChanged(object sender, EventArgs e)
    {
        if (homeButton.Checked)
        {
            tabControl1.SelectTab(0);
        }
    }

最后,在显示之前,使用以下命令对TabControl隐藏选项卡页

protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        tabControl1.Appearance = TabAppearance.FlatButtons;
        tabControl1.ItemSize = new Size(0, 1);
        tabControl1.SizeMode = TabSizeMode.Fixed;            
    }

瞧,你有了一个基本的UI和多个页面,你可以在其中放置控件。
运行时,隐藏选项卡页

相关问题