I want to create a timer when user picks an Item from listbox:
private void ExistUserList_SelectedIndexChanged
(object sender, EventArgs e)
Then write down the wrong password in textbox:
private void FirstPassword_TextChanged
(object sender, EventArgs e)
and push the button:
private void NextForm3_Click(object sender, EventArgs e)
For 3 times, then it will count down time from 10 seconds to 0 seconds in the button with a message box.
What I have tried:
I have tried this code, but it doesn't work:
private void ExistUserList_SelectedIndexChanged(object sender, EventArgs e)
{
string selectedText = ExistUserList.SelectedItem.ToString();
AllText2.Text = selectedText;
if (ExistUserList.SelectedIndex >= 0)
{
string selectedUsername = ExistUserList.SelectedItem.ToString();
int userIndex = users.IndexOf(selectedUsername);
if (userIndex >= 0)
{
FirstPassword.Text = passwords[userIndex];
}
}
if (ExistUserList.SelectedItem != null)
{
selectedUsername = ExistUserList.SelectedItem.ToString();
}
if (ExistUserList.SelectedItem != null
&& ExistUserList.SelectedItem != "Administrator")
{
string selectedUser = ExistUserList.SelectedItem.ToString();
AllText2.Text = "Lokalny użytkownik: " + selectedUser;
}
else if(ExistUserList.SelectedItem == "Administrator")
{
string selectedUser = ExistUserList.SelectedItem.ToString();
AllText2.Text = "Domenowy i lokalny użytkownik: " +
selectedUser;
}
FirstPassword.Text = string.Empty;
failedAttempts = 0;
if (isCountingDown)
{
countdown = 10;
isCountingDown = false;
}
else if (ExistUserList.SelectedItem.ToString() == "Test")
{
if (FirstPassword.Text != "zaq1@WSX")
{
failedAttempts++;
if (failedAttempts >= 3)
{
if (!isCountingDown)
{
StartCountdown();
}
}
else
{
MessageBox.Show("Podałeś złe hasło.
Pozostało prób: " + (3 - failedAttempts));
}
}
private void StartCountdown()
{
isCountingDown = true;
Timer timer = new Timer();
timer.Interval = 1000;
timer.Tick += (s, ev) =>
{
countdown--;
if (countdown <= 0)
{
timer.Stop();
isCountingDown = false;
NextForm3.Enabled = true;
buttonBlocked = false;
FirstPassword.Enabled = true;
SecondPassword.Enabled = false;
MessageBox.Show("Przycisk został odblokowany.");
countdown = 10;
}
else
{
MessageBox.Show("Pozostało " + countdown + " sekund.");
}
};
timer.Start();
}