文本框文本“替换"功能不好用 [英] TextBox text "Replace" function does not work well
问题描述
我已经在这里查看了类似的问题,但没有帮助.
I have already looked at similar questions here but it did not help.
我正在使用 Windows 窗体.我有 button1 和 textbox1.
I am using windows forms. I have button1 and textbox1.
我正在尝试替换(或删除)textBox1 中选定的文本并输入新字母(字母 A)代替它.
I am trying to replace (or delete) the selected text in textBox1 and enter new letter (letter A) in place of it.
该代码适用于随机混合数字和字母例如:
The code works well with random mixed numbers and letters for example:
385F1选择8然后result = 3A5F1(8替换为A)
385F1 select 8 and then result = 3A5F1 (8 replaced by A)
H74S31B 选择 4S 然后结果是 = H7A31B
H74S31B select 4S and then the result is = H7A31B
KQ5689 选择 Q5689 则结果为 KA
KQ5689 select Q5689 and then the result is KA
所以它运行良好,但是当我从由相同数字或字母组成的字符串中选择一个数字或一个字母时,它不起作用,例如:
So it works well, but when I select a number or a letter from a string which consists of same numbers or letters then it does not work, for example:
666777222333 选择任意 7 则结果 = 666AAA222333(不是工作)
666777222333 select any 7 then the result = 666AAA222333 (not working)
9992244GG 选择任意 9 则结果 = AAA2244GG(不起作用)
9992244GG select any 9 then the result = AAA2244GG (not working)
QQQHHHUUU 选择任意 Q 然后结果 = AAAHHHUUU(不工作)
QQQHHHUUU select any Q then the result = AAAHHHUUU (not working)
QQQHHHUUU 选择任意 QH 然后结果 = QQAHHUUU(当选择不同的字母)
QQQHHHUUU select any QH then the result = QQAHHUUU(it works when different letters selected)
4433366 选择 333 然后结果 = 44A66数字被选中)
4433366 select 333 then the result = 44A66 (it works when all same numbers is selected)
希望我解释得很好.我不知道是什么导致了这种行为.请帮忙.谢谢
Hope I explained it well. I don't know what causes this behavior. please help. Thank you
public partial class Form1 : Form
{
int TxTindex;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
ActiveControl = textBox1;
textBox1.Focus();
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Focus();
if (textBox1.SelectedText.Length > 0) // to check if any text selected
{
TxTindex = textBox1.SelectionStart; // save the caret position
textBox1.Text = textBox1.Text.Replace(textBox1.Text.Substring(textBox1.SelectionStart, textBox1.SelectionLength),"A");
textBox1.SelectionStart = TxTindex + 1; // place the caret after the inserted string
}
else
{
return;
}
}
}
推荐答案
你的问题在这里:
textBox1.Text = textBox1.Text.Replace(textBox1.Text.Substring(textBox1.SelectionStart, textBox1.SelectionLength),"A");
Substring 函数返回字符串.在您的示例中(666777222333 选择任意 7 然后结果 = 666AAA222333(不工作)),它返回7".但是 Text.Replace 将替换所有出现的 7.这不是您想要的.你可以做的是,不要使用 string.Replace
函数,而是使用 string.Remove
和 string.Insert
The Substring function returns string. In your example (666777222333 select any 7 then the result = 666AAA222333 (not working)), it returns "7". But Text.Replace will replace all occurrences of 7. That is not what you want. What you can do is, instead of using string.Replace
function, use string.Remove
and string.Insert
textBox1.Text = textBox1.Text.Remove(textBox1.SelectionStart, textBox1.SelectionLength).Insert(textBox1.SelectionStart, "A");
但这对于大字符串可能不是很有效.StringBuilder
会更好.
But this might not be very efficient for large strings. A StringBuilder
would be better.
这篇关于文本框文本“替换"功能不好用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!