C# -- "this.Hide()" fails ?

finance_teacher
finance_teacher used Ask the Experts™
on
The below has a Count > 0 and displays my ProductVisionMain form, but it does not hide my Login form after Rebuilding and deploying the APP.

How can i fix ? (screenshot attached)

        private void Login_Load(object sender, EventArgs e)
        {
            FMUSERTableAdapter ta1 = new FMUSERTableAdapter();
            DataSet_GMLLM.FMUSERDataTable dt1 = ta1.GetData(System.Environment.UserName.ToUpper());
            //DataSet_GMLLM.FMUSERDataTable dt1 = ta1.GetData("235235523"); // use this line to test failed login

            if (dt1.Rows.Count > 0)
            {
                this.Hide();
                ProductVisionMain frm = new ProductVisionMain();
                frm.Show();
            }

            else
            {
                label1.Visible = true;
            }
        }

0001.jpg
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Can you try:

this.Visible = false;

rather than .Hide and let me know how that works?

Regards,
Jon
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
The problem is you can't hide the form in the Load() event...

One solution is to use a Timer():
        private void Login_Load(object sender, EventArgs e)
        {
            FMUSERTableAdapter ta1 = new FMUSERTableAdapter();
            DataSet_GMLLM.FMUSERDataTable dt1 = ta1.GetData(System.Environment.UserName.ToUpper());
            //DataSet_GMLLM.FMUSERDataTable dt1 = ta1.GetData("235235523"); // use this line to test failed login

            if (dt1.Rows.Count > 0)
            {
                Timer tmr = new Timer();
                tmr.Interval = 50;
                tmr.Tick += new EventHandler(tmr_Tick);
                tmr.Start();
            }
            else
            {
                label1.Visible = true;
            }
        }

        void tmr_Tick(object sender, EventArgs e)
        {
            Timer tmr = (Timer)sender;
            tmr.Stop();

            this.Hide();
            ProductVisionMain frm = new ProductVisionMain();
            frm.FormClosed += new FormClosedEventHandler(frm_FormClosed);
            frm.Show();
            frm.BringToFront();
        }

        void frm_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Close();
        }

Open in new window

Miguel OzSenior Software Engineer
Top Expert 2009

Commented:
Is your login form a splashscreen (autologin)  or proper login form (type username/password and then click OK or cancel)?
If yes then Idle_mind post is correct else you need to hide in your OK click implementation.
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
Another option would be to move that logic out to program.cs and display the appropriate form based on the results of your check...
anarki_jimbelSenior Developer

Commented:
Another possible option would be to pass a login form reference to a main form and then hide the login from the main form load event.
It is not advised to hide the the form in its load event. A better solution would be to shift the logic to the place from where loginfrm.Show() or ShowDialog() is called.

Pseudo Code for the same would be something like:

FMUSERTableAdapter ta1 = new FMUSERTableAdapter();
            DataSet_GMLLM.FMUSERDataTable dt1 = ta1.GetData(System.Environment.UserName.ToUpper());
  if (dt1.Rows.Count <= 0)
  {
     LoginForm loginfrm = new LoginForm ();
     loginfrm.ShowDialog();
  }
  else
  {
     ProductVisionMain frm = new ProductVisionMain();
     frm.Show();
  }

And in the Event handler for Login From Load
 private void Login_Load(object sender, EventArgs e)
 {
   label1.Visible = true; // This label will always be visible as the dialog is only shown when dt1.Rows.Count >0 So you can shift it to designer.
 }

Thx!
Swaps...
You can't hide your form in Load event move your code in the Form_Shown event. Shown event is calling after your form is shown.
private void Login_Shown(object sender, EventArgs e)
        {
            FMUSERTableAdapter ta1 = new FMUSERTableAdapter();
            DataSet_GMLLM.FMUSERDataTable dt1 = ta1.GetData(System.Environment.UserName.ToUpper());
            //DataSet_GMLLM.FMUSERDataTable dt1 = ta1.GetData("235235523"); // use this line to test failed login

            if (dt1.Rows.Count > 0)
            {
                this.Hide();
                ProductVisionMain frm = new ProductVisionMain();
                frm.Show();
            }

            else
            {
                label1.Visible = true;
            }
        }

Open in new window

Author

Commented:
Ok, I will retest.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial