I am working with multiple forms in C#.

My startup Form /or/ Parent Form /or/ Form1 is a login form.

on my Form1, I have a textbox1 for username and textbox2 for password and a buttons for submit and cancel.

on click event of submit button,if textbox1.text && textbox2.text are correct, it will show the Form2 and hide the Form1.

on my Form2 I have a delete button.

once delete button is clicked, it will show the Form1 and hide Form2 to enter the username and password again for security of the record before deleting.

Note: I know how to do the above issue, my problem is the issue below this line:

I want to resume the delete method I created once username and password entered in Form1 are correct.

AND do nothing but go back to Form2 if neither is correct OR if Form1 is closed OR if cancel button is hit.

How do I do the checking of username and password again in Form1 to be able to delete the record?

Could it be something like this:

if (Form1.textbox1.text == "mySetUsername" && Form1.textbox2.text == "mySetPassword")
{
resume my delete method
}

else

{
cancel event args
or something like form2.show (); form1.Hide();
}

Please show the possible codes or events. Thank you!

Recommended Answers

All 4 Replies

What you need is a seperate method (maybe even in a new class) for checking the user name and password.
btw, where do you store usernames and their passwords? Do you use a database, or a file on hdd?

In any case, you need a new boolean method where you compare the inserted username with password. Return true or fase. If the username belongs to the password, return true, if not, return false. So if on form1 comes true, form is shown, if its false, you show an exception with messageBox ("username and password are wrong").

And this method you use every single time when you need a login.
Do you understand me?

An example code:

public class Login
    {
        public static bool LoginCheck(string name, string password)
        {
            bool bChecking;
          //if you use database you do the ckecing:
            //using sqlConnection...
            string str = @"SELECT password FROM Users WHERE UserName = @userName";
            //using sqlCommand....
            cmd.Parameters.Add("@userName", SqlDBType.VarChar, 50).Value = name;
            //...
            //then you compare the password from query and the password inserted
            //return true if passwords are equal: 
            bChecking = true;
            //return false if they are different:
            bChecking = false;

            return bChecking;
        }
    }
    }

if not, please feel free to ask for some additional info...
Mitja

I dont use a database for username and password. I just use a lsimple if else logic there. but my question is how do I check again if the correct username and password have been entered the 2nd time once form2 is hidden and form1 is shown the 2nd time.

In Form1 I just use these, not much secured since its just for me:

private void submitButton_Click(object etc)
{
if (textbox1.Text == "myUseName" && textbox2.Text == "myPassword")
{
Form2 myform2 = new Form2();
this.Hide();
myform2.Show();
}
}

//Like I said my problem is the "Below the line" issue. Thanks!

Hi, you can do this by creating a static class and then storing the username and password in a variable that
are declared static

Form1's code

private void button1_Click(object sender, EventArgs e)
        {
            //use the Static class to store the values temporarily
            Class1.username = textBox1.Text.ToString(); 
            Class1.password = textBox2.Text.ToString();
            //Check whether form 2 has loaded already
            if (!Class1.bSecondFormLoaded)
            {
                /********************************
                 * Make Your Authentication process here like *
                 * if (textbox1.Text == "myUseName" && textbox2.Text == "myPassword")*
                 * ******************************/
                //Make the formloaded flag to true
                Class1.bSecondFormLoaded = true;
                Form2 frm2 = new Form2();
                frm2.Show();
                //Unloading may cause the entire application to terminate
                this.Visible = false;
            }
            else
            {
                this.Close();
            }
        }

Form2's Code

private void button1_Click(object sender, EventArgs e)
        {
            Form1 frm = new Form1();
            /*show the dialog*/
            frm.ShowDialog();
            /* the variables you needed are stored in the variables
             * class1.username and class1.password
             * use it for you function
            * */
            MessageBox.Show(Class1.username);
            MessageBox.Show(Class1.password);
        }

The Static Class Implementation
Class1.cs

static class Class1
    {
        public static string username; //Value to store username
        public static string password; //Value to store password
        public static bool bSecondFormLoaded; //flag to check whether the form two is loaded
    }

One -Ve point here would be one form1's instance will be running all thro the application process. hope this would
have solved your problem.

All the best.

Sam Sylvester
samsylvestertty@gmail.com

int[] a;
            a = new int[10];
            int b = 1;

            if (b <= 10)
            {
                for (int i = 0; i < 10; i++) ;
                a[b] = Convert.ToInt32(this.textBox1.text);
                listBox1.Items.Add(a[b]);
                b++;
            }
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.