【发布时间】:2017-06-29 01:03:57
【问题描述】:
我做到了
private void richTextBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
MessageBox.Show("you got it!");
}
}
但我想要的是:
右键单击richTextBox 中的一行时,将该行视为项目,因此菜单命令仅对我右键单击的特定行生效。比如删除、粘贴、复制
如果我选择粘贴,它会将新文本粘贴到richTextBox 的底部(末端)。但是,如果我单击复制或删除,它将考虑到我右键单击的特定行。
为一行或一批行粘贴,并将它们作为行添加到richTextBox的底部(末端)。
这就是我今天将文本作为行添加到richTextBox 的方式。线条是链接。 richTextBox 中的每一行都是一个链接。我想粘贴到richTextBox 的只是链接,而不仅仅是文本。因此,我粘贴到richTextBox 的每个链接都应该像我正在做的那样添加:for 循环仅适用于构造函数。
for (int i = 0; i < lines.Count; i++)
{
RichTextBoxExtensions.AppendText(richTextBox1, "Ready: ", Color.Red, 8.25f);
richTextBox1.AppendText(lines[i] + (i < lines.Count - 1 ? Environment.NewLine : String.Empty));
}
richTextBox1.AppendText(Environment.NewLine);
for (int i = 0; i < newList.Count; i++)
{
RichTextBoxExtensions.AppendText(richTextBox1, "Ready: ", Color.Red, 8.25f);
richTextBox1.AppendText(newList[i] + (i < newList.Count - 1 ? Environment.NewLine : String.Empty));
}
lines 和 newList 是列表
这只是我如何将链接添加到richTextBox 的一个示例。 因此,当我粘贴链接或链接时,它们应该像我正在做的那样以这种方式添加。
这就是richTextBox 现在的样子,例如:
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101330&ir=true
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101330&ir=false
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101345&ir=true
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101345&ir=false
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101400&ir=true
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101400&ir=false
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101415&ir=true
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101415&ir=false
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101430&ir=true
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101430&ir=false
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101445&ir=true
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101445&ir=false
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101500&ir=true
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101500&ir=false
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101515&ir=true
如果我现在正在粘贴链接,例如:http://microsoft.com 现在,richTextBox 的内容将如下所示:
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101330&ir=true
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101330&ir=false
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101345&ir=true
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101345&ir=false
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101400&ir=true
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101400&ir=false
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101415&ir=true
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101415&ir=false
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101430&ir=true
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101430&ir=false
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101445&ir=true
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101445&ir=false
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101500&ir=true
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101500&ir=false
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101515&ir=true
Ready: http://www.microsoft.com
如果我粘贴多个链接,那么它会将链接添加到底部。
我认为这是从剪贴板追加文本的最快方法:
string newText = Clipboard.GetText();
richTextBox1.SelectionStart = richTextBox1.TextLength;
richTextBox1.SelectionLength = 0;
richTextBox1.SelectedText = newText;
但我希望它被添加到 RichTextBox 底部的末尾,并且我正在使用 Ready 的格式:
在什么情况下我应该这样做?如何在代码中添加上下文菜单并使用粘贴菜单?
更新
我现在尝试了这样的事情:
private void richTextBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
var startIndex = richTextBox1.Text.IndexOf("Ready:") + "Ready:".Length;
var length = richTextBox1.Text.IndexOf("Ready:", startIndex) - startIndex;
int index = richTextBox1.SelectionStart;
int line = richTextBox1.GetLineFromCharIndex(index);
var code = richTextBox1.Text.Substring(startIndex + index, length - line - 1);
label1.Text = code;
}
我尝试添加两行:
int index = richTextBox1.SelectionStart;
int line = richTextBox1.GetLineFromCharIndex(index);
当我单击一行时,我试图获取鼠标光标位置的这两行。所以它会解析鼠标所在的行文本,就像 listView 中的项目一样。
但子字符串 i 不正确。
如果我这样做:
private void richTextBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
var startIndex = richTextBox1.Text.IndexOf("Ready:") + "Ready:".Length;
var length = richTextBox1.Text.IndexOf("Ready:", startIndex) - startIndex;
var code = richTextBox1.Text.Substring(startIndex, length - 1);
label1.Text = code;
}
}
它会在 label1 中始终为我提供第一行链接。 而不是点击鼠标光标位置的那一行。 如果我单击第 7 行,那么我想在 label1 中查看第 7 行的整个文本。 如果我点击第 65 行,然后在 label1 中查看第 65 行的全文。
如果我单击项目,则与 listView 中的想法相同。
【问题讨论】:
-
尝试使用 GetCharFromPosition 提供鼠标位置,然后从该位置获取 GetLineFromCharIndex。