我有以下List(Of String)()
buttons=10
button_exit=10
button_x=600
button_y=400
;
button_text_01=Start
button_program_01=Start.exe
button_bckcolor_01=&H0080FFFF
;
button_text_02=Second
button_program_02=Second.exe
button_bckcolor_02=&H00FFFF80
;
button_text_03=Load
button_program_03=Load.exe
button_bckcolor_03=&H0080C0FF
我希望在新列表中添加一些值,例如:
Column1 Column2
------ -----
Start Start.exe
Second Second.exe
Load Load.exe
因此,我需要的唯一值是button_text_xx
和button_program_xx
。有什么想法吗?
下面是我的错误代码尝试。
If listSCMenuSection.Count > 0 Then
For Each item In listSCMenuSection
' MsgBox(listSCMenuSection.IndexOf(item).ToString)
If item.Contains("button_caption_") Then
dtButtons.Rows.Add(item.ToString, 0)
End If
Next
'Loop through
For Each item In listSCMenuSection
If item.Contains("button_program_") Then
Dim index As Integer = -1
Dim sIndex As String
For i As Integer = 0 To dtButtons.Rows.Count
If i.ToString.Count < 10 Then
sIndex = "0" & i.ToString
Else
sIndex = i.ToString
End If
Dim rows As DataRow() = dtButtons.[Select]("Name Like '%" & sIndex & "%'")
If rows.Count = 1 Then
index = dtButtons.Rows.IndexOf(rows(0))
'Add the button_program to the corresponding index of button_caption
dtButtons.Rows(index)(1) = item.ToString
ElseIf rows.Count > 1 Then
MessageBox.Show("You have more than one buttons declared in the ini file for: " & item.ToString)
Exit Sub
End If
Next
'If listSCMenuSection.IndexOf(item).ToString.Count < 10 Then
' sIndex = "0" & listSCMenuSection.IndexOf(item).ToString
'Else
' sIndex = listSCMenuSection.IndexOf(item).ToString
'End If
End If
Next
Dim x As Integer = 0
Else
MessageBox.Show("Please configure the 'MENU' section in the Ini file", "Section keys missing", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End If
答案 0 :(得分:3)
您想要解析字符串列表中的数据。它似乎是从文本文件中加载的,格式类似于ini文件。行"buttons=X"
定义了X按钮。
一种方法如下:
button_n
这可以这样实现:
using System;
using System.Linq;
using System.Collections.Generic;
public class ButtonEntry
{
public int Number { get; set; }
public string Text { get; set; }
public string Program { get; set; }
public string BackgroundColor { get; set; }
}
public class Program
{
public static void Main()
{
// Load data...
var data = new List<string>
{
"buttons=2",
";",
"button_text_01=a",
"button_text_02=b",
"button_program_02=p",
"button_bckcolor_02=c",
};
// Find number of buttons.
string buttonString = ReadData(data, "buttons");
int buttonCount = int.Parse(buttonString);
var buttons = new List<ButtonEntry>();
// Fill each button.
for (int b = 1; b <= buttonCount; b++)
{
var button = new ButtonEntry
{
Number = b
};
string needle = "button_text_" + b.ToString("D2");
button.Text = ReadData(data, needle);
needle = "button_program_" + b.ToString("D2");
button.Program = ReadData(data, needle);
needle = "button_bckcolor_" + b.ToString("D2");
button.BackgroundColor = ReadData(data, needle);
buttons.Add(button);
}
foreach (var button in buttons)
{
Console.WriteLine("Button {0}: {1}, {2}, {3}", button.Number, button.Text, button.Program, button.BackgroundColor);
}
}
/// <summary>
/// Finds the line in <paramref name="data" /> that starts with
/// <paramref name="needle" /> followed by "=", returning the
/// value after "=" or null when not found.
/// </summary>
public static string ReadData(List<string> data, string needle)
{
var line = data.FirstOrDefault(s => s.StartsWith(needle));
if (line == null)
{
return null;
}
return line.Substring(line.IndexOf("=") + 1);
}
}
打印:
Button 1: a, ,
Button 2: b, p, c
这不会进行任何错误处理,并且它不能很好地执行,因为它每次都会循环整个列表以查找给定的条目,但是如果列表保持较小,则可以考虑&# 34;足够好&#34;。如果您不想要C#,那么您就不应该对此问题进行标记。