我的应用程序中有两个多行文本框和一个箭头按钮,我想要的是当用户从多行文本框 1 中选择任何一行或多行时,它应该从 0 更新该行的状态到 1 然后我想将状态为 1 的行加载到多行文本框 2。我已经尝试但不知道下一步该怎么做?
代码:
for (int i = 0; i < txtNewURLs.Lines.Length; i++)
{
if (txtNewURLs.Lines[i].Select)
{
}
}
任何人都可以帮助我或提出一些建议来完成这项任务吗?
最佳答案
假设您正在使用类似于 MSDNS 的 How to: Create a Multiline TextBox Control 的多行文本框, 你可以利用 SelectedText属性来检索用户选择的文本。这些行将由 \r\n
即
如果我有以下内容(在页面行之间):
测试0
测试1
我选择了 test0
和 test1
行,然后 SelectedText
将是 test0\r\ntest1
。
然后您可以拆分 \r\n
并检索每个选定的行。
// Retrieve selected lines
List<string> SelectedLines = Regex.Split(txtNewURLs.SelectedText, @"\r\n").ToList();
// Check for nothing, Regex.Split returns empty string when no text is inputted
if(SelectedLines.Count == 1) {
if(String.IsNullOrWhiteSpace(SelectedLines[0])) {
SelectedLines.Remove("");
}
}
// Retrieve all lines from textbox
List<string> AllLines = Regex.Split(txtNewURLs.Text, @"\r\n").ToList();
// Check for nothing, Regex.Split returns empty string when no text is inputted
if(AllLines.Count == 1) {
if(String.IsNullOrWhiteSpace(AllLines[0])) {
AllLines.Remove("");
}
}
string SelectedMessage = "The following lines have been selected";
int numSelected = 0;
// Find all selected lines
foreach(string IndividualLine in AllLines) {
if(SelectedLines.Any(a=>a.Equals(IndividualLine))) {
SelectedMessage += "\nLine #" + AllLines.FindIndex(a => a.Equals(IndividualLine));
// Assuming you store each line status in an List, change status to 1
LineStatus[AllLines.FindIndex(a => a.Equals(IndividualLine));] = 1;
numSelected++;
}
}
MessageBox.Show((numSelected > 0) ? SelectedMessage : "No lines selected.");
关于c# - 如何在 C# 中检查多行文本框中的选定文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15947788/