Open a new winform by doubleclick and close the caller window - c#

I have a C# winform called Form1, and this winform has a list and a button.
I added a click() event to the button, and a doubleclick() event to the list.
Both events call to the same method: (in form1.designer.cs)
this.myList.DoubleClick += new System.EventHandler(this.myMethod);
this.myButton.Click += new System.EventHandler(this.myMethod);
In myMethod, I want to do the following operations:
open a new winform of kind Form2, and make it the active winform.
close the caller winform (of kind Form1), there is no need for this form anymore.
I did it like this: (in form1.cs)
private void myMethod(object sender, EventArgs e)
{
Form2 frm = new Form2();
this.dispose();
}
when myMethod is being called by list doubleclick event, when myMethod ends, there is a null pointer exception.
When it's being called by the button click event, it works properly.
I tried this.close() as well, and got the same behavior.
my questions:
How should I write myMethod properly so it will make the wanted operations for the button click event and also for the list doubleclick event?
What is the difference between the button and the list? why does it work properly for the button, but crashes for the list?
Thanks

You can hide the Form1 and show Form2. This will raise some issue like closing Form2
won't close Form1.
this.Hide();
var form2 = new Form2();
form2.ShowDialog();

Related

WinForms; detecting form closed from another form

Is it possible to detect a form closing from another form.
For example.
If I had a mainForm that opens subForm, can I detect within the mainForm that the subForm has closed and execute code?
I understand I could create an event handler within the subForm, but this is not really what I'm after because what I'm about to do after the subForm closes, is within the mainForm (changes to mainForm).
The FormClosed event is public, so you can create a handler from the main form.
//Inside main Form. Click button to open new form
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.FormClosed += F2_FormClosed;
f2.Show();
}
private void F2_FormClosed(object sender, FormClosedEventArgs e)
{
MessageBox.Show("Form was closed");
}
Take a look at the public FormClosedEvent. Since the modifier is public, you're able to do something like the following example:
SubForm subForm = new SubForm();
subForm.FormClosed += delegate
{
MessageBox.Show("subForm has closed");
};
subForm.ShowDialog();
The above example creates a new form (of type SubForm), adds a new event handler to display a message box telling the user that the form has closed, and finally uses the ShowDialog() method which will prevent the user accessing the main form until the sub form has been closed.
The usual case for this is a "Modal Dialog" (like Message Box and its Family).
Every form can be opened as Modal Dialog, by using ShowDialog() isntead of Show().
Otherwise the event way is the only way.

What Event To Use For Code Execution On Start and When ShowDialog is Closed

I have form (form1) and in that I have button when user clicks it it will open a another form (form2) using "form2.ShowDialog()".
My Problem is I am looking for a event in form1 which need to execute when the form1 is opened and when the form2 is closed.
I have tried Activated and several other events so far I am not able to find solution. There is also event called "GotFocus" is shown in MSDN but I am not able find it in visual studio so I think probably got depreciated.
If you're using ShowDialog(), then execution in Form1 will pause at that point. When you close Form2, execution continues with the next line.
Just place the code that executes when Form2 closes on the line immediately after ShowDialog().
Form2 form2 = new Form2();
form2.ShowDialog();
// place code here that should execute only after Form2 closes
If you decide to use Show(), then this won't work, but you can still subscribe to Form2's FormClosed event, which fires when Form2 is closed.
Form2 form2 = new Form2();
form2.FormClosed += (s, e) => { /* code that executes after Form2 closes */ };
form2.Show();

C# windows form application

i am trying to go from one form application to the next by clicking a button, how do i do it?
I assume by "go from one form application to the next" you mean you want to invoke another executable (another windows forms app, for example). If that assumption is correct, then you can wire up the OnClick method of the button to do something like this:
System.Diagnostics.Process.Start(#"C:\your_other_app.exe");
You can refer to this msdn link for more info about Process.Start and its different variations.
Try this:
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show() ;
}
and make sure that button1's click event is handled:
this.button1.Click += new System.EventHandler(this.button1_Click);
or
button1.Click += this.button1_Click;
either in the designer code, or in your Form.load handler, or your form constructor.
You have to make in buttonClick SecondForm's object and then object.ShowDialog() or Show, if you want your second form to be active and not to have right to move to first Form, then use ShowDialog method, other way use Show(). Like this :
private void Button_Click(object sender, EvantArgs e)
{
SecondForm form = new SecondForm();
form.ShowDialog(); //form.Show();
}

C# Windows Form disappears suddenly

I am using C# + VSTS2008 + .Net 2.0 to develop a Windows Forms application. In button1 event handler of Form1, I create Form2, then Form2 appears. Then when trigger event handler of button2 of Form2 (button2 is Form2's UI button element), after executing button2 event handler, Form2 will disappear. Here is part of my code which creates Form2. Any ideas what is wrong to cause Form2 disappear?
// button1 belongs to Form1
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2("www.google.com");
form2.ShowDialog();
}
Form2 is being opened as a dialog. Could the button being pressed also be defined as the CancelButton (The CancelButton property will be on form2). Doing that will automatically dismiss the dialog when the event handlers have completed.
Without more details it's impossible to say for sure, but if button2 in Form2 has a value assigned to the DialogResult property, this will cause the form to hide automatically when the button is clicked. Open Form2 in the designer, select the button and check in the property grid. If the DialogResult property is anything else than "None", this is expected behaviour.
From the MSDN documentation of the Button.DialogResult property:
If the DialogResult for this property
is set to anything other than None,
and if the parent form was displayed
through the ShowDialog method,
clicking the button closes the parent
form without your having to hook up
any events. The form's DialogResult
property is then set to the
DialogResult of the button when the
button is clicked.

opening forms in c#

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.

Categories