【问题标题】:Use buttons generated from xml file to set radiobutton values使用从 xml 文件生成的按钮来设置单选按钮值
【发布时间】:2018-03-31 03:14:08
【问题描述】:

案例:我的 Windows 窗体应用程序正在从 XML 文件创建按钮和单选按钮。对于创建的每个按钮(在 XML 中预设),我想将 checked 单选按钮(在 XML 中的 Lijst)更改为我的 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...
}

谢谢。

【问题讨论】:

  • 我喜欢创建一个字典。您可以按标题查找控件,但速度很慢。字典会超级快。
  • 我正在寻找快速的东西,因为创建该程序是为了将数据分配给图像,并将其写入文本文件。 “预设”在那里,因此如果“预设”很常见,用户不必更改 9 个单选按钮的值。但我不知道这样做。

标签: c# xml winforms button radio-button


【解决方案1】:

尝试使用字典。请参阅下面的代码:

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;
            }
        }

    }
}

【讨论】:

  • 谢谢,给我一点时间测试一下。
  • 当我在一个空项目中测试此代码时,我收到以下错误:“在 mscorlib.dll 中发生类型为 'System.Collections.Generic.KeyNotFoundException' 的未处理异常”:“字典 rbPanel = radioPanels[rbNode.SelectNodes("RadioButtonTitel")[0].InnerText];".
  • 感谢您的大力支持,您希望我提供错误详细信息的图片吗?
  • 单选按钮的名称未添加到字典中,或者预设名称与控件的实际名称不匹配。首先检查 rbNopde 和 rbPanel 以查看哪个面板有问题。然后比较xml名称。
  • 好吧,这已修复,但不知何故没有预设按钮。我添加了“panelA.Controls.Add(btn);”,但仍然没有按钮。这个列表似乎是空的:“XmlNodeList titlePreset = node.SelectNodes("ButtonTitel");”。对不起,如果我在这方面有点菜鸟,我还是个学生。
猜你喜欢
  • 1970-01-01
  • 2013-07-24
  • 2013-08-26
  • 2015-03-22
  • 2019-08-01
  • 2020-07-05
  • 2014-12-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多