我有一个代码将richtextbox中的符号替换为表情符号,这是代码:
private void add_smileys(RichTextBox addin)
{
try
{
while (addin.Text.Contains(":)"))
{
addin.SelectionStart = addin.Find(":)", RichTextBoxFinds.WholeWord);
addin.SelectionLength = 2;
Image img = Resources.in_smile;
Clipboard.SetImage(img);
addin.Paste();
}
while (addin.Text.Contains(">:("))
{
addin.SelectionStart = addin.Find(">:(", RichTextBoxFinds.WholeWord);
addin.SelectionLength = 3;
Image img = Resources.in_angry;
Clipboard.SetImage(img);
addin.Paste();
}
while (addin.Text.Contains(":D"))
{
addin.SelectionStart = addin.Find(":D", RichTextBoxFinds.WholeWord);
addin.SelectionLength = 2;
Image img = Resources.in_lol;
Clipboard.SetImage(img);
addin.Paste();
}
while (addin.Text.Contains(":'("))
{
addin.SelectionStart = addin.Find(":'(", RichTextBoxFinds.WholeWord);
addin.SelectionLength = 3;
Image img = Resources.in_cry;
Clipboard.SetImage(img);
addin.Paste();
}
while (addin.Text.Contains(":("))
{
addin.SelectionStart = addin.Find(":(", RichTextBoxFinds.WholeWord);
addin.SelectionLength = 2;
Image img = Resources.in_sad;
Clipboard.SetImage(img);
addin.Paste();
}
while (addin.Text.Contains(";)"))
{
addin.SelectionStart = addin.Find(";)", RichTextBoxFinds.WholeWord);
addin.SelectionLength = 2;
Image img = Resources.in_wink;
Clipboard.SetImage(img);
addin.Paste();
}
while (addin.Text.Contains("xD"))
{
addin.SelectionStart = addin.Find("xD", RichTextBoxFinds.WholeWord);
addin.SelectionLength = 2;
Image img = Resources.in_laugh;
Clipboard.SetImage(img);
addin.Paste();
}
while (addin.Text.Contains(":P"))
{
addin.SelectionStart = addin.Find(":P", RichTextBoxFinds.WholeWord);
addin.SelectionLength = 2;
Image img = Resources.in_tongue;
Clipboard.SetImage(img);
addin.Paste();
}
while (addin.Text.Contains(":|"))
{
addin.SelectionStart = addin.Find(":|", RichTextBoxFinds.WholeWord);
addin.SelectionLength = 2;
Image img = Resources.in_neutral;
Clipboard.SetImage(img);
addin.Paste();
}
while (addin.Text.Contains("^^"))
{
addin.SelectionStart = addin.Find("^^", RichTextBoxFinds.WholeWord);
addin.SelectionLength = 2;
Image img = Resources.in_happy;
Clipboard.SetImage(img);
addin.Paste();
}
while (addin.Text.Contains("o.O"))
{
addin.SelectionStart = addin.Find("o.O", RichTextBoxFinds.WholeWord);
addin.SelectionLength = 3;
Image img = Resources.in_dizzy;
Clipboard.SetImage(img);
addin.Paste();
}
while (addin.Text.Contains(":S"))
{
addin.SelectionStart = addin.Find(":S", RichTextBoxFinds.WholeWord);
addin.SelectionLength = 2;
Image img = Resources.in_confused;
Clipboard.SetImage(img);
addin.Paste();
}
while (addin.Text.Contains(":O"))
{
addin.SelectionStart = addin.Find(":O", RichTextBoxFinds.WholeWord);
addin.SelectionLength = 2;
Image img = Resources.in_omg;
Clipboard.SetImage(img);
addin.Paste();
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
这是输出:
如你所见,有些符号被笑脸取代而有些符号则没有。而且,我得到了这个例外:
此错误的原因是什么?我该如何解决?
答案 0 :(得分:2)
我收到此例外:在此处输入图片说明
此错误的原因是什么?以及如何修复它?
当Find
方法查找的字符串不存在时,-1
方法返回addin.SelectionStart
。此值对Find
无效。
在尝试使用该值之前,您应首先检查以确保-1
的结果大于var selectionStart = addin.Find(":)", RichTextBoxFinds.WholeWord);
if (selectionStart > -1)
{
addin.SelectionStart = selectionStart;
addin.SelectionLength = 2;
}
方法。
Find
您的包含和查找检查是多余的。出于某种原因,当你到达" ^^"他们返回不同的结果,这就是你得到错误的原因。但要删除冗余,您应该在循环中使用Contains
方法而不是while (true)
{
var selectionStart = addin.Find(":)", RichTextBoxFinds.WholeWord);
if (selectionStart > -1)
{
addin.SelectionStart = selectionStart;
addin.SelectionLength = 2;
}
else
{
break;
}
}
。
Dictionary<string, Image>
我也认为你的方法不是很干净。您可以使用@
以更简单,更易于维护的方式完成所有替换。您还应该使用private void add_smileys(RichTextBox addin)
{
var smileys = new Dictionary<string, Image>()
{
{ @":)", Resources.in_smile },
{ @">:(", Resources.in_angry },
{ @":D", Resources.in_lol },
{ @":'(", Resources.in_cry },
{ @":(", Resources.in_sad },
{ @";)", Resources.in_wink },
{ @"xD", Resources.in_laugh },
{ @":P", Resources.in_tongue },
{ @":|", Resources.in_neutral },
{ @"^^", Resources.in_happy },
{ @"o.O", Resources.in_dizzy },
{ @":S", Resources.in_confused },
{ @":O", Resources.in_omg },
};
foreach (var smiley in smileys)
{
add_smiley(addin, smiley.Key, smiley.Value);
}
}
private void add_smiley(RichTextBox addin, string token, Image smiley)
{
while (true)
{
var selectionStart = addin.Find(token, RichTextBoxFinds.WholeWord);
if (selectionStart < 0) break;
try
{
addin.SelectionStart = selectionStart;
addin.SelectionLength = token.Length;
Clipboard.SetImage(smiley);
addin.Paste();
}
catch (Exception ex)
{
MessageBox.Show(e.Message);
break;
}
}
}
符号来逃避字符串。
{{1}}