我有一个 multi-line TextBox
,我希望在释放鼠标左键后将TextBox
通过鼠标选择的文本添加到 a中。List
该列表定义为
public List<string> PtagName = new List<string>();
我附上了关于这个问题的图片
如图所示,无论我TextBox
通过鼠标左键单击选择什么,它都应该被添加到列表中
你去:希望它有帮助。
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace TextBoxLines
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public List<string> PtagName = new List<string>();
private void textBox1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (textBox1.SelectedText.Length > 0)
{
string[] lines = textBox1.SelectedText.Split('\n');
foreach (var line in lines)
{
PtagName.Add(line);
}
}
foreach (var line in PtagName)
MessageBox.Show(line);
PtagName.Clear();
}
}
}
}
您必须在MouseUp
文本框的事件处理程序中进行处理:
private void textBox1_MouseUp(object sender, MouseEventArgs e){
if(e.Button == MouseButtons.Left){
if(textBox1.SelectedText.Length > 0) PtagName.Add(textBox1.SelectedText);
}
}
处理 mouseup 事件并在那里执行所有任务。
public void mytextbox_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) {
if (mytextbox.SelectionLength == 0 || e.Button != MouseButtons.Left) return;
PtagName = mytextbox.SelectedText.Split(new [] { '\r', '\n' }).ToList();
}