2

我有一个 multi-line TextBox,我希望在释放鼠标左键后将TextBox通过鼠标选择的文本添加到 a中。List

该列表定义为

 public List<string> PtagName = new List<string>();

我附上了关于这个问题的图片 在此处输入图像描述

如图所示,无论我TextBox通过鼠标左键单击选择什么,它都应该被添加到列表中

4

3 回答 3

3

你去:希望它有帮助。

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();
            }
        }
    }
}
于 2013-08-01T07:33:59.287 回答
0

您必须在MouseUp文本框的事件处理程序中进行处理:

private void textBox1_MouseUp(object sender, MouseEventArgs e){
  if(e.Button == MouseButtons.Left){
     if(textBox1.SelectedText.Length > 0) PtagName.Add(textBox1.SelectedText);
  }
}
于 2013-08-01T07:25:43.723 回答
0

处理 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(); 
}
于 2013-08-01T07:28:49.570 回答