1
namespace JadeComplete
{
    public partial class Form1 : Form
    {
        string curPass = "someDefaultPass";
        public Form1()
        {
            string fileName1 = "authentication.txt";

            if (System.IO.File.Exists(fileName1))
            curPass = System.IO.File.ReadAllText(fileName1);
            if (System.IO.File.ReadAllText(fileName1) == curPass)
            {
                Hide();
                Form2 frm = new Form2();
                frm.Show();
            }
        }
    }
}

I don't know why this isn't working. I have tried multiple things but can't figure out what's wrong.
I have 2 Forms showing when it's supposed to be removing one, the problematic area being right after public Form1()

Jimi
  • 29,621
  • 8
  • 43
  • 61
Exvient
  • 41
  • 5
  • What is it supposed to do, Hide form1? – TheGeneral Sep 16 '18 at 03:02
  • @Saruman yes, it's supposed to be hiding Form1 and making Form2 visible. I don't know what's going wrong. – Exvient Sep 16 '18 at 03:03
  • 3
    What is the point of reading the same file twice and comparing it to itself? – Tyler Roper Sep 16 '18 at 03:04
  • @TylerRoper Being honest I don't know much of what I'm doing I'm just trying to make a simple login panel for my application, any tips or info would help a lot, and the more I can learn whilst doing this the better. – Exvient Sep 16 '18 at 03:05
  • Does `authentication.txt` exist? If it doesn't, that could cause some sort of error – Radvylf Programs Sep 16 '18 at 03:11
  • 2
    You're trying to hide the form before it's shown. Check [this question](https://stackoverflow.com/q/7003587/4934172). – 41686d6564 stands w. Palestine Sep 16 '18 at 03:13
  • You have your code in the form's constructor before the form has even been created. Just create a bool variable with the result of your file check then switch forms if required after it has loaded. Although this code should probably go elsewhere such as in the Form_Load event. – Deolus Sep 16 '18 at 03:37
  • @Deolus `Form_Load` is no different because it fires before the `Shown` event. Check the duplicate question for more info. – 41686d6564 stands w. Palestine Sep 16 '18 at 04:05
  • @AhmedAbdelhameed Yes I know, that's why I said to create a bool. It would be better to put the code at the Program level and open the correct form depending on the result but I didn't want to complicate things. – Deolus Sep 16 '18 at 04:13

2 Answers2

0

This is the order of events in Forms:

  1. Control.HandleCreated
  2. Control.BindingContextChanged
  3. Form.Load
  4. Control.VisibleChanged
  5. Form.Activated
  6. Form.Shown

You are trying to set form to hide when the Form.Load is trying to set visible to true.

Better to use Form.Shown Or override OnVisibleChanged event instead of using the constructor.

I would also encapsulate IO operations in try {} catch and carch exceptions like FileNotFound or general IOException.

More here:

https://learn.microsoft.com/en-us/dotnet/framework/winforms/order-of-events-in-windows-forms

Gauravsa
  • 6,330
  • 2
  • 21
  • 30
0

@Exvient, Gauravsa answer is correct, you are trying to hide your form before its shown. you can write your code in Shown event, my sample code for you -

namespace HideForm1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            try
            {
                string defpassword = "1234"; //your default password
                string passwordfromfile = "1234"; //password you are reading from .txt file

                if (passwordfromfile == defpassword)
                {
                    Form2 objForm2 = new Form2();
                    Hide();
                    objForm2.Show();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Exception caught: " + ex.Message);
            }

        }
    }
}
umesh shukla
  • 139
  • 3
  • 13
  • That would work. Not a good idea though, because the form will flash for a second before hiding. A better option is to override `SetVisibleCore` as shown in [one of the answers](https://stackoverflow.com/a/7003687/4934172) to the duplicate question. – 41686d6564 stands w. Palestine Sep 16 '18 at 13:42