Form= new form();
Form.showDialog();
This.close();
The new form opens but the this.close doesn't seem to activate. So now the new form is open but the old form is still there.
Note I am trying to close and open a new instance of the same form.
Form.ShowDialog is a blocking call. This means that until it returns, the code after it won't run (and if that's on the UI thread, your application will appear unresponsive). From the MSDN page:
You can use this method to display a modal dialog box in your application. When this method is called, the code following it is not executed until after the dialog box is closed.
You probably want Form.Show instead, to get a window without blocking. You haven't described your full use case, but showing something else and closing yourself is a very strange thing to do, guessing there is a better way to solve your problem as well.
Related
I have been looking for some time on ways to open a second form from another already shown form.
This is some piece of code that works:
frmSecond second = new frmSecond();
this.Hide();
second.ShowDialog();
this.Close();
What it does basically is to Hide() the currently opened form, then it opens another form (the ShowDialog() method). It will only Close() the currently hidden form when the form you have just created is closed.
The problem here is: this way of doing it creates an immense thread of forms. If I need to go from frmSecond to frmThird, it will maintain the first form and the frmSecond being executed in the background, while only showing the frmThird.
Then, as the frmThird is open, if I need to get back to the first form, I would use some code like:
frmFirst first = new frmFirst();
this.Hide();
first.ShowDialog();
this.Close();
And it would create another frmFirst! Then we would have three forms being executed in the background (the first frmFirst, frmSecond, and frmThird).
This method works, but uses an increasingly amount of processing memory, which may be prejudicial for any kind of project.
Is there any alternative or add up to correct this problem?
If anything is unclear, please don't bother in letting me know.
Thank you.
If you want to get access to already created forms try using the static Application.OpenForms property. It contains a list of all of the forms currently open in your application. Documentation is here.
As an example, if you always want to keep frmFirst open and then navigate back to it when you close one of your other forms you can do this:
frmFirst existing = Application.OpenForms.OfType<frmFirst>().FirstOrDefault();
if (existing != null)
existing.Show();
You would need to remove your this.Close() calls for this to work.
To free the memory you need to dispose the form using the Dispose method when it is no longer needed.
Seems like you have a desing error here
Then, as the frmThird is open, if I need to get back to the first
form, I would use some code like:
frmFirst first = new frmFirst();
this.Hide();
first.ShowDialog();
this.Close();
This will leave the original created frmFirst in memory, not visible, doing nothing but eating memory.
If you know you want to get back to frmFirst that was created before, why not just do this :
frmFirst.Show();
and save you lots of memory.
You have 2 choices actually
Hide forms and reactivate them when needed
Close and dispose forms, and recreate them when needed
What you are doing is creating each form over and over again, without getting rid of the prior created forms. Hence you need lots of memory for hidden forms...
When Form2 is closed, via it's X button, the Main form is sometimes hidden as well, but not always. Often times the Main form is hidden after initial 'newForm' button click and other times many open-close operations are required before the Main form gets hidden on Form2's closing. Why is this happening? Why is it irregular?
This is a small test code for a larger application I'm working on. In that application a thread continuously reads the network stream and when a particular message is encountered a modal form is displayed. The user can close that modal form or it can be told to close itself by a different network message. In this event, to give the user some time to view the data that the form is displaying I implemented a delayed form closing for that form. When the form is running its delay closing code, another message can come in over the network that will open up a new instance of this form in which case, I observed, that once the timer of the original form runs out, the original form is left displayed until the new instance is closed. Calling Hide in the FormClosing event handler closes the original form if more than one instances of it are running, but it has this side effect of hiding the entire application (the Main form) when the last instance of this form is closed, either by the user or by the delayed closing code. And again, the entire application is not always hidden, but it does happen.
//Main form's 'newForm' button
private void btn_newForm_Click(object sender, EventArgs e)
{
Form2 f = new Form2();
f.ShowDialog();
}
public partial class Form2 : Form
{
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
Hide();
}
}
Update (from the application I'm working on):
The problem is shown visually below. The top part of the picture, labeled "A", represents the situation where the first modal dialog (greyed out) was instantiated and it is in the process of being auto closed after 4 seconds have elapsed. The second instance (blue window heading) is active and awaiting input. In the lower part of the picture, labeled "B", the counter to closing of the first instance has completed, yet the first instance remains visible. Adding Hide does not change picture "A" but picture "B" would only be showing the active modal dialog, which is what I want. If Hide is not used and we have the case shown in "B", once the active modal dialog is closed the inactive dialog will disappear together with the active one, but no sooner. At this time my main form will be hidden as well, sometimes.
Your main form doesn't get hidden, it disappears behind another window. The flaw in your code is that for a brief moment none of your windows can get the focus. Your main window can't get the focus, it got disabled by your dialog and won't get re-enabled until the dialog is fully closed. Your dialog can't get the focus, you hide it. So Windows goes looking for another window to give the focus to and can only pick a window owned by another application. Like Visual Studio, nice and big so your main window is well covered by it.
Not sure what you are trying to do, it doesn't make sense to call Hide() since the dialog will close a couple of microseconds later. Just delete the statement.
I am not sure if I am right but maybe you forgot to add e.Cancel = true; to your closing event.
Second, I think using a modal form is only usefull when you expect an action like OK or CANCEL from user, that is where DialogResults comes handy. It sounds strange if this happens time to time not all the time! maybe you can try like this:
//Main form's 'newForm' button
//Define form in your mainform
private Form2 f;
private void btn_newForm_Click(object sender, EventArgs e)
{
if(f != null) { f.Show(); return; }
f = new Form2()
f.FormClosing += delegate { f.Hide(); };
f.Show();
}
I know the topic is quite old, but I recently had to look for answers for this precise question.
Why hiding the (child modal) form instead of closing it ?
I may be wrong, but I think that in some cases, hidding the modal child form instead of closing it is sometimes useful.
For example, I'm using a class that is a custom tree of grids. Think of something like an Excel Document with multiples tables (sheets) and each table can have child tables. A very powerful manner to store datas that can be used by multiple objects and multiple forms at a time.
Now, this "TreeTable_Class" object has an inbuilt custom form that actually shows the content of one of its tables at a time in a GridView, and you can select which table to show by selecting it in a Treeview. You can see here that the "Database Editor" is actually and MDI Form that can load the Form from any TreeTable_Class.
And this is the Form I use to edit the content of a Cell for a given (selected) Table (I've chosen another cell with long text content from another table in this database)
Now, when you choose to close the custom form instead of hiding it, that form will be unaccessible, you can't show it anymore, and you get an exception (no instance of the object) Somewhat, it isn't disposed yet (so the check If MyForm Is Nothing Then... is useless) I know I have to implement the GarbageCollector and dispose the Child Form manually, but it's outside the scope of this topic.
Anyway, my class could use a large amount of memory, of datas, and if I had to rebuilt ALL the contents each time I want to show a new instance of that form, that would be a large amount of workload in my application. That's why I have chosen to hide the form instead of closing it until the main application exits or when a specific CloseFormAndDispose() method is explicitly called, either by the program, or if I make this option available for the user via an user interface.
Workaround try :
This is the workaround I've found to override the "form replaced by another because none of the parent/child ones could be retrieved" :
Sorry, I'm in VB.. but you can use a tool to convert this to C#, or do it manually, it's pretty simple..
// This is the child, a Cell Editor that can edit the content of a Cell.
Protected WithEvents _CellEditor As CellEditor_Form = Nothing
This Editor form is a member of TreeTable_Form, a form that can actually show and edit the content of the whole Database File (a single file)
And this TreeTable_Form class contains a routine that handles CellEditor closing event
Public Partial Class TreeTable_Form
// Sorry. The "WithEvents" in C# is a litte bit complex to me... So, in VB :
Protected WithEvents _CellEditor As CellEditor_Form = Nothing
// ...
// CellEditor handling method (I used a Code converter...) :
// The original VB declaration is :
// Protected Sub RecallFormAfterCellEditorHidden() Handles _CellEditor.Closed
// You'll have to write specific Event handler for _CellEditor object declared above...
protected void RecallFocusAfterCellEditorHidden()
{
Application.DoEvents();
this.Focus();
}
End Class
This tiny protected void RecallFormAfterCellEditorHidden() method in your Class (if you are using a class that contains Forms) or in your Main From, assuming that your main form contains the child forms (dialogs) will try to force the focus on your Application or MainForm...
By the way, TreeTable_Form is actually a component of TreeTable_Class. The later is an object that can be used anywhere you want. In a Main Form Application, in another Class, in a dialog, anywhere... and could be passed by reference to share its contents between several items. And this TreeTable_Class contains a RecallFocusAfterTreeViewerHidden() method that handles the closing of that form. That means, the Form or Application that actually uses the class will get the focus each time you close the its child Form. I've made it that way to get an object that could be used anywhere
We still get problems !
However, this method will make your application flicker a bit each time you close your child dialog, and doesn't succeed at 100% ! Sometimes, my parent form still disappear from screen and gets struck behind another window. Alt+TAB wont helt either. But this happens less than without this method trick. I don't have any better answer at this time, still searching... I'll come back here if I find out how. I'm using this custom made application in my work to write memos during meetings for example, and produce PV (procès verbal - french sorry) in PDF or DOCx on the fly...
And I'm sorry, I'm in VB, not C#. Hope this helps a little, until we find a better workaround for this...
is it possible to open my second form, when the method/or function opening the 2nd form
is on another thread?
i have read other threads related to this..
but it seems i cant figure out how to use the invoke
here's how i open the 2nd form
when im calling this.. nothing just happens..(because its on the 2nd thread)
TimerMode f2 = new TimerMode();
f2.ShowDialog();
please help me. i newbie to multi -threading..
You need to execute on the main window thread
Try the following:
this.Invoke((MethodInvoker)delegate{
TimerMode f2 = new TimerMode();
f2.ShowDialog();
}
This will create it on the right thread.
It should be doing something. That is because ShowDialog will run its own message loop. The TimerMode form should at least be visible and functioning. But, you are right, this really is not the best practice especially if this form will be interacting with the other forms which are already running on the main UI thread.
Here is how you might do it.
anotherForm.Invoke(
(MethodInvoker)(() =>
{
new TimerMode().ShowDialog();
}));
Note that anotherForm is a reference to one of your other forms which is already hosted on the main UI thread.
I have a form which instances a new form that I've already created using
statuswindow statwin = new statuswindow();
statwin.ShowDialog();
return statwin;
This code is in a function. The problem is when I run the function in other code the next line of code doesn't run until the newly instanced window is closed by the user, I'm guessing this is the side-effect of using ShowDialog(), is there a way to have it use the same behaver (being topmost, not being to select the other main window) while still letting the main form's code run?
Update: Although Show(); will work, I need to be able to make the window like a dialog by having it always on top of the other window and it always being the active selected window.
The ShowDialog method of showing a window shows it as a dialog, which is a blocking mechanism within C#/.net. If you want to simply display the window and not cause the running code to block until it is closed, you can use the window.Show() function.
In your code:
startwin.Show();
This will cause the startwin form to become visable to the user, and will fire the startwin.VisibleChanged event as well as the startwin.Load event to fire.
To make the new window always on top, you could set the Topmost window flag in the form properties.
To make the window run in a separate thread, first spawn the thread, then create the window from that thread.
There are also "application modal" and "system modal" Win32 window flags, but I don't know if those are exposed to WinForms or not -- go have a look on the form properties!
I have a requirement where the user wants to be able to click a button to show a dialog with some information. They want the ability to move the dialog off of the form and put focus back on the calling form and make changes to the calling form with the dialog still open.
It is basically a map on the main form and the dialog is a map legend.
Is this possible? How would I accomplish this task? It seems like I would need to do something with a panel like how Visual Studio does this with their dockable panels.
Call the Show method instead of ShowDialog.
This method is a non-blocking call (unlike ShowDialog, it will return immediately, not after the new form closes) and will not show the form modally.
You'll probably want to pass the parent form as the parameter so that it will show as a child form.
You can show the dialog in non-modal way.
Like this:
formLegend.Show();
Insead of calling legendForm.ShowDialog(), just use legendForm.Show(). It will display the legend form without restricting the map's usage.