2013-08-01 1 views
1

멀티 라인 TextBox이 있는데 마우스를 클릭하면 에 마우스를 통해 TextBox에서 선택한 텍스트를 추가해야합니다. 내가 마우스 왼쪽 클릭을 통해 TextBox 선택할 어떤 이미지에 나타낸 바와 같이이 질문에 enter image description here입력란의 텍스트를 마우스로 선택한 텍스트를 목록에 추가하는 방법

, 참조에 사진을 첨부

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

목록 정의, 그것은 추가됩니다한다 목록에

답변

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(); 
      } 
     } 
    } 
} 
+0

내 문제에 대해 많은 도움을주었습니다 ... – Deadlock

+0

안녕하세요. –

0

텍스트 상자의 이벤트 처리기에서 처리해야합니다.
private void textBox1_MouseUp(object sender, MouseEventArgs e){ 
    if(e.Button == MouseButtons.Left){ 
    if(textBox1.SelectedText.Length > 0) PtagName.Add(textBox1.SelectedText); 
    } 
} 
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(); 
} 
관련 문제