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:
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:
Hope someone can help me with this.
Best,