2017-10-19 201 views
0

案例:我的Windows窗體應用程序正在從XML文件創建按鈕和單選按鈕。對於每個創建的按鈕(預設爲XML),我想將選中的單選按鈕(XML中的Lijst)更改爲我的XML中描述的值。使用從xml文件生成的按鈕來設置單選按鈕值

我的想法:應該有一個點擊方法,它會讀取預設點擊的xml值並將單選按鈕更改爲正確的值。但我沒有線索如何做到這一點。

代碼的XML:

<Lijsten> 

    <Lijst> 
    <Titel>Discipline</Titel> 
    <Waardes>Elektro</Waardes> 
    <Waardes>Mechanisch</Waardes> 
    <Waardes>Civiel</Waardes> 
    <Waardes>Proces</Waardes> 
    <Waardes>N.v.t.</Waardes> 
    </Lijst> 
    <Lijst> 
    <Titel>Soort</Titel> 
    <Waardes>Tekening</Waardes> 
    <Waardes>Tekst doc</Waardes> 
    <Waardes>Afbeelding</Waardes> 
    <Waardes>N.v.t.</Waardes> 
    </Lijst> 

    <Preset> 
    <ButtonTitel>Preset 1</ButtonTitel> 
    <sets> 
     <RadioButtonTitel>Discipline</RadioButtonTitel> 
     <RadioButtonValue>Elektro</RadioButtonValue> 
    </sets> 
    <sets> 
     <RadioButtonTitel>Soort</RadioButtonTitel> 
     <RadioButtonValue>Afbeelding</RadioButtonValue> 
    </sets> 
    </Preset> 
    <Preset> 
    <ButtonTitel>Preset 2</ButtonTitel> 
    <sets> 
     <RadioButtonTitel>Discipline</RadioButtonTitel> 
     <RadioButtonValue>Mechanisch</RadioButtonValue> 
    </sets> 
    <sets> 
     <RadioButtonTitel>Soort</RadioButtonTitel> 
     <RadioButtonValue>Tekening</RadioButtonValue> 
    </sets> 
    </Preset> 

</Lijsten> 

代碼創建單選按鈕的:

foreach (XmlNode node in nodes) 
{ 
    radioButtonCounter += 1; 
    count += 1; 
    if (count < 4) 
    { 
     int heightRadioButtons = 0; 
     WidthPanelsRow1 += 155; 
     Panel panel = new Panel(); 
     panel.Size = new Size(140, 200); 
     panel.Location = new Point(WidthPanelsRow1, heightPanelsRow1); 
     panel.Name = "panel" + count.ToString(); 
     panel.BackColor = Color.LightGray; 

     Label lbl = new Label(); 
     lbl.Text = node["Titel"].InnerText; 
     lbl.Location = new Point(0, 0); 
     lbl.Font = font1; 
     panel.Controls.Add(lbl); 

     int counterLastRadioButton = 0; 
     XmlNodeList waardeNodes = node.SelectNodes("Waardes"); 
     foreach (XmlNode wNode in waardeNodes) 
     { 
       counterLastRadioButton += 1; 
       heightRadioButtons += 20; 
       RadioButton rb = new RadioButton(); 
       rb.Text = wNode.InnerText; 
       rb.Location = new Point(5, heightRadioButtons); 
       rb.Name = node["Titel"].InnerText; 
       if (waardeNodes.Count - 1 < counterLastRadioButton) 
       { 
        rb.Checked = true; 
       } 
       panel.Controls.Add(rb); 
     } 
     this.Controls.Add(panel); 
    }else... 
} 

代碼用於創建預設按鈕:

foreach (XmlNode node in nodes) 
{ 
    count += 1; 
    if (count < 4) 
    { 
      WidthPanelsRow1 += 155; 
      Panel panel = new Panel(); 
      panel.Size = new Size(140, 40); 
      panel.Location = new Point(WidthPanelsRow1, heightPanelsRow1); 
      panel.Name = "panel" + count.ToString(); 

      XmlNodeList titelPreset = node.SelectNodes("ButtonTitel"); 
      foreach (XmlNode titelNode in titelPreset) 
      { 
       Button btn = new Button(); 
       btn.Text = titelNode.InnerText; 
       btn.Location = new Point(0, 0); 
       btn.Name = node["ButtonTitel"].InnerText; 
       btn.Size = new Size(140,40); 
       btn.Click += (sender, args) => 
       { 
        //What to do here? 
       }; 
       panel.Controls.Add(btn); 
      } 
      this.Controls.Add(panel); 
     }else... 
} 

感謝。

+0

我喜歡創建一個Dictionary 。您可以通過標題查找控件,但速度很慢。字典會超快。 – jdweng

+0

我正在尋找快速的東西,因爲創建程序是爲了將數據分配給圖像,並將其寫入文本文件。 「預設」在那裏,所以如果「預設」是常見的,用戶不必更改9個單選按鈕值。但我沒有線索去做這件事。 – Niels

回答

0

嘗試使用詞典。見下面的代碼:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Xml; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     const string FILENAME = @"c:\temp\test.xml"; 

     public Dictionary<string, Dictionary<string, RadioButton>> radioPanels = new Dictionary<string, Dictionary<string, RadioButton>>(); 

     public Form1() 
     { 
      InitializeComponent(); 

      int radioButtonCounter = 0; 
      int count = 0; 
      int WidthPanelsRow1 = 100; 
      int heightPanelsRow1 = 50; 
      Font font1 = new Font(this.Font, FontStyle.Regular); 

      XmlDocument doc = new XmlDocument(); 
      doc.Load(FILENAME); 
      XmlNodeList nodes = doc.GetElementsByTagName("Lijst"); 

      Dictionary<string, RadioButton> dictRadioButtons; 
      foreach (XmlNode node in nodes) 
      { 
       radioButtonCounter += 1; 
       count += 1; 
       if (count < 4) 
       { 
        int heightRadioButtons = 0; 
        WidthPanelsRow1 += 155; 
        Panel panel = new Panel(); 
        panel.Size = new Size(140, 200); 
        panel.Location = new Point(WidthPanelsRow1, heightPanelsRow1); 
        panel.Name = "panel" + count.ToString(); 
        panel.BackColor = Color.LightGray; 

        Label lbl = new Label(); 
        lbl.Text = node["Titel"].InnerText; 
        lbl.Location = new Point(0, 0); 
        lbl.Font = font1; 
        panel.Controls.Add(lbl); 
        dictRadioButtons = new Dictionary<string, RadioButton>(); 
        radioPanels.Add(lbl.Text, dictRadioButtons); 

        int counterLastRadioButton = 0; 
        XmlNodeList waardeNodes = node.SelectNodes("Waardes"); 
        foreach (XmlNode wNode in waardeNodes) 
        { 
         counterLastRadioButton += 1; 
         heightRadioButtons += 20; 
         RadioButton rb = new RadioButton(); 
         rb.Text = wNode.InnerText; 
         rb.Location = new Point(5, heightRadioButtons); 
         rb.Name = node["Titel"].InnerText; 
         if (waardeNodes.Count - 1 < counterLastRadioButton) 
         { 
          rb.Checked = true; 
         } 
         panel.Controls.Add(rb); 
         dictRadioButtons.Add(rb.Text, rb); 

        } 
        this.Controls.Add(panel); 

        int countA = 0; 
        foreach (XmlNode nodeA in nodes) 
        { 
         countA += 1; 
         if (countA < 4) 
         { 
          WidthPanelsRow1 += 155; 
          Panel panelA = new Panel(); 
          panelA.Size = new Size(140, 40); 
          panelA.Location = new Point(WidthPanelsRow1, heightPanelsRow1); 
          panelA.Name = "panel" + count.ToString(); 

          XmlNodeList titelPreset = node.SelectNodes("ButtonTitel"); 
          foreach (XmlNode titelNode in titelPreset) 
          { 
           Button btn = new Button(); 
           btn.Text = titelNode.InnerText; 
           btn.Location = new Point(0, 0); 
           btn.Name = nodeA["ButtonTitel"].InnerText; 
           btn.Size = new Size(140, 40); 

          } 
          this.Controls.Add(panelA); 
         } 
        } 
       } 
      } 

      XmlNodeList presets = doc.GetElementsByTagName("Preset"); 
      XmlNodeList radioButtonValues = presets[0].SelectNodes("sets"); 

      foreach (XmlNode rbNode in radioButtonValues) 
      { 
       Dictionary<string, RadioButton> rbPanel = radioPanels[rbNode.SelectNodes("RadioButtonTitel")[0].InnerText]; 
       RadioButton rb = rbPanel[rbNode.SelectNodes("RadioButtonValue")[0].InnerText]; 
       rb.Checked = true; 
      } 
     } 

    } 
} 
+0

謝謝,給我一點時間來測試它。 – Niels

+0

當我在一個空項目中測試這段代碼時,出現以下錯誤:「mscorlib.dll中發生類型爲」System.Collections.Generic.KeyNotFoundException「的未處理的異常」at line:「Dictionary rbPanel = radioPanels [rbNode.SelectNodes( 「RadioButtonTitel」)[0] .InnerText];」。 – Niels

+0

感謝btw的巨大努力,你想讓我提供錯誤細節的圖像? – Niels