restart Function in visual C# - c#

I have a windows application I need to create a button that unload/close/restart the currunt form while its running and reload/reopen/restart the same form.
How ??
I tried the hide show thing but it keeps the old form running in the background

Application.Restart();
I found it ....

Application.Restart() will restart your entire application.
Hide() will only do what it says, hide the form. If you just want a fresh version of your form to reappear, you can just create a new one, Show() it, and Close() your current form.
public void Restart()
{
var window = new MyForm();
window.Show();
this.Close();
}
You'll have 2 forms open for a very short time, so if you have any data connections that need to be closed, do so before reopening the form. To the end-user, it will happen so fast that they won't know 2 forms were open.

Related

How to correctly close a c# form

I am currently working with a c# desktop application that is connected to a web service. In one of the forms, I want to close form when the back button is clicked. I have done it like this:
private void bBack_Click(object sender, EventArgs e)
{
//this.Hide();
this.Close();
frmMain fMain = new frmMain();
fMain.MdiParent = this.MdiParent;
fMain.Show();
}
When I use this.Hide(), it seems like the form I want to close still runs in the background, thus still contacting the web service.
When I use this.Close(), the form seems to close and no contact to the web service is made, but my desktop application suddenly goes full screen and frmMain is opened in a new window.
I have read the documentation displayed here, but I could not find anything useful.
Hide does just that, it hides it, its still there.
So yes, you would need to close it, however, if you close it before you make the next, theres a chance that the code exits there and isnt completing any follow on code.
Rearranging your code to
frmMain fMain = new frmMain();
fMain.MdiParent = this.MdiParent;
fMain.Show();
this.Close();
Should work

C# - Don't bring form to front

My apps use a timer that display a form after some seconds.
When I minimize my apps to do other stuff, the timer is still active (I'm ok with that) and the form bring to front over all my windows with focus on it (normal behavior).
I want that the new form open above my main apps but not above all my windows.
In the main form, I call the new form like this :
MRIS.EVENT_BOX form1 = new MRIS.EVENT_BOX();
form1.Owner = this;
form1.ShowDialog();
I manage to remove the focus problem by adding this in the EVENT_BOX :
protected override bool ShowWithoutActivation { get { return true; } }
I also check that TopMost is set to false in the new form.
But the new form is still show above all the others (without focus this time...).
I check some other questions but cannot manage to find something useful.
Some people talk about visible form ?
If you can help me ?
Thanks
You need to call form1.Show(); if you don't want to bring it to front
form1.Show();
//form1.BringToFront() You may need to call this if you want to being it to front
form1.ShowDialog() shows the form as dialog which means it will be shown on top of parent form. You can check more details on this here

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

disable all windows that are related with the current application

I have a main app that is able to call .net methods. My .net method starts a new thread that shows a modal form.
Now my .net method needs to disable all windows from the main app while the modal form is shown.
How can I do this?
I was thinking about the win32 method "GetWindow",, but then I would need the handle of the main-app form.
In my example you see the main app that calls the method with the modal dialog.
When I click on the main app while the threadingform-dlg is open, the threadingform-dlg should blink.
Use the ShowDialog() function of the form instead of Show(). It will automatically make the form modal.
I don't know if it disables all other windows or only the calling one, try.
You should not show a modal form from a separate thread (you really shouldn't even show a non-modal form from a thread). Instead, display the modal form from your application's main form. If this requires moving some of your code around, so be it.
foreach (Form openedForm in Application.OpenForms) {
if (openedForm.GetType() == FormToClose) {
openedForm.Hide();
}
}
You can get the all open windows of the application by accessing OpenForms property.
var forms = Application.OpenForms;

How to know if the Form App open or not c#

Does anyone know how can I know if the Windows Form Application (C#) is open or that the client closed it?
(In my App I have a Windows Form Application (Form1) that allow the user to open another Forms (Form2). I want to know if the Form2 is open or close.)
I need to know that because I run the Form2 from a thread, and I want to make the thread runnig until the user close Form2.
Many thanks!
You can check if a form of a given type is open in your application like this (using LINQ):
if (Application.OpenForms.OfType<Form2>().Count() > 0)
{
// there is an instance of Form2 loaded
}
You need to elaborate on your question a bit more. Are you talking about monitoring the application from another application? Or that one form needs to know if another one is open? Or a form needs to know when another form closes?
There are a couple of ways to monitor forms closing within the same application.
Calling ShowDialog() on your form instead of Show() will ensure that code following the ShowDialog() call doesn't get executed until the user closes the form.
The Form class has a Visible property, which returns true/false depending on whether the form is visible or not.
As for the application itself, there is an ApplicationExit event on the static Application class which gets called just before the application closes so you could listen to that event if, for example, you need to perform some clean-up on exit.
If you want to run only single instance of application, check this link.
There you will also see how to check whether a process is still active.
If you mean MDI application with its child forms:
private Dictionary<Type, Form> SingleInstanceForms = new Dictionary<Type, Form>();
public Form ActivateForm<T>() where T : Form, new()
{
Cursor.Current = Cursors.WaitCursor;
if (!this.SingleInstanceForms.ContainsKey(typeof(T)))
{
T newForm = new T();
//setup child
newForm.MdiParent = this;
newForm.WindowState = FormWindowState.Maximized;
//newForm.Icon = Icon;
newForm.FormClosed += new FormClosedEventHandler(delegate(object sender, FormClosedEventArgs e)
{
this.SingleInstanceForms.Remove(sender.GetType());
});
this.SingleInstanceForms.Add(typeof(T), newForm);
newForm.Show();
this.Refresh();
}
Form formToActivate = this.SingleInstanceForms[typeof(T)];
formToActivate.Activate();
Cursor.Current = Cursors.Default;
return formToActivate;
}
this will create the form child if hasn't been created yet and activate it if it has been created.
sample: ActivateForm<dlgChildOne>();

Categories

Resources