开发者

Silverlight C# - How to copy line of text that is clicked in textbox?

I have a textbox that I break up into an array based on "\r"

I'd like to have the line of text that is clicked on put into a string.

For instance, if I click on the 2nd line below in my textbox,开发者_StackOverflow I'd like "2nd line" to be entered into the string

1st line

2nd line

last line

How would I go about doing that?


This is close, but I recommend selecting text between TextBox.SelectionStart and TextBox.SelectionEnd instead of what you are trying to do.

WPF, but it might be the same

MainWindow.xaml

<TextBox
  MouseDoubleClick="textBox1_MouseDoubleClick" />

MainWindow.xaml.cs

private void textBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
  int characterIndex = this.textBox1.SelectionStart;
  string characterIndexSubstring = this.textBox1.Text.Substring(0, characterIndex);
  int lineNumber = characterIndexSubstring.Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries).Length;
  string[] lines = textBox1.Text.Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
  string clickedOnValue = lines[lineNumber];
  MessageBox.Show(clickedOnValue);
}


This isn't really an answer as I ended up grabbing the string out of a datagrid I was populating with data out of the textbox referenced in this post.

Link to that solution is: Silverlight C# - How to get value of row/cell clicked on in DataGrid?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜