Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C#文本框中使用子字符串_C#_Textbox_Substring - Fatal编程技术网

在C#文本框中使用子字符串

在C#文本框中使用子字符串,c#,textbox,substring,C#,Textbox,Substring,我想用文本框旁边的标签显示剩余的字符,以防用户输入超过字符的最大长度 这是我剩余字符的代码: private void txtID_TextChanged(object sender, EventArgs e) { int MaxChars = 10 - txtID.Text.Length; labelChars.Text = MaxChars.ToString() + " characters remaining."; } 我现在想做的是防止用

我想用文本框旁边的标签显示剩余的字符,以防用户输入超过字符的最大长度

这是我剩余字符的代码:

private void txtID_TextChanged(object sender, EventArgs e)
    {
        int MaxChars = 10 - txtID.Text.Length;
        labelChars.Text = MaxChars.ToString() + " characters remaining.";
    }

我现在想做的是防止用户在我的文本框中输入字符,因为他/她已经超过了字符限制。我计划使用文本框的子字符串属性,但我不知道如何使用。帮帮我?谢谢。

您应该使用MaxLength属性

您只需设置文本框的属性,它就可以为您设置该属性。

您可以使用MaxLength属性吗

您知道该属性吗?

这在客户端会更好。使用textbox的MaxLength属性和一些javascript来保持lblChars控件的功能

<asp:TextBox MaxLength="10" runat="server" ID="txtID" ClientIDMode="Static" onkeydown="changedID();"></asp:TextBox>
<asp:Label ClientIDMode="Static" ID="lblChars" runat="server"></asp:Label>
<script type="text/javascript">
    function changedID() {
        var t = document.getElementById("txtID");
        var l = document.getElementById("lblChars");
        l.innerText = (10 - t.value.length) + " characters remaining.";
    }
</script>

函数changedID(){
var t=document.getElementById(“txtID”);
var l=document.getElementById(“lblChars”);
l、 innerText=(10-t.value.length)+“剩余字符。”;
}