Asked  7 Months ago    Answers:  5   Viewed   32 times

There is two Forms in my project : Form1 and Form2. There is a button in Form1, and what I want to do is closing Form1 and showing Form2 when that button clicked.

First, I tried

Form2 frm = new Form2();
frm.Show();
this.Close();

but as Form1 was closed, Form2 also got closed. Next, I tried

Form2 frm = new Form2();
frm.Show();
this.Hide();

but there is a disadvantage that the application does not exit when the Form2 is closed.So, I had to put in additional sources inĀ form_FormClosing section of Form2.

Hmm.... I wonder whether this is the right way....So, what is the proper way of handling this problem?

 Answers

98

The auto-generated code in Program.cs was written to terminate the application when the startup window is closed. You'll need to tweak it so it only terminates when there are no more windows left. Like this:

    [STAThread]
    static void Main() {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        var main = new Form1();
        main.FormClosed += new FormClosedEventHandler(FormClosed);
        main.Show();
        Application.Run();
    }

    static void FormClosed(object sender, FormClosedEventArgs e) {
        ((Form)sender).FormClosed -= FormClosed;
        if (Application.OpenForms.Count == 0) Application.ExitThread();
        else Application.OpenForms[0].FormClosed += FormClosed;
    }
Tuesday, June 1, 2021
 
innovation
answered 7 Months ago
40

By default, when you close a form, it will be disposed. You have to override the Closing event to prevent it, for example:

// Use this event handler for the FormClosing event.
private void MyForm_FormClosing(object sender, FormClosingEventArgs e)
{
  this.Hide();
  e.Cancel = true; // this cancels the close event.
}
Wednesday, August 4, 2021
 
julesj
answered 5 Months ago
78

Set the 'default' value to "Name". Then, using javascript and the 'onfocus' event, clear the field.

So you would have:

<input type="textbox" value="Name" onfocus="if (this.value=='Name') this.value='';"/>
Tuesday, September 21, 2021
 
sathiya
answered 3 Months ago
25

To your first point, we obsoleted it because it is woefully inadequate by modern security standards. It uses a straight hash with no iteration, which means that anybody who accesses your Web.config can easily figure out what the original passwords were. But if you're willing to accept these risks, you can certainly continue using it going forward. Just suppress the warning and solider on with your original code.

To your second point, there is no relation whatsoever between any of this and OWIN.

Hope this clears it up!

Sunday, September 26, 2021
 
Alexander
answered 3 Months ago
19

The problem with your code is that you're calling RedirectFromLoginPage, which will create the forms authentication cookie, overwriting the cookie you've just created:

HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket); 
cookie.Expires = ticket.Expiration; 
Response.Cookies.Add(cookie); 

FormsAuthentication.RedirectFromLoginPage(userName, persistCookie); <-- creates a new cookie

The cookie created by RedirectFromLoginPage will of course have the default timeout taken from configuration.

Your second version is the way to go.

Saturday, October 16, 2021
 
Valdas
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share