How to access parent form items from child form? - c#

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

Related

Enable, Disable Panel in parent from child form

I am developing C# windows application. My DASHBOARD look like PICTURE-1.enter image description here
But, when I clicked sub menu sub menu shows like PICTURE-2. enter image description here
Sub menu must show like PICTURE-3. enter image description here
I have disabled panel at the time of loading of sub menu. I have tried to load DISABLED PANEL FROM SUBMENU, when submenu closed button is clicked. But the problem is other summery panel is not loaded in CONTAINER PANEL. It looks like PICTURE-4.enter image description here
I have done following things:
1. Set all panels modifiers to PUBLIC.
2. I have created a object in submenu form.
3. After that I tried to call all container and set the property of that panel as enable.
4. I have tried SHOW(), SHOWDIALOGUE(). But another form is loaded in taskbar.
My code is for calling SUBMENU from MAIN MENU (DASHBOARD) is
private void btnAdminDataBackup_Click(object sender, EventArgs e)
{
panelMainMenuRight.Visible = false;
panelTopSummery.Visible = false;
panelRightSummery.Visible = false;
submenuAdmin.Visible = false;
MenuDisplay(new Forms.frmDataBackup());
}
private void MenuDisplay(object MenuName)
{
Form fh = MenuName as Form;
fh.TopLevel = false;
fh.Dock = DockStyle.Fill;
this.panelContainer.Controls.Add(fh);
this.panelContainer.Tag = fh;
fh.Show();
}
Code is from SUBMENU:
private void btnClose_Click(object sender, EventArgs e)
{
Main_Menu mnu = new Main_Menu();
mnu.panelContainer.Visible = true;
mnu.panelMainMenuRight.Visible = true;
mnu.panelRightSummery.Visible = true;
mnu.panelTopSummery.Visible = true;
this.Close();
}
You are requested to help me to solve this issue.
Thank you.

Panels behavior : separation

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)

C# Using One Panel As a Placeholder For Multiple Forms

I apologize if this has been addressed already, but I could not find a case that fit my exact situation. So here goes...
I have a MainForm that contains a toolStrip1 docked to the left that functions as a vertical navigation bar. I have a panel (pnlMain) filling up the remainder of the form. I want to use pnlMain to display different forms which are made up of win form classes. Right now, I can click on the labels/buttons on toolStrip1 to display different forms within pnlMain.
private void tsLblCustomers_Click(object sender, EventArgs e)
{
hidePanels();
CustomerReport cr = new CustomerReport();
cr.TopLevel = false;
cr.AutoScroll = true;
cr.BackColor = Color.White;
pnlMain.Controls.Add(cr);
cr.Show();
}
What I want to do now is display additional forms within pnlMain by clicking on a button on another form rather than a label/button on toolStrip1. Some of my forms are as follows: CustomerReport, AddCustomer, EmployeeReport, AddEmployee. The Report forms are linked to my tool strip buttons. The Add forms are linked to buttons on the Reports forms. I tried several things including the following:
1) On CustomerReport, I tried creating an instance of MainForm, then I'll create an instance of AddCustomer, and then add that instance to the panel on MainForm.
2) I also tried creating a method in MainForm to create the instance of AddCustomer, and then call that method from the Add button on CustomerReport. Even though the code was the same as the toolstrip buttons on MainForm, it did not work.
I tried different variations of hiding forms, showing forms, clearing the panel, setting Visible to true or false, and I can't get it to work right. In some cases, I've managed to hide the CustomerReport, but AddCustomer will not come up. At some point I think I created a NEW instance of MainForm and my code wasn't impacting the original form that is already open. I'm just lost. Should I be using a different design? Originally I set up my application to just hide one form then show the other but I read that that is a 'terrible design'.
This sounds very similar to this thread here: Creating Form Inside the Form
You'd want to look into MDI.
Although it sounds like you're aiming for one cohesive interactive window. Otherwise, if you just want separate windows to popup, you can create properties within that other form and read them after returning a DialogResult. I'm not sure why this would be bad design without knowing more about the context of the program.
//Optionally do a hide(); here.
AddCustomer customer = new AddCustomer();
DialogResult result = customer.ShowDialog();
if(result == DialogResult.OK)
{
var name = customer.Name;
//More properties or whatever here.
}
//The properties would still be accessible here, too.
I ended up keeping the toolstrip nav bar on the left side of the primary window, and I created a panel in the main part of the window. All forms are displayed in the panel. Each time one of the label options in the nav bar is clicked on, the current form is cleared off the panel and the active form is displayed.
private void tsLblCustomers_Click(object sender, EventArgs e)
{
pnlMain.Controls.Clear();
CustomerReport cr = new CustomerReport();
cr.TopLevel = false;
cr.AutoScroll = true;
cr.BackColor = Color.White;
pnlMain.Controls.Add(cr);
cr.Show();
}
private void tsLblEmployees_Click(object sender, EventArgs e)
{
pnlMain.Controls.Clear();
EmployeeReport emp = new EmployeeReport();
emp.TopLevel = false;
emp.AutoScroll = true;
emp.BackColor = Color.White;
pnlMain.Controls.Add(emp);
emp.Show();
}
private void tsLblVendors_Click(object sender, EventArgs e)
{
pnlMain.Controls.Clear();
VendorReport vend = new VendorReport();
vend.TopLevel = false;
vend.AutoScroll = true;
vend.BackColor = Color.White;
pnlMain.Controls.Add(vend);
vend.Show();
}
private void MainForm_Load(object sender, EventArgs e)
{
WelcomeForm welcome = new WelcomeForm();
welcome.TopLevel = false;
welcome.AutoScroll = true;
welcome.BackColor = Color.White;
pnlMain.Controls.Add(welcome);
welcome.Show();
}

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 expand the picturebox into another form?

Currently, my application displays 6 picture boxes, each displaying an picture which is being constantly updating.
Now, I want upon clicking any picture box that picture box extends and fill up the whole screen just showing that chosen picture box.
Is this possible? Must i create another form to do this?
Thanks In Advance,
Perumal
in the onclick event for each the picture box (they can all point to this same method)
picturebox_Click(object sender .....)
{
PictureBox pb= (PictureBox)sender;
if (pb.dock==DockStyle.None)
{
pb.dock=DockStyle.Fill;
pb.BringToFront();
}
else
pb.dock=DockStyle.None;
}
Not seeing any code, here is how you can programmatically change a picture box on click.
pictureBox1.Dock = DockStyle.Fill
So you need to create a on click event handler and call your picture box's Dock function like the above.
update in response to comments
There is a DockStyle.None to revert the picture back to original size.
If i understand you correctly, you want to have 6 pictures and then when you click one it fills, click again, shrinks, click another one, fills etc etc...
To do this, you would use the Dock and Visible properties on the picture boxes. Now it also seems as if you are asking how to actually write the code. Well if you show some code, I could give pointers, with nothing to go on the way I'd approach it is to:
Put all your picture boxes in a list and assign a state to them Big or Small.
Write a OnClick for each picture box to change the state of the picture box clicked on.
Each OnClick then calls a helper function that iterates through each picture box in the list and hides the small one and DockStyle.Fill the big one.
Does the above algorithm accomplish what you need?
try something like this. the code is not re factored but I am sure you can do that
private bool isfill = false;
private void pictureBox1_Click(object sender, EventArgs e)
{
if (!isfill)
{
pictureBox1.Dock = DockStyle.Fill;
pictureBox2.Visible = false;
isfill = true;
}
else
{
pictureBox1.Dock = DockStyle.None;
pictureBox2.Visible = true;
isfill = false;
}
}
private void pictureBox2_Click(object sender, EventArgs e)
{
if (!isfill)
{
pictureBox2.Dock = DockStyle.Fill;
isfill = true;
pictureBox1.Visible = false;
}
else
{
pictureBox2.Dock = DockStyle.None;
isfill = false;
pictureBox1.Visible = true;
}

Categories