简体   繁体   English

如何限制TextBox中的字符?

[英]How to limit characters in TextBox?

I have a button1 whose text is 0, the times i click on button the button text will appear in textbox the code is mention below : 我有一个button1,其文本为0,单击按钮时,按钮文本将出现在文本框中,代码如下:

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        TextBox8.Text = TextBox8.Text + "0"
        End Sub

My problem : 我的问题 :

i want to limit the characters print in the textbox after clicking the button1. 我想限制单击button1后在文本框中打印的字符。

I want if my textbox maximum length in 2 then after 2 charcters i click on button it will not print the text of button1 after two characters. 我想如果我的文本框的最大长度为2,那么在单击2个字符后,我将不会在两个字符后打印button1的文本。

You may try setting MaxLength and use it to check if current text length has reached maximum 您可以尝试设置MaxLength并使用它来检查当前文本长度是否达到最大

' code behind
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    If TextBox8.Text.Length < TextBox8.MaxLength Then
        TextBox8.Text = TextBox8.Text & "0"
    End If
End Sub

' aspx page
<form id="form1" runat="server">
   <asp:TextBox ID="TextBox8" MaxLength="2" runat="server"></asp:TextBox>
   <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</form>

If you could be a little bit more precise what you try to achieve, I could adapt my answer! 如果您可以更精确地达到目标,我可以调整答案!

Your question is a bit hard to follow, but are you looking for the TextBox.MaxLength property? 您的问题有点难以理解,但是您是否在寻找TextBox.MaxLength属性?

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox.maxlength.aspx http://msdn.microsoft.com/zh-CN/library/system.web.ui.webcontrols.textbox.maxlength.aspx

Or, you could just check the length in code: 或者,您可以只检查代码中的长度:

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
    If TextBox8.Text.Length < 3 Then TextBox8.Text = TextBox8.Text + "0"
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM