[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Ways to call code after Form OnLoad is finished?

Posted on 2005-05-17
9
Medium Priority
?
1,169 Views
Last Modified: 2012-05-05
I have 2 forms, a Login window and a mainform. However it isn't the standard Login -> Mainform flow. When the program starts, the Mainform appears (with all functionality disabled) and the Login window appears ontop of the MainForm. When the user logs in, the Login form goes away, and the Mainform is enabled and active.

Currently, the way Im doing this, in Mainform_Onload it starts a Timer set to 100. When the timer pulses, it disables the timer and creates the Login and does ShowDialog(). If they enter correct login information, the Login form goes away and then Mainform is enabled.

I'm using a timer because if I create a Login instance during Mainform_Onload and ShowDialog() while still in the Onload, the Login page shows, but the Mainform never gets painted until the user is done with the Login screen.

Is there a way to do what I am doing *without* having to rely on a Timer? So far nothing I've done has gotten Mainform to show before it finishes the OnLoad method, and the only way I've found to delay the Login page until that has been done is by using a Timer.

-SEinarsson-


0
Comment
Question by:SEinarsson
  • 4
  • 3
  • 2
9 Comments
 
LVL 21

Expert Comment

by:Yurich
ID: 14022625
since you want to have your login screen as a modal dialog, the way you're currently using is prorably the one only. There could be a couple of different ways in the case of not modal dialog, including satring your login form in a separate thread, but since your loging logically must be modal, i don't see any other ways of doing it.

If you want to change your functionality, you can of course have a button "Login" in which case you wouldn't need any timer.

regards,
yurich
0
 
LVL 21

Expert Comment

by:Yurich
ID: 14022666
Huh, I think I have figured out something...

If you don't want to use a timer, you can imploy your mouse enter event of the form, in the following way:

// must have, otherwise will appear everytime you enter your mouse on to the form
bool logged = false;
private void Form1_MouseEnter(object sender, System.EventArgs e)
{
      if( !logged )
      {
            Login login = new Login();
            login.ShowDialog();
            logged = true;
      }
}

it will work fine but I can see a little problem already... You'll have to notify a user somehow that s/he has to point the mouse on something in order to log-in. Anyway, you can work out something starting from here ;)

regards,
Yurich
0
 
LVL 10

Expert Comment

by:jonvaughan
ID: 14023690
Is this WebForm or WinForm ?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 10

Accepted Solution

by:
jonvaughan earned 1400 total points
ID: 14023737
WinForm solution .....

Of course you will have to override the close event to stop someone just closing the modal window but you can do this ...

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication1
{
      /// <summary>
      /// Summary description for Form1.
      /// </summary>
      public class Form1 : System.Windows.Forms.Form
      {
            private System.Windows.Forms.Label label1;
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.Container components = null;

            public Form1()
            {
                  //
                  // Required for Windows Form Designer support
                  //
                  InitializeComponent();
                  this.Show();
                  Form2 loginForm = new Form2();
                  loginForm.ShowDialog();

                  //
                  // TODO: Add any constructor code after InitializeComponent call
                  //
            }

            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            protected override void Dispose( bool disposing )
            {
                  if( disposing )
                  {
                        if (components != null)
                        {
                              components.Dispose();
                        }
                  }
                  base.Dispose( disposing );
            }

            #region Windows Form Designer generated code
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                  this.label1 = new System.Windows.Forms.Label();
                  this.SuspendLayout();
                  //
                  // label1
                  //
                  this.label1.Location = new System.Drawing.Point(104, 24);
                  this.label1.Name = "label1";
                  this.label1.TabIndex = 0;
                  //
                  // Form1
                  //
                  this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                  this.ClientSize = new System.Drawing.Size(292, 273);
                  this.Controls.Add(this.label1);
                  this.Name = "Form1";
                  this.Text = "Form1";
                  this.ResumeLayout(false);

            }
            #endregion

            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                  Application.Run(new Form1());
            }

      
      }

      public class Form2 : System.Windows.Forms.Form
      {
            private System.Windows.Forms.TextBox textBox1;
            private System.Windows.Forms.TextBox textBox2;
            private System.Windows.Forms.Button button1;
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.Container components = null;

            public Form2()
            {
                  //
                  // Required for Windows Form Designer support
                  //
                  InitializeComponent();

                  //
                  // TODO: Add any constructor code after InitializeComponent call
                  //
            }

            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            protected override void Dispose( bool disposing )
            {
                  if( disposing )
                  {
                        if(components != null)
                        {
                              components.Dispose();
                        }
                  }
                  base.Dispose( disposing );
            }

            #region Windows Form Designer generated code
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                  this.textBox1 = new System.Windows.Forms.TextBox();
                  this.textBox2 = new System.Windows.Forms.TextBox();
                  this.button1 = new System.Windows.Forms.Button();
                  this.SuspendLayout();
                  //
                  // textBox1
                  //
                  this.textBox1.Location = new System.Drawing.Point(24, 16);
                  this.textBox1.Name = "textBox1";
                  this.textBox1.Size = new System.Drawing.Size(112, 20);
                  this.textBox1.TabIndex = 0;
                  this.textBox1.Text = "textBox1";
                  //
                  // textBox2
                  //
                  this.textBox2.Location = new System.Drawing.Point(24, 48);
                  this.textBox2.Name = "textBox2";
                  this.textBox2.Size = new System.Drawing.Size(112, 20);
                  this.textBox2.TabIndex = 1;
                  this.textBox2.Text = "textBox2";
                  //
                  // button1
                  //
                  this.button1.Location = new System.Drawing.Point(56, 88);
                  this.button1.Name = "button1";
                  this.button1.TabIndex = 2;
                  this.button1.Text = "Login";
                  this.button1.Click += new System.EventHandler(this.button1_Click);
                  //
                  // Form2
                  //
                  this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                  this.ClientSize = new System.Drawing.Size(192, 117);
                  this.Controls.Add(this.button1);
                  this.Controls.Add(this.textBox2);
                  this.Controls.Add(this.textBox1);
                  this.Name = "Form2";
                  this.Text = "Form2";
                  this.ResumeLayout(false);

            }
            #endregion

            private void button1_Click(object sender, System.EventArgs e)
            {
                  if (this.textBox1.Text == "Jon")
                        this.Close();
            }


      }

}




0
 
LVL 21

Expert Comment

by:Yurich
ID: 14023943
that's right! just put that stuff in a constructor. Well done Jon, collect your points ;)
0
 
LVL 10

Expert Comment

by:jonvaughan
ID: 14024715
And why not ?
0
 

Author Comment

by:SEinarsson
ID: 14035480
This is WinForm.

Putting the Login in the Constructor of the Mainform, and the Mainform doesn't get shown on screen until after the user has logged in. I need the Mainform to be seen on-screen, with the Login window on top of it.

And the login screen has to appear automatically, I can't wait until the user mouse-enters the form.

-Sarkis-
0
 
LVL 21

Expert Comment

by:Yurich
ID: 14035935
actually it will... have you tried jon's code?? works just fine - the main form is shown at the same time as you login form, you can drop a couple of controls on it to see that's are properly drawn as well.
0
 

Author Comment

by:SEinarsson
ID: 14036475
I had ran the code I was working with with the ShowDialog() in the first Form's constructor, and it didn't show. Tried it again just now and it works this time, I guess I did/copied something wrong the first time. It looks to be doing what I need now, thanks.

-SEinarsson-
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Integration Management Part 2
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question