How to insert form into a panel - c#

I am building a material management program using the MetroFramework.
I trying to insert form into a panel to change form when button is click.
But my program occur Exception it is ArgumentException:
Only top-level controls can have an owner.
I dont understand what the problem is.
I'm sorry I do not have enough English.
if (!MainForm.Instance.MainPanel.Controls.ContainsKey("InnerTest"))
{
//inner Test
InnerTest frm = new InnerTest();
frm.TopLevel = false;
frm.Dock = DockStyle.Fill;
MainForm.Instance.MainPanel.Controls.Add(frm);
frm.Show(); // The part that causes the exception
}
Here is my code.

You need to set the ShadowType to none

Related

Display form on top in current software but not in other applications

In my software, I am showing a form using form.Show(). This form should be on top, for that I am using -
form.TopMost = true;
When I open other applications while my software is still running, this inner form is shown on top of other applications too.
I need to use form.Show() because I want other parts of master form
accessible while inner form is shown. So I can't use
form.ShowDialog().
Not duplicate of How can I bring my application window to the front?
The only really robust technique was suggested in this answer, as follows.
form.WindowState = FormWindowState.Minimized;
form.Show();
form.WindowState = FormWindowState.Normal;
Answering my own question-
To achieve the above purpose, there is a need of creating an owned window by displaying it with the Show(owner) overload. Or by explicitly assigning its Owner property. No need to keep form.TopMost = true. Instead, It should be -
form.TopLevel = true; //Its true by default.
form.Show(this);

Devexpress RibbonForm inside SplitPanel

As the title says I have a RibbonForm inside a SplitPanel, the problem here is that while I'm being able to place the form inside the panel, the form controls behave strange, specially the TextEdit control. When you try to place the caret between two characters, you can't, instead the whole text is selected. This is what I have
Form2 frm2 = new Form2();
frm2.TopLevel = false;
frm2.Parent = this;
frm2.FormBorderStyle = FormBorderStyle.Sizable;
frm2.WindowState = FormWindowState.Normal;
splContainer.Panel2.Controls.Add(frm2);
frm2.Show();
frm2.BringToFront();
I've tried using MdiParent but to no avail, the behavior stays the same.
I also read that a possible solution is to use a Dock Panel and a User Control instead of a Ribbon Form, the problem here is that I already have 20+ forms developed and fully working so I was hoping to find another solution.
Thank you.
After an exhaustive search, I've found the solution.
The key was to add a DocumentManager, set the main form as its MdiParent, and select a type of View, the next thing was to add this code:
Form2 frm2 = new Form2();
frm2.MdiParent = this;
frm2.Show();

Form closing before new shows

Hello I'm making my first Windows Forms app in C# using Visual Studio and I have a little problem.
While I'm opening a new form and closing the previous one, when I run the app it looks like it's closing the previous form before it opens a new one.
It doesn't look good and I want to avoid it.
UserPanel MDIUserPanel = new UserPanel(Username);
MDIUserPanel.MdiParent = this.MdiParent;
MDIUserPanel.Show();
this.Close();
I don't know what is going wrong. I will be thankful for any help.
Wirth regards,
DarQScreaM
#Edit
This doesn't seem to be the case actually. Propably its that :
I have 3 forms MainForm, Login, LoggedUser.
MainForm is MDI container with FormBorderStyle set on Fixed Single
Login is child of MainForm with FormBorderStyle set on None
LoggedUser is child of MainForm with FormBorderStyle set on None
When application is runned Login form is created in MainForm. MainForm is never closed since its container.
But when i move from Login form to LoggedUser form and vice-versa its created with FormBorderStyle = Fixed Single (normal windows window) and after 0.5~second its changed into None.
Editing it into that didn't really help :
MDIUserPanel.FormBorderStyle = FormBorderStyle.None;
MDIUserPanel.Show();
#Edit2
This change fixed it for me. I don't know why setting it on Form properties didn't work properly. It looked like form was created as FormBorderStyle.FixedSingle and then it was changed into FormBorderStyle.None. If I made this manually in Load it worked but U had to fix the size of my window too. It doesn't seem to be good though. It should work from the beginning since Form properties in Designer are like that from the very beginning.
private void UserPanel_Load(object sender, EventArgs e)
{
this.FormBorderStyle = FormBorderStyle.None;
this.Size = new Size(649, 357);
}
You can use the form's Shown event to ensure that the new form have been showed before you close the old one.
UserPanel MDIUserPanel = new UserPanel();
MDIUserPanel.Shown += ((s, ee) =>
{
this.Close();
});
MDIUserPanel.Show();
If this form you are trying to close, is the main form that opens when you run your application, then that's the reason. Closing the main form will exit your application. Try instead to just hide this form instead of closing it. And to make sure you can exit your application ('cause you've hidden your main form), just override the closing event of your current form, and put an "Application.Exit()" in it.
Hope this helps you !
First Step:
this.Hide();
Second Step:
MDIUserPanel.Show();

How to hide a form from a panel in another class?

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.

keyboard shortcuts do not work when adding a form to a panel c#

I am writing an application where I have a form with a panel. I have noticed that when I add another form to the panel, that the added form's keyboard shortcuts stop working.
I am using the following code :
MainMenu m = new MainMenu();
m.TopLevel = false;
m.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
m.Dock = System.Windows.Forms.DockStyle.Fill;
pnl.Controls.Add(m);
m.Visible = true;
pnl.ResumeLayout();
Is there anyway to make the keyboard shortcuts work?
Regards
My first guess (and it's totally a guess) is that you need to pass the parent/owner when constructing the child object, rather than just assigning parent ... could you show us that part of your code?
Also, just glancing over your code, it seems strange to, for a MainMenu, set Dock to Fill....
Adding a form to a panel inside another form? That is not a supported scenario; frankly I'm surprised it doesn't throw an exception. A better way to do this would be to use UserControls.
I found out that it was due to focus issues.
I have since converted my forms to user controls and the problems have gone away.

Categories

Resources