Working with Controls in C#

In this Blog, we will see how to use ProgressBar control in a Windows Forms using Visual Studio 2005. We will also use Timer & BackgroundWorker in a forms.

image1.jpg

ProgressBar : Displays a bar that fills to indicate to the user the progress of an operation.

image2.jpg

Timer : Component that raises an event at user defined intervals.

image3.jpg

BackgroundWorker : Executes an operation in a separate thread.

Here, we will use these controls for Windows application at Login Authentication operation.

Let's Start to Windows Application.

Drag  ProgressBar, Timer  &  Backgroundworker to windows form,

Write code to btnLogin

if (txtLogin.Text == "ADMIN" && txtPass.Text == "PASS")
{
  timer1.Enabled = true;
  timer1_Tick(sender, e);
  panel2.Enabled = true;
  panel3.Enabled = true;
  lblTime.Text = Convert.ToString(DateTime.Now.TimeOfDay).Substring(0, 8);
  lblDate.Text = Convert.ToString(DateTime.Now.Date).Substring(0, 11);
}
else
{
MessageBox.Show("Please Enter Right password and username");
}

Write code to BackgroundWorker

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
  System.ComponentModel.BackgroundWorker worker = (System.ComponentModel.BackgroundWorker)sender;
  backgroundWorker1.RunWorkerAsync();
}

Write code to Timer

if (progressBar1.Value < progressBar1.Maximum)
{
   progressBar1.Value = progressBar1.Value + 50;
}
else
{
  timer1.Enabled = false;
  lblTime.Enabled = true;
  lblTime.Visible = true;
  txtLogin.Enabled = false;
  txtPass.Enabled = false;
  progressBar1.Enabled = false;
  progressBar1.Visible = false;
  btnLogin.Enabled = false;
}

For full code, download files.

User ID : ADMIN
Password :PASS

image4.jpg

AFTER SUCCESSFULLY LOGIN AUTHENTICATION…

image5.jpg