Click here to Skip to main content
15,905,614 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Imgur: The magic of the Internet[^]

How can i make it so when OK is pressed, it will open Form1

Form1 f1 = new Form1();
this.Hide();
f1.ShowDialog()

What I have tried:

I've tried looking for youtube videos but that didn't help me.
Posted
Updated 15-Dec-19 5:24am
Comments
Richard MacCutchan 15-Dec-19 12:13pm    
"I've tried looking for youtube videos but that didn't help me."
Don't waste your time on YouTube. Study the documentation on MSDN, it is far more useful.

You are creating new instances of Form1 in each of your button_click methods which is wrong. Create a Form1 variable at the class level, initialised to null, so there is only ever one instance. You can then create that anywhere if it is not already in existence.

And your code would be much easier to understand if you used proper names for your forms and buttons instead of the default Form1, Form2, Button1, Button2, Button3 etc.
 
Share this answer
 
Comments
Maciej Los 15-Dec-19 11:27am    
5ed!
Richard MacCutchan 15-Dec-19 12:11pm    
Thanks, but you solved it.
In addition to Richard's MacCutchan answer...
Replace this:
if(MessageBox.Show("Hello", "Inject", MessageBoxButtons.OK, MessageBoxIcon.None)==DialogResult.Yes)

with:
C#
if(MessageBox.Show("Hello", "Inject", MessageBoxButtons.OK, MessageBoxIcon.Information)==DialogResult.OK)
{
    f1 = new Form1();
    //further instructions
}

due to the fact that:
DialogResult.Yes returns 6
DialogResult.OK returns 1,
So, your if condition will never get true.

DialogResult Enum (System.Windows.Forms) | Microsoft Docs[^]
 
Share this answer
 
Comments
Richard MacCutchan 15-Dec-19 11:42am    
+5. I had a feeling there was something wrong with that if statement.
Maciej Los 15-Dec-19 16:47pm    
Thank you, Richard.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900