【发布时间】:2020-10-17 02:48:42
【问题描述】:
我有一个只获取字节的文本框,我想在每个字节之间添加空格。
到目前为止我做了什么?
public int a=0;
public char c;
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
c = e.KeyChar;
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
a = textBox2.TextLength % 3;
if (c != 0x08 && a==2)
{
a = textBox2.TextLength % 3;
int selectionIndex = textBox2.SelectionStart;
textBox2.Text = textBox2.Text.Insert(selectionIndex, " ");
textBox2.SelectionStart = selectionIndex + 1; // restore cursor position
}
}
我用这段代码添加空间,但如果我删除一些字节,它不会增加空间。
我认为'if'条件存在一些问题。
例如我写'abcdef'它会增加空间并写
ab cd ef
然后我删除 'cdef' 和空格,它在这里正常工作。 我再次写'cdef'它不起作用,结果会是这样
abcde f
我该如何解决这个问题?
【问题讨论】:
-
我研究了这篇文章,但没有一个有效。
标签: c# textbox space textchanged