在文本框中输入相同的 text/string 时如何获得另一个输出?

how to get another output when same text/string is entered in textbox?

当在文本框中输入相同的 "ID" 时,应创建另一个输出。

例如。 当我输入 123、124、125(分别)时,它们输出“成功登录!”,当我输入 123 或已经输入的 "IDs" 中的任何一个时,它会给我这个输出 "Success Logout!" .

我试过将它存储到一个变量中 -- public static String StoreUserID;但我认为这是错误的方法,因为我将使用 textbox.Clear();所以当我不输入任何内容时它输出 "user logged out" :(


public static String StoreUserID;
///
private void IDTextBox_KeyDown(object sender, KeyEventArgs e)
{
 if (e.KeyCode == Keys.Enter)
            {
                if (IDTextBox.Text != "")
                {
                    FlashMessage.Text = IDTextBox.Text  + " Success Login! " + Environment.NewLine + DateTime.Now.ToString();
                    IDTextBox.Clear();
                }
                else if (StoreUserID == IDTextBox.Text)
                {
                    FlashMessage.Text = IDTextBox.Text + " Success Logout! " + Environment.NewLine + DateTime.Now.ToString();
                }
                else
                {
                    FlashMessage.Text = "No ID Entered";
                }
}

我希望它在输入相同的 "ID" 时输出另一条消息,但它只在文本框中没有文本时输出 "Success Logout!"。

您错过了一件重要的事情:您不记得谁登录了谁没有登录,因为您从未设置 StoreUserID 变量。因为您的程序似乎一次只能记住一个人,这对 DTR 程序不是很有用,所以我将其更改为字典。字典就像一个数组,但由字符串而不是整数索引,它为我们提供了一种在登录时存储的方法:

    private Dictionary<string, DateTime> _loggedInUsers = new Dictionary<string, DateTime>(); //need to add this at top of file:  using System.Collections.Generic;

    private void IDTextBox_KeyDown(object sender, KeyEventArgs e)
    {
         if (e.KeyCode == Keys.Enter)
         {
                if (string.IsNullOrWhiteSpace(IDTextBox.Text)) //usually better than comparing with ""
                {
                    FlashMessage.Text = "No ID Entered";
                    return;
                }

                var user = IDTextBox.Text.Trim(); //remove header/trailing spaces
                IDTextBox.Clear();                //might as well do this now

                if(_loggedInUsers.ContainsKey(user))
                {
                    //how long were they logged in?
                    TimeSpan ts = (DateTime.Now - _loggedInUsers[user]);
                    _loggedInUsers.Remove(user); //log them out
                    FlashMessage.Text = $"{user} successfully logged out at {DateTime.Now}, you were logged in for {ts.TotalHours} hours, which is also {ts.TotalSeconds}. You earned {ts.TotalHours * 200} at your rate of 200$/hr";
                }
                else 
                {   
                    _loggedInUsers[user] = DateTime.Now; //log them in at this time
                    FlashMessage.Text = $"{user} successfully logged in. Go do some work and then log out and I'll tell you how long you were";
                }
          }
    }

我还添加了一些其他内容,向您展示如何使用字符串插值,但最关键的部分是;当我们登录时,我们将用户放入字典中,并在他们注销时将其取出。该字符串与登录时间一起输入到字典中。该字符串稍后用于查找登录时间,并为我们提供了一种计算工作时间的方法。从字典中删除用户将他们注销;字典中用户 ID 的 "presence" 或 "not presence" 决定他们是登录(存在)还是注销(不存在)

我在那里放了一个 Trim() 命令,因为空格会产生不同的字符串。如果用户在登录时键入 123,而在 "logout" 上键入 123,他将再次登录,因为这些空格使这些用户变得不同。用户喜欢做这样的蠢事,我们必须尽可能防范他们