Creating an Accounting Application With C# - Part Four

Introduction

 
In the previous part, we discussed various forms, properties, and settings we want to use in the application.
 
In this application, we are discussing the following three forms wanted in our Application:
  1. Splash Window
  2. Activation Window
  3. Login Window

Flow of the article

  • Splash screen
  • Progress bar
  • Activation Window
  • Finding Current Year, Month and Date in C#
  • Login Window

Splash Screen and Progressbar

In many applications like Visual Studio, Adobe Photoshop, and Microsoft Excel, we have seen splash screen.
 
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,
 
Creating Accounting Application With C#
 
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 following code. 
  1. #region Constructor  
  2. public Splash()  
  3. {  
  4.    InitializeComponent();  
  5.    timer1.Enabled = true;  
  6.    timer1.Interval = 3000;  
  7.    progressBar1.Increment(100);  
  8. }  
  9. #endregion  
Under timer tick event, write following code:
  1. #region Form Event  
  2. private void timer1_Tick(object sender, EventArgs e)  
  3. {  
  4.     if (progressBar1.Value == 100)  
  5.     {  
  6.         timer1.Stop();  
  7.         this.DialogResult = DialogResult.OK;  
  8.        this.Close();  
  9.     }  
  10. }  
  11. #endregion  
Then Open Program.cs file and write following code,
  1. Splash sp = new Splash();  
  2. if (sp.ShowDialog() == DialogResult.OK)  
  3. {  
  4.      Application.Run(new Login());  
  5. }  
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 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 customer. But to activate the client computer want to connect with 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 variable in Properties.Settings
 
One is in Boolean ‘isActivated’, second is in string ‘SystemCode’ and third is in integer ‘ActivationKe
 
Creating Accounting Application With C#
 
Then create a form with controls like the below image.
 
Creating Accounting Application With C#
 
Then change code in program.cs file. If the application is not activated then will show the activation form or will go to login window.
 
Creating Accounting Application With C#
 
Then we write code in Activation form.
 
Firstly declare a time zone. Here we use Indian Time Zone, 
  1. 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 time zone date and time.
 
Then we want to get minuets, Hour, Day in month, month, day in the current year and year respectively in integer format.
 
For that firstly we declare a dateTime variable by applying time zone. The code is below.
  1. DateTime indianTime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, INDIAN_ZONE);  
Then we have to write code to get integer values while form loading.
 
Minute
  1. int mnts = indianTime.Minute;  
Hour 
  1. int hour = indianTime.Hour;  
Day in month
  1. int day_in_month = indianTime.Day;  
Month 
  1. int month = indianTime.Month;  
Day in the Year 
  1. int day_in_year = indianTime.DayOfYear;  
Year
  1. int year = indianTime.Year;  
Then, apply these integer values to the textbox text property after converting to string. The code is given below, 
  1. string rslt = "N" + mnts.ToString() + "u" + hour.ToString() + "I" + day_in_month.ToString() + "t" + month.ToString();  
  2. txtSystemCode.Text = rslt;  
Now we got SystemKey.
 
Creating Accounting Application With C#
 
We want to store the SystemKey in Properties.Settings for future reference. The code is below, 
  1. 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,
  1. int otp = mnts * hour * day_in_month * month * year;  
Here we remove alphabetic letters from the System Code, then removing last mentioned days in the year integer and instead we taking year integer.
33 * 20 * 22 * 11 * 2019 as,
 
Minuets is 33, Hour is 20, day in month is 22 , month is 11 and year is 2019.
 
The result is 322474680.
 
Then we have to save the integer in the properties.settings, 
  1. Properties.Settings.Default.ActivationKey = otp;  
Finaly we saving the properties.settings,
  1. Properties.Settings.Default.Save();  
The entire code under Activation Form Loading event is as follows,
  1. private static TimeZoneInfo INDIAN_ZONE = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");  
  2. private void ActivationFour_Load(object sender, EventArgs e)  
  3. {  
  4.      //fistly we checking whether we already stored the Code or not  
  5.     if (Properties.Settings.Default.SystemCode == string.Empty)  
  6.     {  
  7.         DateTime indianTime =   
  8.         TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, INDIAN_ZONE);  
  9.         int mnts = indianTime.Minute;  
  10.         int hour = indianTime.Hour;  
  11.         int day_in_month = indianTime.Day;  
  12.         int month = indianTime.Month;  
  13.         int day_in_year = indianTime.DayOfYear;  
  14.         int year = indianTime.Year;  
  15.         string rslt = "N" + mnts.ToString() + "u" + hour.ToString() + "Y" +   
  16.         day_in_month.ToString() + "t" + month.ToString() + "r" +   
  17.         day_in_year.ToString();  
  18.         Properties.Settings.Default.SystemCode = rslt;  
  19.         int otp = mnts * hour * day_in_month * month * year;  
  20.         Properties.Settings.Default.ActivationKey = otp;  
  21.        Properties.Settings.Default.Save();  
  22.    }  
  23.    txtSystemCode.Text = Properties.Settings.Default.SystemCode;  
  24. }  
Then we write code under submit button click event,   
  1. Login login;  
  2. private void btnSubmit_Click(object sender, EventArgs e)  
  3. {  
  4.      if (txtActivationKey.Text == Properties.Settings.Default.ActivationKey.ToString())  
  5.      {  
  6.          this.Hide();  
  7.          login = new Login();  
  8.          login.Show();  
  9.     }  
  10.     else  
  11.     {  
  12.           MessageBox.Show("The entered Activation Code is not correct. Please check and try again""Error", MessageBoxButtons.OK, MessageBoxIcon.Error);  
  13.     }  
  14. }  
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
 
Creating Accounting Application With C#
 
To get web model text boxes and buttons, we used user controls. We will discuss about creating and applying the user control in the upcoming sessions. Now let us discuss about the code behind the login button.
 
In this form we used Ado.net code to retrieve data from database. In the upcoming sessions we will use Stored Procedure, Ling and Entity Framework.
 
Firstly we have to call the name space in the section, 
  1. using System.Data.SqlClient;  
In the Login button click event we want to write following code.
 
First we have to call connection string. Here we have stored the connection string in Properties.settings; to know more about this method please read my article from here.
  1. string strConnection = Properties.Settings.Default.strConnetionString;  
Here we implement using statement to connect to the database. There are many benefits with the using statement. The using statement automatically calls the Dispose() method while leaving the scope of the statement. Using statement is a more elegant way of disposing the object than manual dispose. So the using statement is a good practice.
 
The query used here 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.
  1. 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 hiding and main form will open.
  1. if (dt.Rows[0][0].ToString() == "1")  
  2. {  
  3.     main = new Main();  
  4.     this.Hide();  
  5.     main.Show();  
  6. }  
To Call Main form class we have to declare the form class first. We are declaring the main form before the button event.
  1. Main main;  
We declare two strings for validation error messages.
  1. string msg1 = "Please check your Username and Password";  
  2. string msg2 = "Please enter Username and Password";  
If the result is not ‘one’ then showing first message and clear both textboxes.
  1. else  
  2. {  
  3.      MessageBox.Show(msg1, "Error");  
  4.      txtUserName.Text = string.Empty;  
  5.      txtPWord.Text = string.Empty;  
  6.      txtUserName.Focus();  
  7. }  
The entire code is as follows, 
  1. Main main;  
  2. private void btnLoggin_Click(object sender, EventArgs e)  
  3. {  
  4.     string strConnection = Properties.Settings.Default.strConnetionString;  
  5.     try  
  6.     {  
  7.         string Query = "Select Count(*) From [User] where UserName ='" + txtUserName.Text.ToUpper() + "' and PassWord = '" + txtPWord.Text + "'";  
  8.         using (SqlConnection con = new SqlConnection(strConnection))  
  9.         {  
  10.              using (SqlDataAdapter da = new SqlDataAdapter(Query, con))  
  11.              {  
  12.                 DataTable dt = new DataTable();  
  13.                 da.Fill(dt);  
  14.                 string msg1 = "Please check your Username and Password";  
  15.                 string msg2 = "Please enter Username and Password";  
  16.                if (txtUserName.Text != string.Empty & txtPWord.Text != string.Empty)  
  17.                {  
  18.                    if (dt.Rows[0][0].ToString() == "1")  
  19.                    {  
  20.                       main = new Main();  
  21.                       this.Hide();  
  22.                       main.Show();  
  23.                   }  
  24.                   else  
  25.                   {  
  26.                     MessageBox.Show(msg1, “Error”);  
  27.                     txtUserName.Text = string.Empty;  
  28.                     txtPWord.Text = string.Empty;  
  29.                     txtUserName.Focus();  
  30.                   }  
  31.             }  
  32.             else  
  33.             {  
  34.                 MessageBox.Show(msg2, “Error”);  
  35.                 txtUserName.Text = string.Empty;  
  36.                 txtPWord.Text = string.Empty;  
  37.                 txtUserName.Focus();  
  38.             }  
  39.         }  
  40.     }  
  41.   }  
  42.   catch (Exception ex)  
  43.   {  
  44.        MessageBox.Show(ex.Message);  
  45.   }  
  46. }  

Summary

 
In this article we learned about splash screen, progress bar, finding current minuets, hour, month, day in the month, year and day in the year from a datetime and we understand the method behind activation window and code behind Login button in details.
 
References

Conclusion

 
Thank you for reading my article. If you feel the article is useful please like and share the same to 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 about implementing three-tier architecture.
 
Creating Accounting Application With C#  
 
Three-tier architecture provides many key benefits for 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.

Similar Articles