如何从文本框删除? [英] how to delet from textbox ?

查看:82
本文介绍了如何从文本框删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果在第8个字符输入框时文本框中有7个字符,我想删除previos字符.文本框的最大长度?
Textbox1.MaxLength = 7;
这样,将不允许用户键入超过7个字符.


处理TextBox.KeyPress事件.
如果发生这种情况,请检查现有文本,并在必要时进行修改.

private void myTextBox_KeyPress(object sender, KeyPressEventArgs e)
    {
    TextBox tb = sender as TextBox;
    if (tb != null)
        {
        string existing = tb.Text;
        if (existing.Length >= 7)
            {
            tb.Text = existing.Substring(existing.Length - 6);
            tb.SelectionStart = tb.Text.Length;
            }
        }
    }


我想您想删除所有之前的7个字符,可以使用波纹管

 如果(textBox1.Text.Length >   0  7 );
   textBox1.SelectionStart = textBox1.Text.Length;
} 


if there is 7 char in text box when the 8th one is enetered i want to delete the previos chars

解决方案

Instead of doing that, why not set the maximum length of the textbox?
Textbox1.MaxLength = 7;
That way, the user will not be allowed to key in more than 7 characters.


Handle the TextBox.KeyPress event.
In the event, check the existing text, and modify if necessary.

private void myTextBox_KeyPress(object sender, KeyPressEventArgs e)
    {
    TextBox tb = sender as TextBox;
    if (tb != null)
        {
        string existing = tb.Text;
        if (existing.Length >= 7)
            {
            tb.Text = existing.Substring(existing.Length - 6);
            tb.SelectionStart = tb.Text.Length;
            }
        }
    }


I think you want to remove all previous 7 chars, you can use bellow

if (textBox1.Text.Length > 7)
{
   textBox1.Text = textBox1.Text.Remove(0, 7);
   textBox1.SelectionStart = textBox1.Text.Length;
}


这篇关于如何从文本框删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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