2017-10-19 203 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