2016-01-08 52 views
0

我有一個.resx文件包含字符串名稱值對。現在我想使用C#(Windows窗體)實用地將名稱和值對變爲List。我怎樣才能做到這一點。但是這裏列出了價值成就的一個轉折點,我有一個「組合框」和兩個文本框。在運行時,應將所有密鑰添加到組合框中,並自動將其他兩個測試框填入值和註釋。請幫助我完成這項任務。 在此先感謝...從.resx文件中的資源讀取鍵值,值和註釋

+0

你是什麼意思與「列表中的我有一個‘組合框’和兩個文本框」?請澄清你的問題,如果可能的話發佈一些代碼。 :) – LucaMus

+0

嗨@LucaMus我想追加所有的鍵到組合框中,並對應每個鍵值和註釋出現在resx文件中。我希望當我通過comboBox選擇一個鍵時,該鍵的值和註釋會自動出現在帶有值的textBox1和帶有通信的textbox2中 – VIVEK

回答

1

看看ResXResourceReader,這可以很容易地做你想做的事情。

例如,你可以這樣做:

private void Form1_Load(object sender, EventArgs e) 
    { 
     //ComboBox will use "Name" property of the items you add 
     comboBox1.DisplayMember = "Name"; 
     //Create the reader for your resx file 
     ResXResourceReader reader = new ResXResourceReader("C:\\your\\file.resx"); 
     //Set property to use ResXDataNodes in object ([see MSDN][2]) 
     reader.UseResXDataNodes = true; 
     IDictionaryEnumerator enumerator = reader.GetEnumerator(); 

     while (enumerator.MoveNext()) 
     { //Fill the combobox with all key/value pairs 
      comboBox1.Items.Add(enumerator.Value); 
     } 
    } 

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     if (comboBox1.SelectedIndex == -1) 
      return; 

     //Assembly is used to read resource value 
     Assembly currentAssembly = Assembly.GetExecutingAssembly(); 
     //Current resource selected in ComboBox 
     ResXDataNode node = (ResXDataNode)comboBox1.SelectedItem; 

     //textBox2 contains the resource comment 
     textBox2.Text = node.Comment; 
     //Reading resource value, you can probably find a smarter way to achieve this, but I don't know it 
     object value = node.GetValue(new AssemblyName[] { currentAssembly.GetName() }); 
     if (value.GetType() != typeof(String)) 
     { //Resource isn't of string type 
      textBox1.Text = ""; 
      return; 
     } 

     //Writing string value in textBox1 
     textBox1.Text = (String)value; 
    } 
相關問題