There I have a button_click event and when it happens it opens a new Form. How do I lock the principal Form, so that it can't be looked at or changed until new Form is closed?
Here's my event handler:
private void button4_Click(object sender, EventArgs e)
{
Form2 f = new Form2();
f.Show();
}
Try showing the child form as a modal window:
Form2 f = new Form2();
f.ShowDialog();
Related
I'm practicing some C# and I decided to make an extremely simple login screen. I would like the login screen to close after the password is entered successfully, but I cannot seem to do it.
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "pwhere")
{
Form2 form2 = new Form2();
Form1 form1 = new Form1();
{
form2.ShowDialog();
form1.Close();
}
}
}
Thanks! :)
This button is in Form1 if that causes any trouble btw.
Fixed it.
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "rbxgod")
{
Form2 form2 = new Form2();
form2.Show();
Hide();
}
}
This button is in Form1 if that causes any trouble btw.
Assuming Form1 is your startup form, you can hide it, then call ShowDialog() with Form2, and finally close the original Form allowing the application to exit gracefully:
this.Hide();
Form2 f2 = new Form2();
f2.ShowDialog();
this.Close();
Alternatively, you could wire up the FormClosed() event of Form2 and close the current form from there. This would allow you to use Show() instead of ShowDialog():
this.Hide();
Form2 f2 = new Form2();
f2.FormClosed += (s2, e2) => { this.Close(); };
f2.Show();
I have a form(f1) that needs to run some instructions before opening another one(f2), and when it ends must close. I set in program.cs Application.Run(new f1());and at the end of f1 istructions I wrote
f2 f = new f2();
f.ShowDialog();
this.Close();
but it doesn't close, just goes in background.
Everything should work the way you have it but hide f1 and display f2 this way:
this.Hide();
var frm2 = new f2();
frm2.Closed += (sender, args) => this.Close();
frm2.Show();
To give a more detailed answer:
public class Form1() : Form
{
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
var worker = new BackgroundWorker();
worker.DoWork += worker_DoWork;
worker.RunWorkerCompleted += worker_RunWorkerCompleted;
worker.RunWorkerAsync();
}
private void worker_DoWork(object sender, DoWorkEventArgs e) {
Thread.Sleep(5000);
// this is where you can load your data from file
}
private void worker_RunWorkerCompleted(
object sender, RunWorkerCompletedEventArgs e)
{
this.Hide();
var frm2 = new Form2();
frm2.Closed += (s, a) => this.Close();
frm2.Show();
}
}
So Form1 will start to load data using a background worker. When the data is loaded, the BackgroundWorker will fire the RunWorkerCompleted event. That event handler will hide Form1 instead of closing it, create Form2, add a handler for form2's Closed event so that closing Form2 will stop your application, and then shows Form2.
I have 3 Form. I want to show Form3 and close Form1, Form2 when click button in Form2. This is my code . when I run this code it can show Form3 but not close Form1.
Form1
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.ShowDialog();
//frm2.Show();
}
Form2
private void button1_Click(object sender, EventArgs e)
{
Form3 frm3 = new Form3();
Form1 frm1 = new Form1();
frm3.Show();
frm1.Hide(); // It not close Form1
this.Hide();
// frm1.Close();
// this.Close();
}
Problem : You are creating the newinstance of Form1 and then trying to close/hide it.
Solution: You need to get the Form1 instance which was already in Memory and then hide or close it.
Replace This:
Form1 frm1 = new Form1();
frm1.Hide(); // It not close Form1
With This:
Form1 form1 = (Form1) Application.OpenForms["Form1"];
Form3 frm3 = new Form3();
frm3.Show();
form1.Hide();
I am using Visual studio 2012. I created two forms, form1 with a button to open form2 and the form2 has a button "exit" which will take me back to form1.
this is my code from form 1:
private void btnRecords_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.ShowDialog();
this.Hide();
}
and from form 2:
private void btnExit_Click(object sender, EventArgs e)
{
this.Hide();
}
I know I can use frm2.Show(); this.Hide(); instead of frm2.showdialog();. But, I need my form 1's state to be unaltered. My form1 contains a login form, which only enables the buttons (such as the new form button) if the login is correct. So if I hide form1 and just show it again, the login resets;
On Form2 class add a property to store the reference to the parent form:
public Form ParentForm { get; set; }
Then on Form1, you can show Form2 this way, hiding Form1 at the same time:
private void btnRecords_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.ParentForm = this;
this.Hide();
frm2.ShowDialog();
}
When closing Form2, you can show Form1 again:
private void btnExit_Click(object sender, EventArgs e)
{
this.ParentForm.Show();
this.Close();
}
Or even better, close Form2 this way:
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
if (this.ParentForm != null)
this.ParentForm.Show();
}
This will also show Form1 back if we the user closes Form2 using the cross button in the title bar.
Why don't you handle the login in the form_load event of the main form. The form_load does not run every time the form regains focus. Is it appropriate to close the form on them if they do not login on load? In my case, I send an email with the username of windows user to Domain administrators and close the program. They have to reopen the program to give it another go.
private void frmMain_Load(object sender, EventArgs e)
{
//Check login
Form frmLogin = new Form();
frmLogin.ShowDialog();
if (frmLogin.LoginSucessful == true)
{
btnRecords.Enabled = true;
lblWarning.Visible = false;
}
else
{
btnRecords.Enabled = false;
lblWarning.Visible = true;
lblWarning.Text = "You must first Login";
}
//other setup code here
}
I am trying to open a secondary form immediately after the main form displays, but instead, the secondary form displays first and then the main form displays after the secondary one closes (The second form is acting like a splash screen). Here is an example:
private void Form1_Load(object sender, EventArgs e)
{
doSomething1(sender, e);
doSomething2(sender, e);
// The new form I want to open after the main form.
Form2 f2 = new Form2();
doSomething3(sender, e);
}
private void doSomething1(object sender, EventArgs e)
{
// Do something here...
}
private void doSomething2(object sender, EventArgs e)
{
// Do something here...
}
private void doSomething3(object sender, EventArgs e)
{
// Do something here...
}
Use the Form1.Shown event instead of the Load event:
private void Form1_Shown(Object sender, EventArgs e) {
doSomething1(sender, e);
doSomething2(sender, e);
// The new form I want to open after the main form.
Form2 f2 = new Form2();
doSomething3(sender, e);
}
Alternatively, depending on whether doSomething1 and doSomething2 are doing background processing that the user doesn't need to see: you could retain those two in the load handler, and merely move the last two statements into the Shown handler.
In your constructor initialized the Shown event handler first:
public Form1()
{
InitializeComponent();
this.Shown += new EventHandler(Form1_Shown);
}
Then put the code there:
void Form1_Shown(object sender, EventArgs e)
{
doSomething1(sender, e);
doSomething2(sender, e);
// The new form I want to open after the main form.
Form2 f2 = new Form2();
doSomething3(sender, e);
}
public Form1()
{
InitializeComponent();
Form2 f2 = new Form2();
f2.Show();