使用 XML 中的文本创建新复选框
CreateNewCheckBox with Text from XML
我有点受困于读取 xml 文件的值。
XML 看起来像:
<?xml version="1.0" encoding="utf-16"?>
<spfFiles>
<file>200_006 xxxxxxx</file>
<file>200_010 xxxxxxx</file>
<file>200_022 xxxxxxx</file>
<file>200_023 xxxxxxx</file>
<file>200_024 xxxxxxx</file>
<file>200_031 xxxxxxx</file>
<file>200_041 xxxxxxx</file>
</spfFiles>
我想做的是为每个文件创建一个新的复选框。
XmlTextReader xReader = new XmlTextReader("spfFiles_simW.xml");
while (xReader.Read())
{
switch (xReader.NodeType)
{
case XmlNodeType.Text: //Display the text in each element.
pnlSPFLIST.Controls.Add(CreateNewCheckBox(xReader.Value));
break;
}
}
每个新元素的创建工作正常。但是我在文件名方面遇到了麻烦。每个 Checkbox 只取 space 之前的部分作为名称。例如“200_006”。我的 xmlReader 不知何故似乎删掉了其余部分。
编辑:
所以这是我的 CreateNewCheck
private CheckBox CreateNewCheckBox(string sName)
{
label1.Text = sName;
int iExistingCheckBoxX = 0;
int iExistingCheckBoxY = 0;
int iIncrementX = 100;
int iIncrementY = 20;
CheckBox cbNew = new CheckBox();
cbNew.Width = iIncrementX;
if (pnlSPFLIST.Controls.Count == 0)
{
cbNew.Location = new Point(pnlSPFLIST.Location.X, pnlSPFLIST.Location.Y-25);
}
else
{
// Existing checkboxes, so get the Location of the last one.
iExistingCheckBoxX = pnlSPFLIST.Controls[pnlSPFLIST.Controls.Count - 1].Location.X;
iExistingCheckBoxY = pnlSPFLIST.Controls[pnlSPFLIST.Controls.Count - 1].Location.Y;
iExistingCheckBoxX = pnlSPFLIST.Location.X;
iExistingCheckBoxY = iExistingCheckBoxY + iIncrementY + 10;
cbNew.Location = new Point(iExistingCheckBoxX, iExistingCheckBoxY);
}
// Set the Text property according to the input.
cbNew.Text = sName;
return cbNew;
}
有谁知道我哪里出错了?
所以这是正确的解决方案。
我只需要更改我的 CreateNewCheckBox 方法:
private CheckBox CreateNewCheckBox(string sName)
{
...
int iIncrementX = 300;
int iIncrementY = 20;
...
}
只需增加 X 值,一切正常。
我有点受困于读取 xml 文件的值。 XML 看起来像:
<?xml version="1.0" encoding="utf-16"?>
<spfFiles>
<file>200_006 xxxxxxx</file>
<file>200_010 xxxxxxx</file>
<file>200_022 xxxxxxx</file>
<file>200_023 xxxxxxx</file>
<file>200_024 xxxxxxx</file>
<file>200_031 xxxxxxx</file>
<file>200_041 xxxxxxx</file>
</spfFiles>
我想做的是为每个文件创建一个新的复选框。
XmlTextReader xReader = new XmlTextReader("spfFiles_simW.xml");
while (xReader.Read())
{
switch (xReader.NodeType)
{
case XmlNodeType.Text: //Display the text in each element.
pnlSPFLIST.Controls.Add(CreateNewCheckBox(xReader.Value));
break;
}
}
每个新元素的创建工作正常。但是我在文件名方面遇到了麻烦。每个 Checkbox 只取 space 之前的部分作为名称。例如“200_006”。我的 xmlReader 不知何故似乎删掉了其余部分。
编辑: 所以这是我的 CreateNewCheck
private CheckBox CreateNewCheckBox(string sName)
{
label1.Text = sName;
int iExistingCheckBoxX = 0;
int iExistingCheckBoxY = 0;
int iIncrementX = 100;
int iIncrementY = 20;
CheckBox cbNew = new CheckBox();
cbNew.Width = iIncrementX;
if (pnlSPFLIST.Controls.Count == 0)
{
cbNew.Location = new Point(pnlSPFLIST.Location.X, pnlSPFLIST.Location.Y-25);
}
else
{
// Existing checkboxes, so get the Location of the last one.
iExistingCheckBoxX = pnlSPFLIST.Controls[pnlSPFLIST.Controls.Count - 1].Location.X;
iExistingCheckBoxY = pnlSPFLIST.Controls[pnlSPFLIST.Controls.Count - 1].Location.Y;
iExistingCheckBoxX = pnlSPFLIST.Location.X;
iExistingCheckBoxY = iExistingCheckBoxY + iIncrementY + 10;
cbNew.Location = new Point(iExistingCheckBoxX, iExistingCheckBoxY);
}
// Set the Text property according to the input.
cbNew.Text = sName;
return cbNew;
}
有谁知道我哪里出错了?
所以这是正确的解决方案。 我只需要更改我的 CreateNewCheckBox 方法:
private CheckBox CreateNewCheckBox(string sName)
{
...
int iIncrementX = 300;
int iIncrementY = 20;
...
}
只需增加 X 值,一切正常。