Form disappears when resetting Form's TopLevel property - c#

In my application I am displaying a new form which needs to be at TopLevel.
So, I am setting
someForm.TopLevel = true;
Now, I have a checkbox, which will allow user to set it to "not a top level".
When unchecked, i want to set TopLevel = false
But when I do this, my form disappears. Does anyone know why?
Here is my code:
private void stayOnTop_CheckedChanged(object sender, EventArgs e)
{
this.TopLevel = this.stayOnTop.Checked;
}

Because your checkbox is named stayOnTop, I assume you want to set the TopMost property instead of TopLevel.

The setting TopLevel is only meaningful in MDI applications - ones where a parent form has one or more child forms inside it (like Word and Excel used to work).

TopLevelControl is the main form of your application. By setting TopLevel to false the TopLevelControl is set to null. In that case there is no main form to display for your application. If you add a timer that will switch it back to true you will see that it appears again. (It is interesting though that there is no preventing mechanism. For instance it's not possible to add a top level form to another top level form. But you are allowed to get rid of top levels forms entirely.)
So that's why it disappears. If you want it only sent to background you can use SendToBack() method. It will change Z-index of the form. So if there is a window behind your control, the control will be moved behind the window.

Try these codes :
private void stayOnTop_CheckedChanged(object sender, EventArgs e)
{
if(e.checked == true)
{
someForm.TopLevel = false;
}
else
{
someForm.TopLevel = true;
}
}

Related

Open ChildForm with owner permission

I am trying to insert a form inside another form. I used .Controls.Add(form) and it works. My problem is that I have to declare the owner and to do that I have to set the toplevel to true but if I set the toplevel to true I get stuck with the control.add bacause telling me that it is impossible to add a top level control to a control.
How can I do?
public void openChildForm(Form childForm)
{
if (activeForm != null) activeForm.Close();
activeForm = childForm;
childForm.TopLevel = true;
childForm.TopMost = true;
childForm.FormBorderStyle = FormBorderStyle.None;
childForm.Dock = DockStyle.Fill;
this.Controls.Add(childForm);
panelChildForm.Tag = childForm;
childForm.Owner = this;
childForm.BringToFront();
childForm.Show();
}
thankyou in advance,
Davide.
For me it's a bit strange what you are trying to do. A form is kind of window. Why would you add a window in another window? Don't you want to create a user control and to display that user control in your existing form?
There isn't enough information to go on from the original question you've posted, but it looks like you're trying to create the kind of functionality that a sidebar page menu might offer. In that situation, clicking on a button on the sidebar changes the window that's currently being displayed. Perhaps this question about sidebar navigation would relate to what you're saying? There's some information also available if you try searching "winforms sidebar navigation," such as this post about creating navigation in winforms, and there are a lot more linked from this question.
I'm just taking a guess at what you're trying to do here. Perhaps if you want to refine the answers you're getting, the original question should include a link to a github sample project, or at least a bit more of an explanation of what the target is.
It looks like you are working with WinForms.
Look at MDI Forms (Multiple-Document Interface Forms)
You can handle this by setting the parent form property IsMdiContainer = true
and then the child form childForm.MdiParent = parentForm;.
Here is a small example:
Add a menuStrip to the parentForm and set the parent form MainMenuStrip = menuStrip. Add a menu item and add some code to the menuStrip_ItemClicked event.
private void menuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
if (e.ClickedItem.Text == "New Child")
ShowNewForm(sender, e);
}
private void ShowNewForm(object sender, EventArgs e)
{
Form childForm = new Form();
childForm.MdiParent = this;
childForm.Text = "Window " + childFormNumber++;
childForm.Show();
}
You should end up with this behaviour.
If you want to access other forms within the same application you can use a loop and find a form using Application.OpenForms or find a form using either Application.OpenForms["form1"] or Application.OpenForms[0]
If you use MdiForms you can find child forms using parentMdiForm.MdiChildren

C# Topmost=true - restrict to application

Our Application is displaying an "update-hint", if a new version is available.
This Update-hint should be "topmost" within the application, but, if the application is minimized, or send to the background, the update-hint should vanish as well..
Just using
this.TopMost = true;
will overlay "ANY" application that is currently running...
Is there a way to just "overlay" windows generated by the current application?
Desired:
application shows update-hint on top of every window, while application is in the foreground. Switching to another application will also send the update-hint to the background.
Desired: Update-Hint overlays ANY window of the current application:
Not desired: Update-Hint overlays FOREIGN applications as well:
Despite the name of the property, TopMost is actually your enemy here. To make a "floating form" stay above a main form, without obscuring other applications when they get focus, try it this way:
FormX f = new FormX();
f.Show(this);
"this" in this example, is the main form instance. It means the form you created is now owned by the main form, and will make it float above it. You get the added benefit of, when minimizing the main form, the floating forms will disappear, too.
I figured out a way to solve it.
Setting the owner of the UpdateHint is required, but in order to keep it on top of every application window, one has to change the owner, if a new Window is either shown, or activated.
Within our application, every Form is inheriting InterceptorForm, so all I had to do was to modify the InterceptorForm accordingly:
Change the Owner to this, except if there is no dialog, or this is the dialog itself:
public class InterceptorForm : Form
{
protected override void OnLoad(EventArgs e)
{
...
if (this.GetType() != typeof(UpdateHint) && MainWindow.updateHint != null)
{
Log.t("Setting owner on update hint during onload: " + this.GetType());
MainWindow.updateHint.Owner = this;
}
base.OnLoad(e);
}
and
protected override void OnActivated(EventArgs e)
{
if (this.GetType() != typeof(UpdateHint) && MainWindow.updateHint != null)
{
Log.t("Setting owner on update hint: " + this.GetType());
MainWindow.updateHint.Owner = this;
}
base.OnActivated(e);
}
}
The UpdateHint now stays on top of every window belonging to our application, but can be overlayed by any other application.

How to create a windows form that replaces the current form?

I am making a game using Windows Forms, and I feel that the easiest way to make different screens (Main Menu, Settings, Etc...) is using a separate form for each screen. I have looked up up many different ways to do this. Many people talk of using form2.Show(); this.Hide();, however, this creates a new popup window.
People have also suggested using this.IsMdiContainer = true; to create a new window inside of the current window, but this is also not the functionality I want.
EDIT: Another question, multiple-guis-one-window, explains slightly that this can be achieved using UserControls, but does not elaborate with any examples, or what I should do.
I want to completely replace the current form's functionality with that of a new form.
Is there a way to achieve this? If not, is there a good alternative?
As I understood, you want to keep the same form and change the data inside that form (to not get the effect that your form was closed and reopened again).
The problem is windows form does not support such thing directly (at least not in an optimal way), I would use another UI frameworks for that. However, if you want to stick though with windows forms, you may use GroupBox or Panel tools (available from windows form design tools in Visual studio). Here you can group your elements as required, and show/hide the group/panel as required.
That is one of the best available solution for windows form AFAIK.
You want to open child forms within main form then you should try this i have create it without any User Control.
I have manage one parent form and two child forms. child forms should be open within parent form.
frmMain(Parent Form)
Set frmMain Property as Following
WindowState = System.Windows.Forms.FormWindowState.Maximized
I have take 3 panel in frmMain window.
pnlMenu (For Display Menu)
Set pnlMenu.Dock = System.Windows.Forms.DockStyle.Top property
Set height of this panel as you require.
pnlMain (For Display Child Forms)
Set pnlMain.Dock = System.Windows.Forms.DockStyle.Fill property
pnlFooter (For Footer Section)
Set pnlFooter.Dock = System.Windows.Forms.DockStyle.Bottom; property
Set height of this panel as you require.
I have set menubar in pnlMenu(Click on that menu for display child form in pnlMain)
frmMain.cs
public void BindFormIntoMainForm(Form Main)
{
Main.TopLevel = false;
Main.WindowState = FormWindowState.Maximized;
Main.AutoScroll = true;
pnlMain.Controls.Clear();
pnlMain.Controls.Add(Main);
pnlMain.Refresh();
Main.Show();
}
private void childToolStripMenuItem_Click(object sender, EventArgs e)
{
frmChildForm1 ChildForm1 = new frmChildForm1();
BindFormIntoMainForm(ChildForm1);
}
private void childForm2ToolStripMenuItem1_Click(object sender, EventArgs e)
{
frmChildForm2 ChildForm2 = new frmChildForm2();
BindFormIntoMainForm(ChildForm2);
}
BindFormIntoMainForm method responsible for display child form in main window.
frmChildForm1 & frmChildForm2(ChildForm)
Set both form Property as Following
FormBorderStyle = System.Windows.Forms.FormBorderStyle.None
Now when you click on Child Form 1 Menu then display following output:
when you click on Child Form 2 Menu then display following output:
I think it can be helpful for you.

Updating Form1's widgets by clicking Form2's button in Visual C# Windows Forms

I'm fairly new to Visual C# and I'm writing an GUI app with multiple forms. One form is main window, and the rest are some kind of option windows. When showing an option window, I need to load some data to it (for example a string to window's editbox), then edit it and return back to main window when closing option window. Is there any simple way I can achieve it?
I've found some solutions like, or c# event handling between two forms, but I can't really conform it to my needs. I was thinking about passing data in constructor, but how to get it back? I've found something about ShowDialog, but as I said I'm new to C# (started yesterday ^^) and don't know if I can use it.
Any ideas, please?
I found the following previous answer which outlines sending specific properties from the one form to another:
Send values from one form to another form
The using keyword will also ensure that the form is cleaned-up properly, here's a link to it's usage (pardon the pun...) : http://msdn.microsoft.com/en-us/library/vstudio/yh598w02.aspx
I've run into the same issue to be honest, and I have to say that prior to this discussion I would just pass the parent form itself to the child and alter it in that way. Such as:
ChildForm child = new ChildForm(this); //from the parent
and
public ChildForm(ParentForm parent)
{
this.parent = parent;
}
Probably not the best convention though, as you probably don't need to access that much from the parent as the child.
Thanks guys, I think I finally get it. Idle_Mind, your idea was the easiest in my point of view, so I decided to use it. If someone else has a problem like this, here's what I've coded:
In main window form: when button is clicked, a new form appears; after closing it, label1 shows the text typed in that form
private void Button1_Click(object sender, EventArgs e)
{
LoadDataForm loaddata = new LoadDataForm("initial value");
if (loaddata.ShowDialog() == DialogResult.OK)
{
label1.Text = loaddata.textBox1.Text;
}
}
In load data form: argument passed in form's constructor appears in textBox1; textBox1's Modifiers property has to be modified to "public"
public LoadDataForm(string initvalue)
{
InitializeComponent();
textBox1.Text = initvalue;
}
private void ApplyButton_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.OK;
}
Regards,
mopsiok

Statically located winforms in C#

I have three forms in C#, now when I want to show form2 I hide the main one and show the form and then when done working, hide the second form and show the main form again - I am doing this with the simple hide and show functions in winforms. Now the problem is every called form is placed on a different location on the screen, while I want all of them to stay on the same place. How to do it?
Try setting the owner of the form when you call .Show()
You can also set the start position before you call show with .StartPosition = FormStartPosition.CenterParent
Or set the form.Location property after you call show
See here and here for more details
You no doubt have a bug in your code, you are creating a new instance of the form instead of calling Show() again on the hidden form object. That's a bad kind of bug, it will make your program consume a lot of machine resources, ultimately it will crash when Windows refuses to allow your process to create more windows.
To make your scheme work, you have to write code that distinguishes between a closed form and a hidden one. Best way to do that is to explicitly keep track of its lifetime with the FormClosed event. Like this:
private Form2 form2Instance;
private void button1_Click(object sender, EventArgs e) {
if (form2Instance == null) {
// Doesn't exist yet, so create and show it
form2Instance = new Form2();
form2Instance.FormClosed += delegate { form2Instance = null; };
form2Instance.Show();
}
else {
// Already exists, unhide, restore and activate it
form2Instance.WindowState = FormWindowState.Normal;
form2Instance.Visible = true;
form2Instance.BringToFront();
}
}

Categories