C# 为什么我没有得到我点击/选择的真实当前字符?
代码如下:C# 为什么我没有得到我点击/选择的真实当前字符?,c#,winforms,C#,Winforms,代码如下: private void richTextBox1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { if (mouseisup == false) { textBox1.Text = "";
private void richTextBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (mouseisup == false)
{
textBox1.Text = "";
int positionToSearch = richTextBox1.GetCharIndexFromPosition(new Point(e.X, e.Y));
richTextBox1.SelectionStart = positionToSearch;
textBox1.Text = richTextBox1.Text.Substring(positionToSearch, 1);
previousChar = positionToSearch;
textBox2.Text = "";
mouseisup = true;//add this statement
}
else
{
currentChar = richTextBox1.GetCharIndexFromPosition(new Point(e.X, e.Y));
if (currentChar > previousChar + 2 || currentChar < previousChar - 2)
{
richTextBox1.SelectionStart = currentChar;
textBox2.Text = richTextBox1.Text.Substring(currentChar, 1);
button1.Enabled = true;
}
mouseisup = false;
}
}
}
private void richTextBox1_MouseUp(object sender, MouseEventArgs e)
{
if (textBox2.Text == "")
{
mouseisup = true;
button1.Enabled = false;
}
}
private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
string[] text = Parse_Text.ParseText(richTextBox1.Text, textBox1.Text, textBox2.Text);
for (int i = 0; i < text.Length; i++)
{
richTextBox2.Text = text[i];
}
}
private void richTextBox1_MouseDown(对象发送方,MouseEventArgs e)
{
if(e.Button==MouseButtons.Left)
{
如果(mouseisup==false)
{
textBox1.Text=“”;
int positionToSearch=richTextBox1.GetCharIndexFromPosition(新点(e.X,e.Y));
richTextBox1.SelectionStart=positionToSearch;
textBox1.Text=richTextBox1.Text.Substring(位置搜索,1);
previousChar=位置搜索;
textBox2.Text=“”;
mouseisup=true;//添加此语句
}
其他的
{
currentChar=richTextBox1.GetCharIndexFromPosition(新点(e.X,e.Y));
如果(currentChar>previousChar+2 | | currentChar
我为测试创建了一个文本文件,并输入了文本文件:
“Daniel>较小><然后Daniel或Daniel取决于D>但是<”
然后在程序中,第一次点击第一个但以理的第一个D,第二次点击第二个但以理的第二个D
然后在代码中parse_text的新类中:
List<string> parsedText = new List<string>();
string[] entries = null;
int startIndex = text.IndexOf(startTag);
if (startIndex >= 0)
{
int endIndex = text.IndexOf(endTag, startIndex);
List parsedText=new List();
字符串[]项=null;
int startIndex=text.IndexOf(startTag);
如果(起始索引>=0)
{
int endIndex=text.IndexOf(endTag,startIndex);
startIndex是0,endIndex也是0。但是endTag是第二个D,它不应该是索引0。
所以这也是一个问题。你的问题是你对角色进行了索引,这确实会让你第一次出现。 要避免这种情况,请使用角色的位置而不是实际角色 您已在以下行中获得该信息:
currentChar = richTextBox1.GetCharIndexFromPosition(new Point(e.X, e.Y));
编辑
您应该更改Parse_Text方法以用于位置而不是字符。
在我的头上,你应该得到这样的东西:
public static string[] ParseText(string text, int startPos, int endPos)
{
List<string> parsedText = new List<string>();
string[] entries = null;
if (startPos >= 0 && endPos > startPos)
{
string images = text.Substring(startPos + 1, endPos - startPos - 1);
entries = images.Split(new[] { ',' });
for (var i = 0; i < entries.Length; i++)
{
entries[i] = entries[i].Replace("\"", "");
}
for (int i = 0; i < entries.Length; i++)
{
parsedText.Add(entries[i]);
}
}
return entries;
}
publicstaticstring[]ParseText(stringtext,intstartpos,intendpos)
{
List parsedText=新列表();
字符串[]项=null;
如果(startPos>=0&&endPos>startPos)
{
stringimages=text.Substring(startPos+1,endPos-startPos-1);
条目=images.Split(新[]{',});
对于(var i=0;i
显然,您应该添加一些关于允许参数的额外测试。
例如:
- 文本不能为字符串。为空
- startPos不能小于零
- endPos必须大于startPos
172
,这是Koen的位置。我刚刚用完整的代码更新了我的问题,并在我选择开始点和结束点后解析文本的类。parse_text类似乎没有问题。问题是在表格1中的某个地方。试着像我一样使用此文本。首先单击D,然后单击标题的<,当它们都出现在文本框中时,单击按钮对其进行解析,并看到它不会在两者之间获得整个文本。第一次在!和D之间单击,然后第二次在>和<之间单击,这就是我如何做的将整个文本放在中间。我看到了你的答案,那么我应该更改什么?这行不是取字符的位置吗?然后我使用这个变量作为字符的位置。用一个关于如何使用位置而不是字符的示例更新了我的答案。