Enable, Disable Panel in parent from child form - c#

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.

Related

Enable/Disable Buttons On Owner Form When Owned Form Is Closed

I am working on a WinForms Desktop application in C# .NET. The target framework is .NET Framework 4.8. When the application is started, the main form is displayed with several buttons. Each button hides the main form when clicked and opens another form that displays data with which the user interacts. The main form is the owner of the new form.One button opens a form that presents a list of files from a network share folder in a data grid view. The user selects a row in the data grid and clicks a button to import the information in the file to various tables in a SQL Server database. When the import is complete, the selected row is removed from the data grid. When the user closes this form, there is code in the Form Closed event to show the owner. This all works well.My problem is that when the main form is unhidden, I need to disable the button that opens the form to list files to import if there are not any files in the network share folder to be imported. There is also a line of code to change the button's text property informing the user there are not any files to import.I realize I can place the code to disable the button and change button text in the VisibleChanged event. But, I only want the code to run after the owned form's closed event shows the owner form. How do I enclose the code in the main form's VisibleChanged event to disable the file import button only after the owned form is closed. Or, is it possible to edit the properties of the button on the owner form in the Form Closed event prior to the Owner.Show();I found a similar question WinForm Form Closed Event. But when I follow the suggestion
private void LoadChildForm_Click(object sender, EventArgs e)
{
ChildForm form = new ChildForm();
form.FormClosed += new FormClosedEventHandler(ChildFormClosed);
form.Show();
}
substituting my names
private void btnImportHFR_Click(object sender, EventArgs e)
{
Form form = new frmHFRFiles();
form.FormClosed += new FormClosedEventHandler(frmHFRFiles_FormClosed);
form.Show(this);
Hide();
}
Visual Studio flags frmHFRFiles_FormClosed as an error for the name does not exist in the current context.
ChildForm form = new ChildForm(this);
Then in ChildForm constructor:
MainForm m_MainForm;
Public ChildForm (MainForm mainForm)
{
m_MainForm = mainForm;
}
Then in closing event:
m_MainForm.button1.Enabled = false;
Ensure button1 is public
Here is what I did. I created a boolean variable in the main form and set the initial value to false.
private bool updateButtons = false;
The main form's constructor executes the search for files in the network folder.
public frmMainMenu()
{
InitializeComponent();
Shared.FilesForImport = GetHFRFiles();
}
The form's load event calls the EnableButtons() method
public void EnableButtons()
{
btnImportHFR.Enabled = Convert.ToBoolean(Shared.FilesForImport.Count);
btnImportHFR.Text = btnImportHFR.Enabled ? "Find Available HFR" : "No HFR For Import";
btnGetFacilityStatus.Enabled = Shared.sqlWrap.GetDataForFacStat(Shared.DsFacStat);
updateButtons = false;
}
The main form's visible changed event fires after the form load event. The network folder is not searched again because the updateButtons value is set to false.
private void frmMainMenu_VisibleChanged(object sender, EventArgs e)
{
if(updateButtons)
{
EnableButtons();
}
}
In the button click event, the updateButtons value is set to true after the main form is hidden.
private void btnImportHFR_Click(object sender, EventArgs e)
{
frmHFRFiles form = new frmHFRFiles();
form.Show(this);
Hide();
updateButtons = true;
}
The child form's closed event calls the Owner.Show() method
private void frmHFRFiles_FormClosed(object sender, FormClosedEventArgs e)
{
Owner.Show();
}
This causes the main form's visible changed event to fire. Only this time the EnableButtons() method will run because the updateButtons value is true.
private void frmMainMenu_VisibleChanged(object sender, EventArgs e)
{
if(updateButtons)
{
EnableButtons();
}
}
public void EnableButtons()
{
btnImportHFR.Enabled = Convert.ToBoolean(Shared.FilesForImport.Count);
btnImportHFR.Text = btnImportHFR.Enabled ? "Find Available HFR" : "No HFR For Import";
btnGetFacilityStatus.Enabled = Shared.sqlWrap.GetDataForFacStat(Shared.DsFacStat);
updateButtons = false;
}
Finally, the EnableButtons() method sets the updateButtons value to false.
It seems rudimentary to me, but it works. Thank you everyone for your feedback.
My problem is that when the main form is unhidden, I need to disable the button that opens the form to list files to import if there are not any files in the network share folder to be imported. There is also a line of code to change the button's text property informing the user there are not any files to import.
So your main form has a button X. If this button is clicked a method is called. This method will first hide the form, then show the subform until the subform is closed. The method should disable button X, change the button's text and unhide the form.
To make this flexible, we won't do everything in one procedure, we make several procedures with the intention of the procedure: "Inform operator there are no files to import" "Disable opening the subform", and of course their counterparts "Enable opening the subform", "Inform operator there are files to import"
TODO: invent proper method names
private void ShowNoFilesToImport()
{
this.buttonX.Text = ...;
}
private void DisableOpeningSubForm()
{
this.buttonX.Text.Enabled = false;
}
The advantage of this, is that if you later want to change the way that you want to inform the operator, for instance if you want to use an information field at the bottom of you screen, you will only have to change this in one place.
Furthermore, you can reuse the procedures, for instance you can add a menu item that will do the same as button X, this menu item can call the same methods
private void PerformActionsButtonX() // TODO: invent proper name
{
// Hide this form, and show the Subform until it is closed
this.Visible = false;
using (var dlg = new SubForm())
{
// if needed, set properties of the subForm:
dlg.Text = ...
dlg.Position = ...
// show the form until it is closed
var dlgResult = dlg.ShowDialog();
this.ProcessDlgResult(dlgResult, dlg);
}
// Show that there are no files to Import and disable OpeningSubform
this.ShowNoFilesToImport();
this.DisableOpeningSubform();
// Finally: show this form:
this.Visible = true;
}
And of course call this method when ButtonX or menu item Y are clicked:
private void OnButtonX_Clicked(object sender, ...)
{
this.PerformActionsButtonX();
}
private void OnMenyItemYClicked(object sender, ...)
{
this.PerformActionsButtonX();
}

C# - How to choose which Control User load first?

I'm using WinForm to do my little CRM.
I got MainWindow form and on this form 3 panels ( 1 Top/ 1Left/ the other part is filled up by 7 Control User )
All the Control User are on top of each other, and when i click on some button( attached to the left panel) the called CU is brought to the front.
public void BtnContact_Click(object sender, EventArgs e)
{
contactControl1.Visible = true;
contactControl1.BringToFront();
panelBar.Height = BtnContact.Height;
panelBar.Top = BtnContact.Top;
employe1.Visible = false;
comptaControl1.Visible = false;
histoControl1.Visible = false;
alerteControl1.Visible = false;
voyageursControl1.Visible = false;
parametresControl1.Visible = false;
}
I don't want all the CU to load at the start of the App, but i want them to be launch when i click on the button on the left. And let say if i opened one and now opening a new one it close the one who was opened.
If i have no choice to open everything ( which i doubt ) how can i choose the one i want to open first or second etc ??
Thank you
I don't want all the CU to load at the start of the App
Use properties tab and make visible=false or use formload event to prevent all cu loading at the start.
private void Form1_Load(object sender, System.EventArgs e)
{
//use code to make anything visible or invisible
}
you can put all controls that should be shown or hidden on the panel.
This is called the other part is filled up by 7 Control User in the question, so just use Panel to store all those controls.
Then you can introduce new field on your form to store current control index that is shown and to write button click that will check the index and activate next control
int currentIndex = -1;
public void BtnContact_Click(object sender, EventArgs e)
{
currentIndex++;
if (currentIndex >= panelFor7Control.Controls.Count)
currentIndex = 0;
foreach (Control c in panelFor7Control.Controls)
c.Visible = false;
Control control2show = panelFor7Control.Controls[currentIndex] ;
control2show.Visible = true;
}
Button BtnContact has to be pressed to activate next control on the panel.
PS I suppose that you are trying to load data for the visible control only. You can use Control.VisibleChanged to check is data loaded and to load if control is visible and data was not loaded.
First you need to make instance of your UC, something like this:
public partial class YourUC : UserControl
{
public static YourUC _instance;
public static YourUC Instance
{
get
{
if (_instance == null)
_instance = new YourUC();
return _instance;
}
}
}
Then you need to call and show UC when button is clicked
private void btn_Click(object sender, EventArgs e)
{
if (!pnlControls.Controls.Contains(YourUC.Instance))
{
pnlControls.Controls.Add(YourUC.Instance);
YourUC.Instance.Dock = DockStyle.Fill;
YourUC.Instance.BringToFront();
}
else
{
YourUC.Instance.BringToFront();
}
}
You it sounds like you want to have a "pages" navigation, I'm not use to work with WinForm, but perhaps this post could help you
What is the most efficient way to create a Win form with multiple pages?
It seems that you have to manage what Control you want to display manually.
Note that passing Controls from Visible to Hidden doesn't unload them

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

Using Button to show context menu [duplicate]

I have used toolstripdropdown in my Windows form to show list of buttons on click of another button.
var td = new ToolStripDropDown
{
AutoSize = true,
DropShadowEnabled = false,
BackColor = Color.Transparent,
Margin = Padding.Empty,
Padding = Padding.Empty
};
var host = new ToolStripControlHost(panel)
{
BackColor = Color.Transparent,
Margin = Padding.Empty,
Padding = Padding.Empty
};
td.Items.Add(host);
The panel contains list of buttons to be displayed. To show the panel to user, on button(Show) click following line is called.
td.Show(pointonScreen);
By default, AutoClose is set to true. So whenever user clicks anywhere in the form, the toolstripdropdown is getting closed. This is ok.
My requirements:
Click Show button
Display the toolstripdropdown by calling td.show() and close the popup if td.Visible
Again click the Show button
toolstripdrown should be closed
Click anywhere in the form, toolstripdropdown should be closed if it is visible
What is happening now is, on step 3, before the button click event is raised, toolstripdropdown is getting closed. So again the dropdown is being displayed.
Is there any other way to achieve my requirements?
You should handle Closing event of the dropdown and set a flag if the dropdown is closing by click on the button which opened it. Then when you click on button, check the flag and if there wasn't a flag, show dropdown and set the flag, otherwise close the dropdown and clear the flag:
ToolStripDropDown td;
private void Form1_Load(object sender, EventArgs e)
{
td = new ToolStripDropDown { /*...*/};
var host = new ToolStripControlHost(this.panel1){ /*...*/};
td.Items.Add(host);
td.Closing += td_Closing;
}
void td_Closing(object sender, ToolStripDropDownClosingEventArgs e)
{
if (e.CloseReason == ToolStripDropDownCloseReason.AppClicked)
if (this.button1.Bounds.Contains(this.PointToClient(MousePosition)))
{
td.Tag = true;
return;
}
td.Tag = null;
}
private void button1_Click(object sender, EventArgs e)
{
if (td.Tag == null)
{
td.Show(Cursor.Position);
td.Tag = true;
}
else
{
td.Close();
td.Tag = null;
}
}

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

Categories