Hello all,

As a newbie in C# I am searching numerous forums and tutorial to do my reading and learning. I was also searching for a solutin for Login by a separate form and found on this forum a tread from May 14th 2010 called : Enabling and disabling a form.
The only thing in my form is that I have more than 1 control I want to enable of disable and that I want to start my login form from the main form by a button.
At this moment I have to log in triple maybe because I have 3 enable lines and my loginform start before my main form.
[edit] Just found out it is not effected by the 3 enable lines..[/edit]

Is there someone who can help me to let the forms open in the right order form me and to let me log in just once? Many thanks!

Think the trick is in my main form in the load void.

public bool MainControl = false;

        LoginForm form2;

        public Form1()
        {

            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)
        {

                OpenPhone.Enabled = false;
                OpenLog.Enabled = false;
                emailToolStripMenuItem.Enabled = false;

            form2 = new LoginForm(this);
            form2.ShowDialog();

            if (MainControl)
            {
                OpenPhone.Enabled = true;
                OpenLog.Enabled = true;
                emailToolStripMenuItem.Enabled = true;
            }

        }

        private void loginbutton_Click(object sender, EventArgs e)
        {
            // opening login form
            // some code to write...

        }

btw, one thing to mention here: Why you dont create a login form before the main one even appears?
You can use DialogResult() method to check if the user has entered the correct login data or not. How to use it, you can take a look on example here: http://support.microsoft.com/kb/816145

And this would be my example:

static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            string proc = Process.GetCurrentProcess().ProcessName;
            Process[] processes = Process.GetProcessesByName(proc);
            if (processes.Length > 1)
            {
                MessageBox.Show("Application is already running.", "Notification", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                using (Login login = new Login())
                {
                    login.StartPosition = FormStartPosition.CenterScreen;
                    if (login.ShowDialog() == DialogResult.OK)
                    {   
                        //passing logged userName into Form1`s constructor                     
                        Application.Run(new Form1(login.strUserName)); 
                    }
                    //if you will turn down the login 
                    //(for example after 3 wrong logins in Login forms)
                    //the app. will close it self!
                }
            }
        }

Hello Mitja,

Thanks for replying this fast. When I read all this I realize I have much more reading to do. However I get the point youre making.
Some explaining: I need my base form before logging in because it has features that can be used without logging in. The logging in makes it running with other (same) programms around the globe. I will study your code and examples to understand how it can be used in my script.
However, what I don't get is why, in the code I used from the other tread, why logging in is triple and even after shrinking and growing the login form appears and have again to log in triple, only then by just closing the loginform 3 times. There is somehow someting that makes that do. Also your code will shut that option down as I understand it well.

Do you maybe know some good turorials for C# that are also understandable for newbies like me? Thanks!

Again some reading and trying further.. but found a solution. Also the tripple login disapeared. Just to share and thanks to everyone who helped.

Mainform:

namespace NrkComCenter
{
    public partial class Form1 : Form
    {

        public bool MainControl = false;

        LoginForm form2;

        public Form1()
        {

            InitializeComponent();

            OpenPhone.Enabled = false;
            OpenLog.Enabled = false;
            emailToolStripMenuItem.Enabled = false;

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void loginbutton_Click(object sender, EventArgs e)
        {
            
            form2 = new LoginForm(this);
            form2.ShowDialog();

            if (MainControl)
            {
                OpenPhone.Enabled = true;
                OpenLog.Enabled = true;
                emailToolStripMenuItem.Enabled = true;

            }
        }

        private void OpenPhone_Click(object sender, EventArgs e)
        {
            // openen telefoonlijst scherm
        }

        private void OpenLog_Click(object sender, EventArgs e)
        {
            // openen logging scherm
        }

        private void OpenApp_Click(object sender, EventArgs e)
        {
            // openen apparatuur scherm
        }

        private void emailToolStripMenuItem_Click(object sender, EventArgs e)
        {
            EmailProp setemail = new EmailProp();
            setemail.Show();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Interval = 1000;
            clock1.Text = DateTime.Now.ToLongTimeString();
        }

    }
}

Loginform:

namespace NrkComCenter
{
    public partial class LoginForm : Form
    {

        Form1 form1;

        public LoginForm(Form1 F)

        {
            InitializeComponent();

            form1 = F;
        }

        private void Inloggen_Load(object sender, EventArgs e)
        {

        }

        private void LoginLocal_Click(object sender, EventArgs e)
        {
            if (UsernameInput.Text != "" && PasswordInput.Text != "")
            {
                if (UsernameInput.Text == "abc" && PasswordInput.Text == "123")
                {

                    form1.MainControl = true;

                }
                this.Close();

            }
            else
            {
                MessageBox.Show("Ongeldige gebruikersnaam of wachtwoord", "Waarschuwing",
                MessageBoxButtons.OK,
                MessageBoxIcon.Warning);
            }
        }

        private void LoginGlobal_Click(object sender, EventArgs e)
        {
            if (UsernameInput.Text != "" && PasswordInput.Text != "")
            {
                if (UsernameInput.Text == "abc" && PasswordInput.Text == "123")
                {

                    form1.MainControl = true;

                }
                this.Close();
            }
            else
            {
                MessageBox.Show("Ongeldige gebruikersnaam of wachtwoord", "Waarschuwing",
                MessageBoxButtons.OK,
                MessageBoxIcon.Warning);
            }
        }

        private void LoginCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

So running for the next dilemma.
Regards, M

Why you dont use ShowDialog method, like I showed in the example above?
You should just modify the code a bit, to look something like this:

//MAIN FORM:
        private void buttonLogin_Click(object sender, EventArgs e)
        {
            using (Form2 form2 = new Form2())
            {
                form2.StartPosition = FormStartPosition.CenterParent;
                if (form2.ShowDialog() == DialogResult.OK)
                {
                    //LOGIN SUCCEEDED
                    //ENABLE THINGS HERE
                }
            }
        }

        //LOGIN FORM:
        private void button1_Click(object sender, EventArgs e)
        {
            string userName = textBox1.Text;
            string password = textBox2.Text;
            if (userName != String.Empty && password!=String.Empty)
            {
                if (userName == "abc" && password == "123")
                    DialogResult = DialogResult.OK;
                else
                    MessageBox.Show("Username and password are not correct.", "Warning");
            }
            else
            {
                MessageBox.Show("Ongeldige gebruikersnaam of wachtwoord", "Waarschuwing",
                MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

Try to use this kinda code - it more appropriate then your version - using a ShowDialog() method has plenty of advantages, you can see that in LoginForm you dont need to call a Close() or Dispose() method when you call DialogResult property.
And at the same time, on MainForm, you can use DialogResult property if its equal to OK (depends how you set it in LoginForm), and you can then do a seperate code for OK, or Cancel.

Hope it helps,
Mitja

Tnx Mitja,

Will try out your suggestions. It makes the code simpler indeed.

Marcel

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.