2014-12-22 40 views
2

所以我尝试了一些研究,但我只是不知道如何google一下..C# - 读一个复杂的文件转换成一个ComboBox

例如,我有一个.db的(作品一样为.TXT我)文件,这样写的:

DIRT: 3; 
STONE: 6; 

到目前为止,我有一个代码,可以把物品在这样的组合框:

DIRT, 
STONE, 

会把灰尘和石头的组合框。这是我正在使用的代码:

 string[] lineOfContents = System.IO.File.ReadAllLines(dbfile); 
     foreach (var line in lineOfContents) 
     { 
      string[] tokens = line.Split(','); 
      comboBox1.Items.Add(tokens[0]); 
     } 

如何扩展此以便它将例如DIRT和STONE在组合框中,并将其余(3)保留在变量中(int,如int varDIRT = 3)? 如果你想,它不一定是txt或db文件..我听说xml也是配置文件。

+0

尝试创建一个类来保存该信息。拆分逗号是没有意义的,因为在您的示例数据中,您没有显示任何逗号。你的意思是:冒号? – LarsTech

+0

'ComboBox'有两个属性可以使用。一个是“ValueMember”,另一个是“DisplayMember”。例如,你可以将你的数据库文件读入一个包含两列,'element'和'value'等的表格。然后将'ValueMember'设置为'value'并将'DisplayMember'设置为'element'。当然,您还必须确保将“ComboBox”的数据源设置为包含您的数据的表格。 –

+0

@LarsTech在我之前的DB文件中,我有逗号,那就是为什么它仍然存在。对于新的,我必须使用分号。这让我想到了一个想法:如果我在双点上有线问题,我还能读出线的其余部分吗? – devRicher

回答

2

试着做这样的事情:

  cmb.DataSource = File.ReadAllLines("filePath").Select(d => new 
      { 
       Name = d.Split(',').First(), 
       Value = Convert.ToInt32(d.Split(',').Last().Replace(";","")) 
      }).ToList(); 
      cmb.DisplayMember = "Name"; 
      cmb.ValueMember= "Value"; 

记住它需要使用using System.Linq; 如果您想OT引用组合框的设定值,你可以使用 cmb.SelectedValue; cmb.SelectedText;

+0

我得到一个错误... Forms.ComboBox不包含'DataMember'的定义,也没有扩展方法'DataMember'等等。价值相同。 – devRicher

+0

错误代码,但已更正尝试 – Patrick

+0

OP发布的示例数据不是用逗号分隔的。你会想要使用':'进行分割并删除';'在转换值之前。 – jbriggs

0

你也可以使用FileHelpers库。首先定义你的数据记录。

[DelimitedRecord(":")] 
public class Record 
{ 
    public string Name; 
    [FieldTrim(TrimMode.Right,';')] 
    public int Value;  
} 

然后你在你的数据像这样写着:

FileHelperEngine engine = new FileHelperEngine(typeof(Record)); 
//Read from file 
Record[] res = engine.ReadFile("FileIn.txt") as Record[]; 

// write to file 
engine.WriteFile("FileOut.txt", res); 
1

我觉得你真的有两个问题,所以我会尽力单独回答。

的第一个问题是“我怎样才能解析看起来像这样一个文件...

DIRT: 3; 
STONE: 6; 

为名称和整数?”您可以从每行删除所有空白和分号,然后拆分冒号。一个清洁的方式,在我看来,是使用正则表达式:

// load your file 
var fileLines = new[] 
{ 
    "DIRT: 3;", 
    "STONE: 6;" 
}; 

// This regular expression will match anything that 
// begins with some letters, then has a colon followed 
// by optional whitespace ending in a number and a semicolon. 
var regex = new Regex(@"(\w+):\s*([0-9])+;", RegexOptions.Compiled); 
foreach (var line in fileLines) 
{ 
    // Puts the tokens into an array. 
    // The zeroth token will be the entire matching string. 
    // Further tokens will be the contents of the parentheses in the expression. 
    var tokens = regex.Match(line).Groups; 
    // This is the name from the line, i.e "DIRT" or "STONE" 
    var name = tokens[1].Value; 
    // This is the numerical value from the same line. 
    var value = int.Parse(tokens[2].Value); 
} 

如果你不熟悉正则表达式,我建议你检查出来;他们可以很容易地格式化字符串并提取值。 http://regexone.com/

第二个问题,“我如何将价值与名称一起存储?”,我不确定自己完全理解。如果你想要做的是用文件中指定的数值返回每个项目,那么的建议对你有好处。您需要将名称作为显示成员,作为值成员。但是,由于您的数据不在表格中,因此您必须将数据放置在可访问的位置,以便您可以对要使用的属性进行命名。我推荐一本字典:

 // This is your ComboBox. 
     var comboBox = new ComboBox(); 

     // load your file 
     var fileLines = new[] 
     { 
      "DIRT: 3;", 
      "STONE: 6;" 
     }; 

     // This regular expression will match anything that 
     // begins with some letters, then has a colon followed 
     // by optional whitespace ending in a number and a semicolon. 
     var regex = new Regex(@"(\w+):\s*([0-9])+;", RegexOptions.Compiled); 

     // This does the same as the foreach loop did, but it puts the results into a dictionary. 
     var dictionary = fileLines.Select(line => regex.Match(line).Groups) 
      .ToDictionary(tokens => tokens[1].Value, tokens => int.Parse(tokens[2].Value)); 

     // When you enumerate a dictionary, you get the entries as KeyValuePair objects. 
     foreach (var kvp in dictionary) comboBox.Items.Add(kvp); 

     // DisplayMember and ValueMember need to be set to 
     // the names of usable properties on the item type. 
     // KeyValue pair has "Key" and "Value" properties. 
     comboBox.DisplayMember = "Key"; 
     comboBox.ValueMember = "Value"; 

在这个版本中,我用Linq来构造字典。如果您不喜欢Linq语法,则可以使用循环代替:

var dictionary = new Dictionary<string, int>(); 
foreach (var line in fileLines) 
{ 
    var tokens = regex.Match(line).Groups; 
    dictionary.Add(tokens[1].Value, int.Parse(tokens[2].Value)); 
} 
+0

中使用“:”整齐,但对于“第一个问题”,在分割后如何读取文件的其余部分? – devRicher

+0

@devRicher我写它的方式是一行一行地工作,所以你仍然必须按照原来的方式来执行ReadAllLines,以便在我的示例中获取行到fileLines数组中。 – JwameeQohwiye

+0

@devRicher如果你想让你的文件包含其他不在DIRT中的东西:3;格式,那么你需要使用正则表达式来测试行*是否匹配。如果是,那么你可以像我一样使用解析的令牌。如果不是,你会做别的。 – JwameeQohwiye

相关问题