Introduction
In the previous part, we discussed various forms, properties, and settings we want to use in the application.
If you haven’t read the first three parts, please have a look before continuing with this.
In this application, we are discussing the following three forms wanted in our Application.
- Splash Window
- Activation Window
- Login Window
The flow of the article
- Splash screen
- Progress bar
- Activation Window
- Finding Current Year, Month, and Date in C#
- Login Window
Splash screen and progress
In many applications like Visual Studio, Adobe Photoshop, and Microsoft Excel, we have seen splash screens.
What is a splash screen?
It's a small form, which shows when we open an application. The model of our application splash screen is given below.
It includes two or three labels and one progress bar. And also we want to use a timer in this form.
In the Constructor region with the InitializeComponent function, we use the following code.
#region Constructor
public Splash()
{
InitializeComponent();
timer1.Enabled = true;
timer1.Interval = 3000;
progressBar1.Increment(100);
}
#endregion
Under the timer tick event, write the following code.
#region Form Event
private void timer1_Tick(object sender, EventArgs e)
{
if (progressBar1.Value == 100)
{
timer1.Stop();
this.DialogResult = DialogResult.OK;
this.Close();
}
}
#endregion
Then Open Program.cs file and write the following code.
Splash sp = new Splash();
if (sp.ShowDialog() == DialogResult.OK)
{
Application.Run(new Login());
}
After placing this code you can run the application.
Initially, you can see the splash screen.
Activation window
There are many ways of activation. Some of them are mentioned below
Activation with serial key
This method is the most popular. This type of activation is used in Adobe products and Microsoft products.
Activation with webservice
In this type, we have more control over customers. But to activate the client's computer want to connect to the internet. In the latter session, we will describe in detail about this method.
Custom activation with date and time
This is very easy to implement. In this session, we are disclosing this method.
First of all, we are declaring three variables in Properties.Settings
One is in Boolean ‘isActivated’, the second is in string ‘SystemCode’ and the third is in integer ‘ActivationKe
Then create a form with controls like the below image.
Then change the code in the program.cs file. If the application is not activated then will show the activation form or will go to the login window.
Then we write code in Activation form.
Firstly declare a time zone. Here we use the Indian Time Zone.
private static TimeZoneInfo INDIAN_ZONE = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");
By putting a time zone you can get the date and time of your Location otherwise you will only get the client's time zone date and time.
Then we want to get minutes, Hours, and Days in month, month, and day in the current year and year respectively in integer format.
For that firstly we declare a dateTime variable by applying a time zone. The code is below.
DateTime indianTime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, INDIAN_ZONE);
Then we have to write code to get integer values while form loading.
- Minute
int mnts = indianTime.Minute;
- Hour
int hour = indianTime.Hour;
- Day in month
int day_in_month = indianTime.Day;
- Month
int month = indianTime.Month;
- Day in the Year
int day_in_year = indianTime.DayOfYear;
- Year
int year = indianTime.Year;
Then, apply these integer values to the textbox text property after converting to string. The code is given below.
string rslt = "N" + mnts.ToString() + "u" + hour.ToString() + "I" + day_in_month.ToString() + "t" + month.ToString();
txtSystemCode.Text = rslt;
Now we have SystemKey.
We want to store the SystemKey in Properties.Settings for future reference. The code is below.
Properties.Settings.Default.SystemCode = rslt;
And then we have to find out the activation key from the system key. The code for that is as follows.
int otp = mnts * hour * day_in_month * month * year;
Here we remove alphabetic letters from the System Code, then remove the last mentioned days in the year integer and instead, we take the year integer.
33 * 20 * 22 * 11 * 2019 as.
Minutes are 33, Hours are 20, the day in the month is 22, the month is 11 and the year is 2019.
The result is 322474680.
Then we have to save the integer in the properties.settings.
Properties.Settings.Default.ActivationKey = otp;
Finally, we save the properties.settings.
Properties.Settings.Default.Save();
The entire code under Activation Form Loading event is as follows.
private static TimeZoneInfo INDIAN_ZONE = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");
private void ActivationFour_Load(object sender, EventArgs e)
{
// Firstly we check whether we already stored the Code or not
if (Properties.Settings.Default.SystemCode == string.Empty)
{
DateTime indianTime =
TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, INDIAN_ZONE);
int mnts = indianTime.Minute;
int hour = indianTime.Hour;
int day_in_month = indianTime.Day;
int month = indianTime.Month;
int day_in_year = indianTime.DayOfYear;
int year = indianTime.Year;
string rslt = "N" + mnts.ToString() + "u" + hour.ToString() + "Y" +
day_in_month.ToString() + "t" + month.ToString() + "r" +
day_in_year.ToString();
Properties.Settings.Default.SystemCode = rslt;
int otp = mnts * hour * day_in_month * month * year;
Properties.Settings.Default.ActivationKey = otp;
Properties.Settings.Default.Save();
}
txtSystemCode.Text = Properties.Settings.Default.SystemCode;
}
Then we write code under the submit button and click the event.
Login login;
private void btnSubmit_Click(object sender, EventArgs e)
{
if (txtActivationKey.Text == Properties.Settings.Default.ActivationKey.ToString())
{
this.Hide();
login = new Login();
login.Show();
}
else
{
MessageBox.Show("The entered Activation Code is not correct. Please check and try again", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
If the entered activation key is correct, then the current activation form is hiding and the login form is showing. Otherwise, show an error report.
Login window
To get web model text boxes and buttons, we used user controls. We will discuss creating and applying user control in the upcoming sessions. Now let us discuss the code behind the login button.
In this form, we used Ado.net code to retrieve data from the database. In the upcoming sessions, we will use Stored Procedure, Ling, and Entity Framework.
Firstly we have to call the namespace in the section.
using System.Data.SqlClient;
In the Login button click event we want to write the following code.
First, we have to call the connection string. Here we have stored the connection string in Properties.settings; to know more about this method please read my article from here.
string strConnection = Properties.Settings.Default.strConnectionString;
Here we implement using statements to connect to the database. There are many benefits to using a statement. The using statement automatically calls the Dispose() method while leaving the scope of the statement. Using a statement is a more elegant way of disposing of the object than manual disposal. So using statements is a good practice.
The query used here is to collect the count of the user with the given Username and password. If the count is one, then the username and password entered is correct.
string Query = "Select Count(*) From [User] where UserName = '" + txtUserName.Text.ToUpper() + "' and PassWord = '" + txtPWord.Text + "'";
If the count is one, then the username and password entered is correct.
Then the current form is hidden and the main form will open.
if (dt.Rows[0][0].ToString() == "1")
{
main = new Main();
this.Hide();
main.Show();
}
To Call the Main form class we have to declare the form class first. We are declaring the main form before the button event.
Main main;
We declare two strings for validation error messages.
string msg1 = "Please check your Username and Password";
string msg2 = "Please enter Username and Password";
If the result is not ‘one’ then show the first message and clear both textboxes.
else
{
MessageBox.Show(msg1, "Error");
txtUserName.Text = string.Empty;
txtPWord.Text = string.Empty;
txtUserName.Focus();
}
The entire code is as follows.
Main main;
private void btnLoggin_Click(object sender, EventArgs e)
{
string strConnection = Properties.Settings.Default.strConnetionString;
try
{
string Query = "Select Count(*) From [User] where UserName = '" + txtUserName.Text.ToUpper() + "' and PassWord = '" + txtPWord.Text + "'";
using (SqlConnection con = new SqlConnection(strConnection))
{
using (SqlDataAdapter da = new SqlDataAdapter(Query, con))
{
DataTable dt = new DataTable();
da.Fill(dt);
string msg1 = "Please check your Username and Password";
string msg2 = "Please enter Username and Password";
if (txtUserName.Text != string.Empty & txtPWord.Text != string.Empty)
{
if (dt.Rows[0][0].ToString() == "1")
{
main = new Main();
this.Hide();
main.Show();
}
else
{
MessageBox.Show(msg1, "Error");
txtUserName.Text = string.Empty;
txtPWord.Text = string.Empty;
txtUserName.Focus();
}
}
else
{
MessageBox.Show(msg2, "Error");
txtUserName.Text = string.Empty;
txtPWord.Text = string.Empty;
txtUserName.Focus();
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Summary
In this article, we learned about the splash screen, progress bar, finding current minutes, hours, month, day in the month, year, and day in the year from a datetime and we understand the method behind the activation window and code behind the Login button in detail.
References
Conclusion
Thank you for reading my article. If you feel the article is useful please like and share the same with your friends and colleagues. And also please put comments about the article in the comment section. This will be very encouraging to me to write the subsequent chapters. If you want the entire source code you can contact me in my e-mail [email protected]
In the next session, we will discuss implementing a three-tier architecture.
Three-tier architecture provides many key benefits for the production and development of applications by modularizing the user interface, business logic, and data layers. Doing so gives greater flexibility to the development teams by allowing them to update a specific part of the application independently.
Most of the software companies are following this method. So I think the following sessions will be more helpful to students and beginners to get jobs. So please wait and see.
Thank you.