如何设置文本框的最小限制 vb.net [英] how to set minimum limit of a textbox vb.net

查看:29
本文介绍了如何设置文本框的最小限制 vb.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为可以在文本框中使用的密码设置最小长度,然后设置一个标签,说明它是否满足所需的最少字符数.我知道如何设置字符限制,我不能做的是离开文本框后它会立即显示在标签中的部分.我在想我需要使用一个事件,比如 Leave 或 LostFocus,但它不起作用.请帮忙:(

I need to set a minimum length for a password that can be used in a textbox and then set a label which will say if it meets the minimum number of characters required. I know how to set the limit of characters, what I can't do is the part where it will show in a label as soon as I leave the textbox. I was thinking I need to use an event, like maybe Leave or LostFocus, but it's not working. Please help :(

推荐答案

好的,有很多方法可以实现您想要实现的目标.我个人喜欢单独的子程序;如果您需要更改一件事,则不必编辑具有相同代码的每个事件

据我所知,这样的事情应该可以帮助您继续前进.
基本上,我们只需设置一个子例程来检查 textbox1.text 的长度是否超过 5,然后我们通过使用诸如 button click 的事件来触发子例程,如果 textbox 是点击关闭.

Ok, There are plenty of ways to do what you want to achieve. I personally like to a separate subroutine; if you need to change one thing, you wont have to edit every single event that has the same code

From what I can understand, something like this should help get you on your way.
Basically, we just setup a subroutine that will check to see if textbox1.text's length is more than five and we trigger the subroutine by using events such as a button click of if the textbox is clicked off.

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click ''save button
    checkPassword(TextBox1.Text)
    End Sub

    Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles TextBox1.Leave
    checkPassword(TextBox1.Text)
    End Sub

    Private Sub checkPassword(password As String)
    If Not password.Length > 5 Then
        Label1.Text = "The password must be more than 5 charcharacters"
        TextBox1.Clear()
    Else
        Label1.Text = "Password accepted"
    End If
    End Sub

这篇关于如何设置文本框的最小限制 vb.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆