How to hide a form from a panel in another class? - c#

I want to hide a form in panel1(form content) using this code:
frmChangePassword frmChangePassword = new frmChangePassword();
frmChangePassword.Hide();
with no luck. Here is my code to display in a panel:
//SHOW CHANGEPASSWORD FORM
if (isChangePasswordActive == false)
{
isChangePasswordActive = true;
frmChangePassword frmChangePassword = new frmChangePassword();
frmChangePassword.TopLevel = false;
frmChangePassword.Parent = this.panel1;
frmChangePassword.Show();
}
Is there a way to clear the content in my panel and display a new form in the same panel?
Many thanks in advance.

You are creating a new instance of the Password form. You need to get the current instance and then hide it. There are two ways I can think of:
Retain the frmChangePassword object you have created to show the form and call hide method on that object. You may have to change the scope of that object.
Use Application.OpenForms collection to get the opened instance of password form and hide it.
I would go with first approach.

Well, the problem is that you're creating a new form and then hiding the panel on that form. You can tell because you used the new keyword:
frmChangePassword frmChangePassword = new frmChangePassword();
frmChangePassword.Hide(); // affects the ^^ NEW instance ^^ you just created!!
So the code actually works fine in that it does exactly what it's supposed to be doing, it just doesn't produce the result you intended, which was to hide the panel on the existing instance of the form.
That gets a little bit tricker, because you have to find a way to get a reference to the existing instance of the form. In order to tell you how to redesign your code to do that, I'd have to see more of the code. Generally, the method you're writing this code in (the one that needs to do the hiding) will take a parameter of type Form (or even frmChangePassword), and the caller will pass in the current instance of the form to be modified.

If you still need it.
You have to declare a global varible of type Form in your public class:
Form frm = new Form();
Every time you trigger a new form load be sure to put first: frm.Hide();
and then intitialize the variable with the new form that will show:
frm.Hide();
frm = new thenewForm() { Dock = DockStyle.Fill, TopLevel = false, TopMost = true };
this.yourPanel.Controls.Add(frm);
frm.Show();
The properties I put in the "{" might change depending on the style you want, but if you want to have it as a borderless window you can copy those.

Related

MenuStrip not updating when editing it from another form (C#)

I've been having an issue that's been bugging me for the past 3-4 hours.
Basically, I have a main form, that is an MDI parent. I have another form, that is an MDI child of that parent. I use the second form for logging in and after I've been logged in, I want to add new items to the menuStrip of that main form, but for some reason I can't. I tried debugging and I saw that the menuStrip's indexes are expanding correctly for the new items, but they are not being updated in the form. I tried displaying the main form again with this.Show. This worked! I got a new form displaying the new items in the menuStrip. However, I just want to update them and not create another form. I tried refreshing/updating, but nothing worked. :/ Any suggestions?
Thank you!
EDIT:
Here is some more info:
this is the code from the 2nd form
if (successLogin==DialogResult.OK || successLogin==DialogResult.Cancel)
{
Form1 main = new Form1(); //define main form
FullUser = textBox1.Text; //getting the username (will use it for the menuStrip items "Logged in as: "+ FullUser)
main.LogedIn(FullUser);//calls method
this.Close(); //closing the 2nd form
}
that calls for the public method in the main form
public void LogedIn(string user)
{
menuStrip1.Items.Add("Item here"); //adding the item to the menuStrip
}
Edit 2: I put a timer which starts when opening the 2nd form and checks for a public boolean. That got it working, but still not the way I wanted it to.
You are creating a new form of type Form1 in the line:
Form1 main = new Form1();
And adding the items to that new form, instead of your existing one.
If the form is the MDIParent of the second form as you state in your question, you could change that line to:
Form1 main = (Form1)this.MDIParent;
If it's not, then you need to keep a reference to the existing form. If you create a new one (like you are doing) then the items are added to that new one, but not to the one that is already created.

How to make changing "screens" in c#

I'm new in C#, and I wanted to know if there was any way to show a screen with certain elements, and then with the click of a button, switch to another screen, similar to an installer.
From my experience in Java, I would just use a few JPanels and then hide only the one i want visible.
However, I'm new to C# forms and it's very different from Java swing. Anyone understand my problem and can tell me pretty much how this works? Thanks.
Simple approach
Just use a Grid with multiple Grids inside of it. Set the Visibility property of each internal Grid (except the first one you want to show) to Hidden or Collapsed, and then set them to Visible when you want to display them.
Better approach
Create a class for each section, each of which derives from the same parent class. Create a DataTemplate for the parent class, then just have instances of the template load into the original Grid through a ContentPresenter.
You can try this creating new forms. From my experience I've tried this:
Form2 formTwo = new Form2(); // creates instance
formTwo.Show(); // displays the new form
this.WindowState = FormWindowState.Minimized; // minimizes previous form
this.ShowInTaskbar = false; // hides it from taskbar
Keep in mind that this does not close the previous form. I would recommend setting ShowIntaskbar as True if you don't mind the user seeing the form minimized.
EDIT: If you want to show new elements I suggest you can try adding a new Form class to the project then using the designer.

Switching between forms

Anytime I want to display a new form, I create a new form object and hide the current form.
For example:
this.Hide();
new Form2().Show();
In this way, a new object keeps getting created over and over, and the old form is still running but hidden somewhere.
I would like to know what would be the most appropriate way to do so, I've been doing this for long time and anytime I do this I feel like I am doing it wrong. I mean how can we access the previous object and set it back to show instead of creating a new one.
Use Application.OpenForms property to get already opened form object and show that.
The OpenForms property represents a read-only collection of forms
owned by the application. This collection can be searched by index
position or by the Name of the Form.
Form1 frm1 = Application.OpenForms["Form1"] as Form1 ;
if (frm1 != null)
{
frm1.Show();
}

Set form as Parent throw exception "Top-level control cannot be added to a control"

I want to access variables of a form from another form. On clicking a button inside my Main form, I want to set my Main form as Parent, then bring up another form (child form) wherein I will access variables of the Main form. My click handler is as follow:
private void btnSystem_Click(object sender, EventArgs e)
{
Form_EnterPassword EP = new Form_EnterPassword();
EP.Parent = this; //error: Top-level control cannot be added to a control
EP.ShowDialog();
}
It compiles fine without any error. However, when I run the Main form and click on the System button, it throws me an exception. I do something similar in another code (not mine) with the same button click, and encounter no error (just with setting Main form as Parent).
What am I doing wrong? Is there something in my Main code that cause this?
Best way would be to use EP.ShowDialog(this) and later use Owner property.
You need the EP.TopLevel property to be set to false. It will let you to set a parent to it.
Further reading.
In case you only want to access variables and controls of another form, then maybe you can reach it in other ways, not trough a Parent relationship.
OK,
apparently the way to do it is to call
Form_Child.ShowDialog(this)
and then I can call
FromParent_aVariable = ((Form_Parent)this.Owner).aVariable;
or if I define aVariable in the namespace Properties then
FromParent_aVariable = NameSpace.Properties.Settings.Default.aVariable;
there are two ways.
Form_EnterPassword EP = new Form_EnterPassword();
EP.MdiParent = this;
EP.Show();
try this way, it helps for me. you need to set principalform as isMdicontainer = true at the form properties
I had a similar situation recently.
I was attempting something similar but by controlling the Child Forms from a different class.
Note(s):
You're trying to set the Child Form(s) "TopMost" to something that does not allow it.
In this case the "MdiContainer".
To accomplish this:
• Disable MainForm "isMdiContainer" property (its use is kind of obsolete anyway).
• Set the Form(s) TopMost properties to true.
• You should now be able to accomplish your feature.
**Code Example:**
/* On your Main Form Class */
private void btnSystem_Click(object sender, EventArgs e)
{
// Instantiate the Form_EnterPassword by passing the MainForm
Form_EnterPassword EP = new Form_EnterPassword(this);
EP.Show(); // No longer as modal Form to display in front.
}
/* Under your EnterPassword Form Class */
// Do not create a new Instance of MyMainForm.
// You want to use the same thread as your MainForm
private MyMainForm mainForm;
/* Constructor */
public Form_EnterPassword(MyMainForm form)
{
mainForm = form;
this.Owner = mainForm; // "this" refers to the: EnterPassword Form.
}
Remarks:
The only additional thing that you (may) have to do, (to achieve perfection) is to check the MainForm > WindowState; and create a code block to minimize or bring the Forms to their specific state.
i.e:
if (WindowState == FormWindowState.Minimized)
{ /* Code to Minimize all the Child Forms. */ }
else { /* Code to bring all Forms to their "Normal" State */ }
Writing this way, made the dialog display on the center of the parent form.
Form_Child.StartPosition = FormStartPosition.CenterParent;
Form_Child.ShowDialog(this);

How can I create a new C# Window based on my existing Window?

I know this mite be a bit of a silly question but how do i create another window from my main window in c# windows application? I don't know where to look for this type of questions.
You can use the following to create a new form. Note that I have provided two examples.
// This example creates a new Form control. While this Form is open,
// you cannot gain focus of the parent form.
Form form = new Form();
form.ShowDialog();
// If you want to be able to use both Forms. Then this is what you want:
Form form = new Form();
form.Show();
Also, MSDN is your bestest friend: MSDN on Windows Forms.
...And Google.
What about:
YourForm newForm = new YourForm();
newForm.Show();
You have several methods of showing your form. I use YourForm as a name here, replace that with the classname of your own form.
Note that a form-class is nothing more than a regular class that can be instantiated like any other class using new and inherits all methods from it parent calls (Form in this case), which includes the methods Show and ShowDialog. You can create as many instances of your class (i.e., of your form) as you like.
I will assume you are using winforms and will walk you through a simple example:
In Solution Explorer, Right Click your project and select Add | New Item...
Select the type About Box and you will see a new AboutBox1.cs get generated.
Select View | Toolbox to get the Toolbox to appear.
On your main form, drag a Button from the ToolBox | Common Controls onto the form.
Double click the newly created button to create the Clicked Event.
the clicked event type the following code:
AboutBox1 aboutBox = new AboutBox1();
aboutBox.ShowDialog();
This code will declare a variable aboutBox of type AboutBox1 and then instantiate it ( construct it ). Then you call the ShowDialog() method to get it to appear.

Categories

Resources