c# - 如何在 C# 中检查多行文本框中的选定文本?

标签 c# .net select textbox

我的应用程序中有两个多行文本框和一个箭头按钮,我想要的是当用户从多行文本框 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


我选择了 test0test1 行,然后 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/

相关文章:

C#创建从基类继承的泛型类的实例

c# - Web API 路由问题

c# - 正则表达式 - 获取所有未用 "/"包裹的单词

.net - 在 MSChart 中对齐主轴和副轴

c# - 使用生成的 Golang DLL 返回字符串或 *C.Char

c# - 当指针指向项目时如何在gridview中指定项目的样式

c# - Visual Studio 2017 无法加载项目

mysql - 如何在 MySQL 的 instr() 中使用区分大小写?

html - 选择元素时如何更改选择元素的背景颜色?

sql - 选择具有不同名称 SQL SERVER 的 TOP 2