Reputation: 486
I've seen a few answers on this already but it doesn't seem like any of them work for some reason so figured I'd pass my code along here and see if somebody is noticing something that I'm missing. I have created my form to work kind of like a custom tabbed form where when the user presses a button from my side panel the appropriate panel is displayed in the main section. Here is a simplified version of my code:
// From the function that is fired on each button click
// Set active button
currentBtn = (IconButton)senderBtn;
currentBtn.BackColor = Color.FromArgb(52, 73, 94);
currentBtn.ForeColor = Color.FromArgb(142, 68, 173);
currentBtn.IconColor = Color.FromArgb(142, 68, 173);
currentBtn.Font = new Font(currentBtn.Font, FontStyle.Bold);
// Left panel to show active
leftBorderBtn.BackColor = Color.FromArgb(142, 68, 173);
leftBorderBtn.Location = new Point(0, currentBtn.Location.Y);
leftBorderBtn.Visible = true;
leftBorderBtn.BringToFront();
// Show panel
switch (currentBtn.Text)
{
case "Lobby":
HideForms();
lobbyPanel.Visible = true;
break;
case "User":
HideForms();
hostPanel.Visible = true;
break;
}
// This is my HideForms function (again a basic version)
private void HideForms()
{
lobbyPanel.Visible = false;
hostPanel.Visible = false;
}
From my understanding what should be happening here is: after pressing my button it should hide all the panels and then only set the panel I want to visible. However, my hostPanel works fine but the lobbyPanel doesn't for whatever reason. Any help on why this isn't working would be greatly appreciated. Thanks!
Upvotes: 0
Views: 120
Reputation: 486
I've found a better approach that works flawlessly and was actually rather easy to implement. Thanks to RJ Code Advance EN on YouTube I found this.
The solution was to separate the panels into new Forms and then create a method to invoke these forms:
// Track our current form
private Form currentForm = null;
// Our method
private void openForm(Form childForm)
{
// If form is not null close current form
if (currentForm != null)
{
currentForm.Close();
}
// Set current form to passed in form and set it up to show
currentForm = childForm;
childForm.TopLevel = false;
childForm.Dock = DockStyle.Fill;
// Grab the empty panel for displaying our form
childPanelForm.Controls.Add(childForm);
childPanelForm.Tag = childForm;
// Show our form
childForm.BringToFront();
childForm.Show();
}
In my case I created an panel in my main form called childPanelForm
that's sole purpose was to be used to display any child Forms as you can see above. Then, whenever you want to invoke your method you would do this (in my example on button click):
// Set active button
currentBtn = (IconButton)senderBtn;
currentBtn.BackColor = Color.FromArgb(52, 73, 94);
currentBtn.ForeColor = Color.FromArgb(142, 68, 173);
currentBtn.IconColor = Color.FromArgb(142, 68, 173);
currentBtn.Font = new Font(currentBtn.Font, FontStyle.Bold);
// Left panel to show active
leftBorderBtn.BackColor = Color.FromArgb(142, 68, 173);
leftBorderBtn.Location = new Point(0, currentBtn.Location.Y);
leftBorderBtn.Visible = true;
leftBorderBtn.BringToFront();
// Show panel
switch (currentBtn.Text)
{
case "Lobby":
openChildForm(new LobbyForm());
break;
case "User":
openChildForm(new HostForm());
break;
}
Upvotes: 1