Tuesday, September 17, 2013

Login Form in a Windows Forms Application

The Form Shape : 



// there is 2 Labels, 2 TextBoxes, 2 Buttons, the first TextBox is named : txtUserName , the second TextBox is named : txtPassword, the Log In Button is named : btnLogin, and the Clear Button is named : BtnClear 

The Table in the database : 



// The table named is : LogIn_T 

In the Table Data i added a user name and a password : 



The Log In Button code : 

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if(txtUserName.Text == string.Empty && txtPassword.Text == string.Empty)
            {
                MessageBox.Show("please enter a user name and a password", "Alert", MessageBoxButtons.OK);
            }
            else if (txtUserName.Text == string.Empty && txtPassword.Text != string.Empty)
            {
                MessageBox.Show("please enter a username", "Alert", MessageBoxButtons.OK);
            }
            else if (txtUserName.Text != string.Empty && txtPassword.Text == string.Empty)
            {
                MessageBox.Show("please enter a password", "Alert", MessageBoxButtons.OK);
            }
            else if (txtUserName.Text != string.Empty && txtPassword.Text != string.Empty)
            {
                SqlConnection Con = new SqlConnection();
                Con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Saleem\Documents\Project.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
                Con.Open();
                String query = "SELECT UserName, Password From LogIn_T WHERE UserName = @UserName AND Password = @Password" ;
                SqlCommand com = new SqlCommand(query, Con);
                com.Parameters.AddWithValue("@UserName", txtUserName.Text);
                com.Parameters.AddWithValue("@Password", txtPassword.Text);
                SqlDataReader reader = com.ExecuteReader();
                if (reader.Read())
                {
                    // show the form you want
                    Form2 form2 = new Form2();
                    form2.Show();
                    this.Hide();
                }
                else
                {
                    MessageBox.Show("invalid username or password", "Alert", MessageBoxButtons.OK);
                }
              reader.Close();
              Con.Close();
            }
        }

The Clear Button Code : 

        private void BtnClear_Click(object sender, EventArgs e)
        {
            txtUserName.Text = string.Empty;
            txtPassword.Text = string.Empty;
        }



Testing : 

1 - Not entering a username and a password : 






2- Not entering a user name : 



3- Not entering a password : 



4- Invalid username : 


5- Invalid password : 


6- Invalid username and password : 


7- Valid username and password : 

Form2 opens : 


Using CheckBox, TextBox, Label, Button Controls in a Windows Forms Application

The Form shape : 





The Table in the database: 



// OrderID is an Identity, the table name is Order_T

The Order Button code : 

        private void button1_Click(object sender, EventArgs e)
        {
            string Meal1;
            string Meal2;
            string Meal3;
            if (CBMeal1.Checked == true)
            {
                Meal1 = "Ordered";
            }
            else
            {
                Meal1 = "Not ordered";
            }

            if (CBMeal2.Checked == true)
            {
                Meal2 = "Ordered";
            }
            else
            {
                Meal2 = "Not ordered";
            }

            if (CBMeal3.Checked == true)
            {
                Meal3 = "Ordered";
            }
            else
            {
                Meal3 = "Not ordered";
            }

            SqlConnection Con = new SqlConnection();
            Con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Saleem\Documents\Project.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
            Con.Open();
            String query = "Insert into Order_T(Name , Place, Phone, Meal1, Meal2, Meal3)Values('" + txtName.Text + "','" + txtPlace.Text + "','" + txtPhone.Text + "','" + Meal1 + "','" + Meal2 + "','" + Meal3 + "')";
            SqlCommand com = new SqlCommand(query, Con);
            com.ExecuteNonQuery();
            Con.Close();
            txtName.Text = string.Empty;
            txtPhone.Text = string.Empty;
            txtPlace.Text = string.Empty;
            CBMeal1.Checked = false;
            CBMeal2.Checked = false;
            CBMeal3.Checked = false;
        }

Example : 

The Form : 




// The data entered is only for testing 

The Table Data after pressing the Order Button : 






Calculate the mod of two Integer numbers in a Windows Forms Application


The Form i made : 




It includes 5 Labels, 3 TextBoxes, and 1 button , i named the first TextBox : TxtFirstNumber, the second TextBox  TxtSecondNumber, the third TextBox : TxtOutput, the Button : BtnCalculate 


The Calculate Button Code : 

        private void BtnCalculate_Click(object sender, EventArgs e)
        {
            int a = Convert.ToInt32(TxtFirstNumber.Text);
            int b = Convert.ToInt32(TxtSecondNumber.Text);
            TxtOutput.Text = Convert.ToString(a % b);
        }

Example : 







Monday, September 16, 2013

Calculate the division of two Integer numbers in a Windows Forms Application

The Form i made : 




It includes 5 Labels, 3 TextBoxes, and 1 button , i named the first TextBox : TxtFirstNumber, the second TextBox  TxtSecondNumber, the third TextBox : TxtOutput, the Button : BtnCalculate 


The Calculate Button Code : 

        private void BtnCalculate_Click(object sender, EventArgs e)
        {
            int a = Convert.ToInt32(TxtFirstNumber.Text);
            int b = Convert.ToInt32(TxtSecondNumber.Text);
            TxtOutput.Text = Convert.ToString(a / b);
        }

Example : 








Calculate the Multiplication of two Integer numbers in a Windows Forms Application

The Form i made : 





It includes 5 Labels, 3 TextBoxes, and 1 button , i named the first TextBox : TxtFirstNumber, the second TextBox  TxtSecondNumber, the third TextBox : TxtOutput, the Button : BtnCalculate 


The Calculate Button Code : 

        private void BtnCalculate_Click(object sender, EventArgs e)
        {
            int a = Convert.ToInt32(TxtFirstNumber.Text);
            int b = Convert.ToInt32(TxtSecondNumber.Text);
            TxtOutput.Text = Convert.ToString(a * b);
        }

Example : 








Calculate the subtraction of two Integer numbers in a Windows Forms Application

The Form i made : 




It includes 5 Labels, 3 TextBoxes, and 1 button , i named the first TextBox : TxtFirstNumber, the second TextBox  TxtSecondNumber, the third TextBox : TxtOutput, the Button : BtnCalculate 


The Calculate Button Code : 

        private void BtnCalculate_Click(object sender, EventArgs e)
        {
            int a = Convert.ToInt32(TxtFirstNumber.Text);
            int b = Convert.ToInt32(TxtSecondNumber.Text);
            TxtOutput.Text = Convert.ToString(a - b);
        }

Example : 



Calculate the addition of two Integer numbers in a Windows Forms Application


The Form i made : 




It includes 5 Labels, 3 TextBoxes, and 1 button , i named the first TextBox : TxtFirstNumber, the second TextBox  TxtSecondNumber, the third TextBox : TxtOutput, the Button : BtnCalculate 


The Calculate Button Code : 

        private void BtnCalculate_Click(object sender, EventArgs e)
        {
            int a = Convert.ToInt32(TxtFirstNumber.Text);
            int b = Convert.ToInt32(TxtSecondNumber.Text);
            TxtOutput.Text = Convert.ToString(a + b);
        }

Example :