vb.net 当有人在 txtusername 中输入一个 2-20 个字符的单词时,我希望 progressbar1 为 =100

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28588179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:49:02  来源:igfitidea点击:

When someone types in a word with 2-20 characters in into txtusername I want progressbar1 to =100

vb.netstring

提问by ClapTrap1231

I can make the progress bar equal 100if someone enters a specific word into txtUsernameby writing:

100如果有人通过以下方式输入特定单词,我可以使进度条相等txtUsername

Dim word1 As String = "14TSmith"

If txtUsername = word1 Then 
    progressbar1.value = 100
End If

(sorry I can't copy and paste the exact code because it is on another system)

(对不起,我无法复制和粘贴确切的代码,因为它在另一个系统上)

But I want the user to type in any string that is between 2-20characters long, not just a specific word.

但我希望用户输入任何长度介于2-20字符之间的字符串,而不仅仅是特定的单词。

回答by TeeKea

If txtusername.TextLength >= 2 And txtusername.TextLength <= 20 Then
   ProgressBar1.Value = 100
End If

I hope this helps.

我希望这有帮助。

回答by Idle_Mind

If you want to give the user feedback of how "good" their password is based on the length, then:

如果您想根据长度向用户反馈他们的密码“好”程度,那么:

Public Class Form1

    Private Const MinLength As Integer = 2
    Private Const MaxLength As Integer = 20

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        TextBox1.MaxLength = MaxLength
    End Sub

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
        Dim Percent As Double
        If TextBox1.TextLength < MinLength Then
            Percent = 0
        Else
            Percent = CDbl(TextBox1.TextLength) / CDbl(MaxLength)
        End If
        Dim value As Integer = ProgressBar1.Minimum + (ProgressBar1.Maximum - ProgressBar1.Minimum) * Percent
        ProgressBar1.Value = Math.Max(Math.Min(value, ProgressBar1.Maximum), ProgressBar1.Minimum)
    End Sub

End Class