Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to create a timer when user picks an Item from listbox:
C#
private void ExistUserList_SelectedIndexChanged
(object sender, EventArgs e)

Then write down the wrong password in textbox:
C#
private void FirstPassword_TextChanged
(object sender, EventArgs e)

and push the button:
C#
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:
C#
private void ExistUserList_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Pobierz wybrany element z kontrolki ExistUserList
            string selectedText = ExistUserList.SelectedItem.ToString();
            // Ustaw wybrany tekst w kontrolce AllText2
            AllText2.Text = selectedText;

            if (ExistUserList.SelectedIndex >= 0)
            {
                // Wybrano użytkownika, więc wyświetl hasło w TextBox
                string selectedUsername = ExistUserList.SelectedItem.ToString();
                int userIndex = users.IndexOf(selectedUsername);

                if (userIndex >= 0)
                {
                    // Ustaw hasło w TextBox
                    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; // Zerujemy liczbę nieudanych prób
            if (isCountingDown)
            {
                // Jeśli trwa odliczanie, zatrzymaj je i zresetuj
                countdown = 10;
                isCountingDown = false;
            }

else if (ExistUserList.SelectedItem.ToString() == "Test")
                {
                    /*
                    if (FirstPassword.Text != "zaq1@WSX")
                    {
                        failedAttempts++;
                        if (failedAttempts >= 3)
                        {
                            NextForm3.Enabled = false;
                            buttonBlocked = true;
                            FirstPassword.Enabled = false;
                            SecondPassword.Enabled = false;
                            FirstPassword.Text = string.Empty;
                            failedAttempts = 0; // Zerujemy liczbę nieudanych prób
                            MessageBox.Show("Podałeś złe hasło trzy razy. 
                            Przycisk zostanie zablokowany na 10 sekund.");
                            Timer timer = new Timer();
                            timer.Interval = 10000; // 10 sekund w milisekundach
                            timer.Tick += (s, ev) =>
                            {
                                NextForm3.Enabled = true;
                                buttonBlocked = false;
                                FirstPassword.Enabled = true;
                                SecondPassword.Enabled = false;
                                MessageBox.Show("Przycisk został odblokowany.");
                                timer.Stop();
                            };
                            timer.Start();
                        }
                        else
                        {
                            MessageBox.Show("Podałeś złe hasło. 
                            Pozostało prób: " + (3 - failedAttempts));
                        }
                    }
                    */
                    if (FirstPassword.Text != "zaq1@WSX")
                    {
                        failedAttempts++;
                        if (failedAttempts >= 3)
                        {
                            if (!isCountingDown)
                            {
                                StartCountdown();
                            }

                            // Reszta kodu bez zmian
                        }
                        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; // 1 sekunda w milisekundach
            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; // Zresetuj czas odliczania
                }
                else
                {
                    MessageBox.Show("Pozostało " + countdown + " sekund.");
                }
            };
            timer.Start();
        }
Posted
Updated 1-Nov-23 10:14am
v2
Comments
Richard MacCutchan 31-Oct-23 8:33am    
What do you mean by "it doesn't work"? Please do not expect people to be able to guess what happens when you run your code. You need to expalian exactly what results you see, or do not see, and why that is not what is expected.
Inczu 31-Oct-23 8:36am    
The timer doesn't work, rest of the code is working fine.
Richard Deeming 31-Oct-23 9:08am    
"I have tried this code but it doesn't work"
"The timer doesn't work"

Stop trying to type as little as possible; you're not being charged by the character here.

Repeatedly saying "it doesn't work" without any further details is useless. We can't run your code, we can't access your computer, we can't see your screen, and we can't read your mind. You need to explain precisely what the problem is.
Inczu 31-Oct-23 9:36am    
When I select a Item from listbox, write wrong password and click on button 1 and 2 times then I have messagebox from code in button that user have 3 times to write correct password or it will block button and password textbox for 10 seconds. The code is blocking that what I want 10 seconds and at the end, user have a next message box from button that informs unlock password textbox and button but it didn't count the 10 seconds in another messagebox till the blocking starts.
[no name] 31-Oct-23 14:03pm    
You're displaying a message box while the timer is still active. The event handler will "re-enter" with each interval "tick". MessageBox is "modal"; it is not async. You need a (non-modal) "window" or "progress bar".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900