0

I am developing a simple app user interface using C# winforms.

The issue is that the child form doesnt seem to display on the panel after I click a button.

I have based this on a tutorial I saw Online and here is the following code:

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());
        }
    }
}

For some reason I am not able to call/open the childForm on the panel I have selected on my app:

This is what the app looks like:

enter image description here

Basically this method should open the selected child form when i run the BtnHome_Click event:

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());
}

This is what one of the forms looks like for example:

enter image description here

Hope someone can help me with this.

Best,

  • 1
    I don't think you can display a form inside a control. I might be wrong. You seem to try to mimic the behavior of a tab control so give that a try first. – John Alexiou Mar 21 '23 at 14:30
  • Thanks for your response. I tried to base of this video: https://youtu.be/5AsJJl7Bhvc. Maybe it's a depreciated method? – Nicolantonio De Bari Mar 21 '23 at 14:33
  • I think adding a `TabControl` to the form and hiding the buttons using https://stackoverflow.com/a/10346520/380384 would be the way to go. – John Alexiou Mar 21 '23 at 14:43
  • Actually what you do should work. When it doesn't it might be hidden in something we don't see (presumably some property setting of the panel or the forms that interfer). You might see something when you debug OpenChildForm and have a look at the properties of childForm. Typical things would be Location, Size etc. if they are of and if they are find out why. A TabControl works but then you presumably concentrate all visual code on the one Main Form. Another typical solution would be to use UserControl(s) here. Would be more or less equal to doing it with forms. – Ralf Mar 21 '23 at 15:44

1 Answers1

1

I think you should use a TabControl and add multiple TabPage objects, one for each form you would have

fig1

Overall add a SplitControl to separate the contents from the menu, and make the menu a TableLayoutControl with each row containing a single RadioButton with the display style as a button and an image.

On each radio button click you bring forth the correct TabPage

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

and finally, before display, hide the tab pages from the TabControl using

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

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

And voila, you have a basic UI and multiple pages you can place controls in.

when run, the tab pages are hidden

fig3

John Alexiou
  • 28,472
  • 11
  • 77
  • 133
  • Wow okay. This is great! thanks for the amazing explanation. So is tab control the best way to manage multiple pages right? Does this method have any downsides instead of using multiple forms? – Nicolantonio De Bari Mar 21 '23 at 15:11
  • @NicolantonioDeBari - read more: https://www.youtube.com/watch?v=TiCUXiQTM4o and https://www.youtube.com/watch?v=w2A488eL6vQ – John Alexiou Mar 21 '23 at 15:48
  • There are some bugs with tab control. See https://stackoverflow.com/a/16929255/380384 – John Alexiou Mar 21 '23 at 17:30