简体   繁体   English

C# 如果满足条件,如何使用 RichTextBox 中的字符串填充 ComboBox

[英]C# How to populate ComboBox with Strings from RichTextBox if criteria met

I am trying to populate a combo box with lines from a rich text box.我试图用富文本框中的行填充组合框。 But only if the last 6 characters of a line in the rich text box contains the string "device".但前提是富文本框中一行的最后 6 个字符包含字符串“device”。 I do not know the amount of lines in the rich text box and the amount of lines that contain the string "device", until runtime.我不知道富文本框中的行数以及包含字符串“device”的行数,直到运行时。

Let's say there are 6 lines in the combo box and 2 items that contain the string "device".假设组合框中有 6 行和 2 个包含字符串“device”的项目。 But both numbers can and do change during runtime.但是这两个数字都可以而且确实会在运行时发生变化。

int IntCountLines is equal to the amount of lines in the rich text box. int IntCountLines 等于富文本框中的行数。 int IntNumberOfDevices is equal to the number of lines that contain the string "device" (in the last 6 characters) in the rich text box. int IntNumberOfDevices 等于富文本框中包含字符串“device”(最后 6 个字符)的行数。

The first line [0] in the rich text box is always ignored.富文本框中的第一行 [0] 始终被忽略。 So, starting at line [1].因此,从第 [1] 行开始。

If line 1 in the rich text box contains the string "device", I want to add it to the combo box.如果富文本框中的第 1 行包含字符串“device”,我想将其添加到组合框中。 If it does not, then move to line 2 and check that.如果没有,则移至第 2 行并检查。 If this contains the string "device" add it to the combo box.如果它包含字符串“device”,则将其添加到组合框中。 If it does not, then move to line 3 and so on.如果没有,则移至第 3 行,依此类推。

int IntCountLines int IntCountLines

int IntNumberOfDevices. int IntNumberOfDevices。

Richtextbox name is: RtxtAdbOutput. Richtextbox 名称是:RtxtAdbOutput。

ComboBox name is: CmbIPs.组合框名称为:CmbIPs。

I have:我有:

StrTmpOutput = rtxtAdbOutput.Lines[1].Substring(rtxtAdbOutput.Lines[1].Length - 6);

if (StrTmpOutput == "device")
{
    CmbIPs.Items.Add(rtxtAdbOutput.Lines[1].Remove(rtxtAdbOutput.Lines[1].Length - 7));
}



StrTmpOutput = rtxtAdbOutput.Lines[2].Substring(rtxtAdbOutput.Lines[2].Length - 6);

if (StrTmpOutput == "device")
{
    CmbIPs.Items.Add(rtxtAdbOutput.Lines[2].Remove(rtxtAdbOutput.Lines[2].Length - 7));
}


StrTmpOutput = rtxtAdbOutput.Lines[3].Substring(rtxtAdbOutput.Lines[3].Length - 6);

if (StrTmpOutput == "device")
{
    CmbIPs.Items.Add(rtxtAdbOutput.Lines[3].Remove(rtxtAdbOutput.Lines[3].Length - 7));
}

And so on.等等。 But do not know the amount of lines until runtime means I do not know how long to carry on adding if statements.但是直到运行时才知道行数意味着我不知道要继续添加 if 语句多长时间。 Plus, it gives errors if it is an empty line (as it tries to strip 7 characters from the end of a string that does not exist though I could put in some error checking to stop this).另外,如果它是一个空行,它会给出错误(因为它试图从不存在的字符串的末尾去除 7 个字符,尽管我可以进行一些错误检查来阻止它)。

Is there any way I can improve this with a for loop or similar?有什么办法可以用 for 循环或类似的方法来改进吗?

I want to keep doing this until the amount of lines added to the combobox matches the value of IntNumberOfDevices.我想继续这样做,直到添加到组合框的行数与 IntNumberOfDevices 的值匹配。

You can use a combination of the List<T>.ForEach() method and the String.EndsWith()您可以组合使用List<T>.ForEach()方法和String.EndsWith()

rtxtAdbOutput.Lines.ToList()
    .GetRange(1, rtxtAdbOutput.Lines.Count() - 1)
    .Where(line => line.EndsWith("device")).ToList()
    .ForEach(validLine => CmbIPs.Items.Add(validLine.Remove(validLine.Length - 7)));

What this does is get all of the lines with an index higher than 0 as a list, then selects the ones that end with "device" using String.EndsWith , then for each of those adds them to the combo box using ForEach() .这样做是将索引高于 0 的所有行作为列表,然后使用String.EndsWith选择以“device”结尾的行,然后使用ForEach()将它们添加到组合框中。

DotnetFiddle demonstrating it DotnetFiddle 演示它

String.EndsWith documentation String.EndsWith 文档

List.ForEach documentation List.ForEach 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM