I have two forms in my code. When I click a button in Form1, it shows the second form (Form2). There is an ILPanel in Form2. The first time that I click the button, Form2 is shown without any problem, but if I close Form2 and then click the button on Form1 again, I get the following error message when Form2 is re-shown. Does anybody know why this is happening? Thank you.
The code is very simple but here it is again
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f = new Form2();
f.Show();
}
}
This looks like a bug. One workaround - of course - is to reuse the form. "Closing" the form would not unload it, but only hide it. Clicking on button1 would only create the form for the first time and Show() it otherwise. That way, the OpenGL context (which seems to cause the problem) is not re-created every time you click on button1.
You can file a bugreport at http://ilnumerics.net/mantis
Related
So, my project currently has two forms (Form1, Form2). When a user presses the login button Form1 is hidden and Form2 is shown. For some reason while running the program, if you press the X at the top right of Form2 it will close Form2 but keep Form1 running and hidden. How do I set it so that the X button will close both forms completely (so that you don't need to go back into Visual Studio to close it).
I assume your Form1 is the 'main' form, in other words, that's the form that's launched when you launch the application. So this would be your main thread, and Form2 is a form that's opened upon some event, and that form runs in a different thread.
So, when the Form2 is closed if you want the main program to exit as well (although this doesn't quite seem like a great design idea), then you'd have to use Application.Exit().
You can capture your form 'closing' event in your Form2 by adding the Form2_FormClosing event handler, and in it, you call the above method.
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
Application.Exit();
}
}
I'm trying to work with multiple forms, what I want is change the way the form is depending on the selected index of a combobox, the only way I could think of is hide form1 and show form2, but the problem is when you close form2, the process does not end...I tried the code below
private void Form2_FormClosing(object sender, FormClosedEventArgs e)
{
foreach (var process in Process.GetProcessesByName("Process Name.exe"))
{
process.Kill();
}
}
if there isn't, is there a way the form can change on combobox selected index?
Try Application.Exit();
It exits your entire application and closes all your forms and threads.
Simply pass an instance of Form1 to the constructor of Form2, keep a reference to it in a form1 member
public class Form2 : Form{
private Form _form1;
public Form2(Form form1):this()
{
_form1 = form1;
InitializeComponent();
}
}
later you can simply use that reference :
_form1.Close();
This is a cleaner way to do it.
Other mechanisms are also ok, like implementing an eventhandler on form1 for an event in form2.
based on your pastebin code change this:
Form2 HeadquarterForm = new Form2(this);
you also only need the closed eventhandler and call close on the _form1 only once. So you don't really need the closing event handler.
The process is still running because form1 is still alive but hidden.
Try using Environmental.exit() to kill the process
Looking at your code in pastebin. Problem is, you're not passing Form1 in constructor of your Form2 when creating it. Change the part of your switch-case (4) to:
Form2 HeadquarterForm = new Form2(this);
I have some problems with Form control focusing.
On form1 I click a button and run the code below:
private void btnTest_Click(object sender, System.EventArgs e)
{
form2 = new Form2();
Application.Idle += new EventHandler(Application_Idle);
form2.Show();
form2.Activate();
form2.textBox1.Focus();
Form3 form3 = new Form3();
form3.ShowDialog();
}
Then, after this CLR I run the event Application_Idle on which I add a method that must focus on the textBox2 control..
private void Application_Idle(object sender, EventArgs e)
{
form2.textBox2.Focus();
form2.textBox2.Select();
form2.textBox2.Focus();
Application.Idle -= new EventHandler(Application_Idle);
}
But when I click the button on form1, I see Form2 showing, Form3 showing and then Application_Idle method raise, but form2.textBox2 control doesn't get focused...
If I comment out the form3.ShowDialog(); line it's works fine, but how do I focus a form element with another form activation?(form3.ShowDialog()) ?
Remark added:
Problem in also is I have a strict architecture and all I can change is Application_Idle method.
The issue you are having is with modality:
Forms and dialog boxes are either modal or modeless. A modal form or dialog box must be closed or hidden before you can continue working with the rest of the application.
Dialog boxes that display important messages should always be modal. The About dialog box in Visual Studio is an example of a modal dialog box. MessageBox is a modal form you can use.
Modeless forms let you shift the focus between the form and another form without having to close the initial form. The user can continue to work elsewhere in any application while the form is displayed.
When you use ShowDialog, the form that is shown prevents the caller from returning control until the dialog box is closed. If this is not the desired effect, you can use the Show method.
You could focus the textfeld, when the form itself got the focus:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
this.GotFocus += (s, e) =>
{
this.textBox2.Focus();
};
}
}
As John Koerner stated, you cannot set focus to Form 2 while Form 3 is open because of modality.
Since you stated that a user input in Form 3 is necessary to proceed, you should change your approach. You can place a listener watch for Form 3's closing. Only then can you set the focus somewhere else
form3.FormClosed += Application_Idle
I have two forms form1 is main form and form two is model form I want to set the forms as below:
Form1
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show(this);
}
The above would set the form1 owner of form2 and form2 would be shown but the problem is that this will break the order of forms on press of Alt+Tab keys hence I have tried it with another way as below.
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.ShowDialog(this);
}
This would be works but the problem is that the dialogue forms will not allow me to maximise/minimise and close
My form2 is borderless form and it is set to show on specific location as to fit with main form1. My aim to do not shows the form2 in Alt+Tab list and as I close the form2 then form1 will show immediately without break order of form.
When I press Alt+Tab keys on first condition and try to close form2 then the other application shown instead of form1 which is I do not want.
Is there any solution of this problem?.
It really sounds like you could do the second form as a custom control.
See Microsoft's documentation and this set of examples.
Think of it as a standard control, like a Button, DataGridView, TextBox, or the like, except that you have total control over it. You can show or hide it, you don't have to worry about where it is positioned, it won't take focus away from the parent form, and so on. And you can put whatever other controls you want in it, encapsulate all their logic, etc.
A possible hack is to keep your parent form active after opening child form as a modal form, so that you could do maximize/minimize your parent as well. An extension method:
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern bool EnableWindow(IntPtr hWnd, bool enable);
public static DialogResult ShowDialogSpecial(this Form formToBeShown, Form parent)
{
parent.BeginInvoke(new Action(() => EnableWindow(parent.Handle, true)));
formToBeShown.ShowDialog(parent);
return formToBeShown.DialogResult;
}
You can call:
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
//additionally do f2.ShowInTaskbar = false to make sense.
f2.ShowDialogSpecial(this);
}
This wont let child form truly act as non-modal form, since child form can cover over parent form.
I had a button in first form .if i click the button the second form is opening ,if i click again same button in first form another second form is opening.i need to open only one second form only in c# .
Well you could do a :
Form.ShowDialog()
This will prevent the user clicking the button on the first form, as the second form will keep focus until it is closed.
Or you could do
Form2 form2 = null;
void button_click(object sender, EventArgs e)
{
if(form2 == null)
{
form2 = new Form2();
form2.Disposed += new EventHandler(f_Disposed);
form2.Show();
}
}
void f_Disposed(object sender, EventArgs e)
{
form2 = null;
}
Check to see if the form is already shown by storing a reference to it and using something like:
if(form2Instance.Visible==true)
....
If you provide some sample code, we'll have a more specific answer, but it sounds like you are instantiating a new form in your button click event. The first time you click the button, your second form (will/may) not exist, so create it and keep a reference to it local to your first form. Then the next time the button is clicked, show the form instead of recreating it.
I'd declare a private variable in the class with the button that contains a reference to the opened form.
If the button is clicked:
Check whether it is null,
If yes, create and show a new form, if no, don't do anything.
If it's about having exactly one form open, you might also check out form.ShowDialog(), which blocks the caller form until the new form is closed.