2017-10-19 4 views
0

ケース:Windowsフォームアプリケーションは、XMLファイルからボタンとラジオボタンを作成しています。作成されたすべてのボタン(XMLのプリセット)について、ラジオボタン(XML内のLijst)をmy 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) 
{ 
    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... 
} 

ありがとう:

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

コードプリセットボタンを作成します。

+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 '型の未処理の例外があります」という行が表示されます: "Dictionary rbPanel = radioPanels [rbNode.SelectNodes( "RadioButtonTitel")[0] .InnerText]; "。 – Niels

+0

偉大な努力のおかげでbtw、私はエラーの詳細の画像を提供したいですか? – Niels

関連する問題