Panels behavior : separation - c#

I have 3 panels on top of each others and 3 buttons. What I want to achieve is with every button click its correspondent panel appears, currently I am using panel.Visible = true; and panel.Visible = false; but since every element over a panel in a WFA is considered a child of that panel all I get is either they are all visible or they are all invisible.
Q: How to make each panel behave separately?
This is the visibility control code:
private void btnHome_Click(object sender, EventArgs e)
{
panelHome.Visible = true;
panelContact.Visible = false;
panelOther.Visible = false;
}
private void btnContact_Click(object sender, EventArgs e)
{
panelHome.Visible = false;
panelContact.Visible = true;
panelOther.Visible = false;
}
private void btnOther_Click(object sender, EventArgs e)
{
panelHome.Visible = false;
panelContact.Visible = false;
panelOther.Visible = true;
}

This issue is easly solved using the graphical user interface:
You just need to carefully place every panel on top of the previous one until the blue guidelines appear.
PS: You need to check 2 guidelines: one vertical (left or right) and one horizontal (top or bottom)

Related

Overlapping of controls

I'm fairly new at coding so I need your help!!
I have controls overlapping. I have two richtextBoxes, one pictureBox and two buttons where richtextBoxes have the same location and size. The pictureBox overlaps on the left half of richtextBox. All but buttons are hidden when the form initializes. Here's how the code flows:
`
private void Button1_Click(Object sender, EventArgs e)
{
richtextBox2.Visible = false;
richtextBox1.Visible = true;
pictureBox.Visible = true;
}
private void Button2_Click(Object sender, EventArgs e)
{
richtextBox1.Visible = false;
richtextBox2.Visible = true;
pictureBox1.Visible = true;
}
Button1_click works fine but when I hit Button2_click my richtextBox2 overlaps on the pictureBox1 whereas I want the pictureBox1 to always overlap all richtextBoxes.
Go to the [Design] view of your form, right click pictureBox1 and select "Bring to Front".
Hope that helps :)

Switch between panels

I have 3 panels in 1 form to go through a process to input certain data. When the next button in a panel is clicked the next panel should be displayed.
Initially I have enabled the visibility of first panel and disabled the visibility of other panels.
When the next button is clicked the below code will be executed.
panel1.Visible = false;
panel2.Visible = true;
For the purpose of development I have put them side by side(not one upon another) and it woks perfect.
But when I put them one upon another the above code does not appear to be wok, which means when the next button is clicked it just shows a empty form.
Then I added below code too.
panel1.SendToBack();
panel2.BringToFront();
But it didn't work. Can someone help me with this.
Thank you.
This always goes wrong in the designer, the bottom panel will become the Parent of the top one. So if you hide the bottom one you can never see see the top one.
This can be worked around with View > (Other Windows) > Document Outline, drag the top panel back to the form. Still pretty painful, you typically have to edit the Location by hand and making any changes to the form in the designer later tends to slurp the panel back.
There are better ways to do this. Creating UserControls instead is highly recommended, they have their own design surface. Or use the RAD way and do this with a TabControl instead. All you have to do is hide the tabs at runtime, the subject of this Q+A.
You have to be careful when putting container controls like a Panel 'one upon another '.
In the designer you can do it, but only by carfully moving the panel with the keyboard. Using the mouse will always put the moved one into not upon the other one whenever its top left corner gets inside the other one.
As an alternative you can do the moving in code.
Doing it in code has the advantage of still being able to work with the lower panels and its content. Sometimes I stuff them into the tabpages of an (at runtime invisible) dummy tab and move it in or out of the pages to hide and show them..
Here is the code you can use to have multiple panels at the same time and switch between them with the next buttom added to the form.
public Form1()
{
InitializeComponent();
panel1.Visible = true;
panel3.Visible = false;
panel2.Visible = false;
}
private void btnNext_Click(object sender, EventArgs e)
{
if (panel1.Visible)
{
panel1.Visible = false;
panel2.Visible = true;
panel3.Visible = false;
}
else if (panel2.Visible)
{
panel1.Visible = false;
panel2.Visible = false;
panel3.Visible = true;
}
else if (panel3.Visible)
{
panel1.Visible = true;
panel2.Visible = false;
panel3.Visible = false;
}
}
Of course if you tagged your panels in ascending/descending format no gap between the tags for example 1,2,3,4 or 5,4,3,2 not 1,2,4 you could use this code
public Form1()
{
InitializeComponent();
panel1.Visible = true;
panel2.Visible = false;
panel3.Visible = false;
}
private void btnNext_Click(object sender, EventArgs e)
{
TogglePanels();
}
public void TogglePanels()
{
List<Panel> allPanelsInForm = new List<Panel>();
foreach (var control in Controls)
{
if (control is Panel)
allPanelsInForm.Add(control as Panel);
}
Panel visiblePanel = allPanelsInForm.Where(o => o.Visible).FirstOrDefault();
int nextPanelId = Convert.ToInt32(visiblePanel.Tag) + 1;
bool nextPanelExists = allPanelsInForm.Exists(o => Convert.ToInt32(o.Tag) == nextPanelId);
nextPanelId = nextPanelExists ? nextPanelId : 1;
foreach (Panel panel in allPanelsInForm)
{
panel.Visible = Convert.ToInt32(panel.Tag) == nextPanelId ? true : false;
}
}
I wish it could help you.

How to access parent form items from child form?

Hello Friends Please help me, I'm new to C# Programming. Kindly help me, I'm unable to integrate my project due to below problem.
I have created a MainScreen form, in that I took two panels. First Panel contains project name and a menustrip. In Second panel I'm loading different panels depending on what user click in menustrip. menustrip contain different elements like Home, Update Profile, Search, Book and Logout. By default I'm loading Home form in MainScreen 2nd panel. It kindoff looks like webpage. After logging successfully I want to clear the 2nd panel and want to load Home form/Search form. But when I try to do it, it shows "U cant access panel2 in this context". Please help me, I'm tired of searching solution for it. If this way is not possible, provide me some alternate way. Thanks in Advance!
I used below code...I made mdi parent true too.
private void homeToolStripMenuItem_Click(object sender, EventArgs e)
{
Home ob1 = new Home();
ob1.TopLevel = false;
ob1.FormBorderStyle = FormBorderStyle.None;
pnlBody.Controls.Clear();
pnlBody.Controls.Add(ob1);
ob1.Show();
}
private void MainScreen_Load(object sender, EventArgs e)
{
MainMenuStrip.Items[5].Visible = false;
Home ob1 = new Home();
ob1.TopLevel = false;
ob1.FormBorderStyle = FormBorderStyle.None;
pnlBody.Controls.Clear();
pnlBody.Controls.Add(ob1);
ob1.Show();
}
You can simply do this by assigning that control Modifier to public. but, it is not a good way to do this. Don't do this. If you want to execute any specific code from the outside the form than you can create a separate method and then create delegate for that method. you can invoke that method by using delegate.Invoke. I have already suggested that in my previous answer.
Let's imagine you have these controls which are from your main page:
button1
textbox1
label1
Now when you click a menu option you need to hide some or all of the controls above and then show these ones:
button2
textbox2
picturebox1
label2
If you want this, then you can just make this in the click event without using panels:
private void homeToolStripMenuItem_Click(object sender, EventArgs e)
{
button1.Visible = false;
textbox1.Visible = false;
label1.Visible = false;
button2.Visible = true;
textbox2.Visible = true;
picturebox1.Visible = true;
label2.Visible = true;
button1.Location = new Point(X, Y);
//Other controls locations...
}
Where the new Point is a class constructor that allows you to change the position of a control in the form (X and Y are pixel coordinates)
And... I guess that's all ~ :3
Ohhh and you could use a public int to count the page number... So if you have for example 3 pages, when the user click, I don't know "Page 2" your variable X which is public will have the value of 2, so in your event you can compare page combinations:
private void homeToolStripMenuItem_Click(object sender, EventArgs e)
{
if(x==1)//You know you are un page 1, you hide all the page 1 controls
{
button1.Visible = false;
textbox1.Visible = false;
label1.Visible = false;
}
else if(x==2)
{
//Hide you page 2 control, etc.
}
//After hidding your controls, next you have to show this page controls and adjust them to the form which are this ones:
button2.Visible = true;
textbox2.Visible = true;
picturebox1.Visible = true;
label2.Visible = true;
button1.Location = new Point(X, Y);
//Other controls locations...
//Finally, set X the value of the page number so you can copy and paste te comparation os X above in your events of every page:
X = pagenumber;
}

Toggle Group Box Visibility

I have two Group Boxes grpMeter and grpTag. I have to place grpMeter over grpTag.. both need same location and size..
On button click, I have to make them visible alternately. Is it possible? I tried many times but only 1 group box becomes visible. Maybe because of the overlapping problem. I tried with panel, but the same problem arises. Is there any solution?
public void ShowMeter()
{
grpMeter.Visible = true;
grpTags.Visible = false;
}
public void ShowTag()
{
grpTags.Visible = true;
grpMeter.Visible = false;
}
Place both group boxes next to each other so that they don't overlap and see if it works then. If you made it work, don't move the one group box with the mouse, but select it only and then set the coordinates manually in the Properties list.
That way you can prevent the one group box from accidentially becoming the child of the other group box.
Try this logic inside a button_click event:
private void btn_Click(object sender, EventArgs e)
{
if (grpTags.Visible)
ShowMeter();
else
ShowTag();
}
Try this:
private void button_Click(object sender, EventArgs e)
{
grpMeter.Visible = !grpMeter.Visible;
grpTags.Visible = !grpTags.Visible;
}
See the code below. The button Click will toggle visibilty. Also it's important that you set one of the groupboxes as visible and the other one as invisible in your constructor
using System;
using System.Windows.Forms;
namespace TestForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//This is important. Set one of them to be Visible and the other one to be invisible
grpMeter.Visible = false;
grpTags.Visible = true;
}
private void button1_Click(object sender, EventArgs e)
{
grpMeter.Visible = !grpMeter.Visible;
grpTags.Visible = !grpTags.Visible;
}
}
}
I am not sure but what you are looking for seems like FlowLayoutPanel. Then you can put group boxes next to each other and positioning will be handled automatically. This prevents accidentally putting one GroupBox into another or shifting locations. Also provides an easier working at design time.
One of the good way is to use RadioButton. Take two Radio buttons and place it inside a groupbox.
Something like this would work:
private void rdMeter_CheckedChanged(Object sender, EventArgs e)
{
grpMeter.Visible = rdMeter.Checked;
grpTag.Visible = !rdMeter.Checked;
}
private void rdTag_CheckedChanged(Object sender, EventArgs e)
{
grpTag.Visible = rdTag.Checked;
grpMeter.Visible = !rdTag.Checked;
}

Panel within another Panel

I have 2 panels. Each start at the same location.(Let's say 10,10) and have the same size.
I have 2 buttons. One shows the first panel and the other shows the second panel.
My code is:
private void button1_Click(object sender, EventArgs e)
{
panel1.Visible = true;
panel2.Visible = false;
}
private void button2_Click(object sender, EventArgs e)
{
panel1.Visible = false;
panel2.Visible = true;
}
When I press button one the the first panel shows up, but when click button 2 the second panel doesn't show up. The panel's visible properties are initially false..
What is wrong?
Make sure that Panel2 is not a child window of Panel1.

Categories