Dobz
Dobz

Reputation: 1213

CheckBox.Text = X... CheckBoxes Text stays Blank C#

I parse an xml document and get what I need, 2 strings (y and z).
They work fine. But when I add the string z to a CheckBox text.. the text stays blank and All I get is a checkbox with no words.

    List<string> extens = new List<string> { };
    String xmlPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "/ExtraEx.xml";

    public Exten()
    {
        InitializeComponent();
    }

    private void Exten_Load(object sender, EventArgs e)
    {
        if (File.Exists(xmlPath))
        {
            XmlDocument xml = new XmlDocument();
            xml.Load(xmlPath);
            XmlNodeList xnList = xml.SelectNodes("/Extras/Add");
            foreach (XmlNode xn in xnList)
            {
                string y = xn["checked"].InnerText;
                string z = xn["Exten"].InnerText;

                //Shows The String Value!
                MessageBox.Show(z);

                CheckBox cb = new CheckBox();

                //cb.Text Still Stays Blank?
                cb.Text = z;
                fLayout.Controls.Add(cb);
            }
        }
        else
        {
            MessageBox.Show("XML File Does not Exist!");
        }

    }

Upvotes: 0

Views: 233

Answers (3)

Dobz
Dobz

Reputation: 1213

My Xml file was set up like so.
And due to the tabs beside the extension my program took the string as "whitespace.mp3".

<?xml version="1.0" encoding="utf-8" ?>
<Extras>
    <Add>
        <Exten>
            .mp3
        </Exten>
        <checked>
            True
        </checked>
    </Add>
    <Add>
        <Exten>
            .xls
        </Exten>
        <checked>
            False
        </checked>
    </Add>
</Extras>

I added some extra code to fix this in my program.

string y = xn["checked"].InnerText;
string z = xn["Exten"].InnerText;
y = Regex.Replace(y, @"\s+", "");
z = Regex.Replace(z, @"\s+", "");

This took the whitespace from around the extension leaving the string as ".mp3".

Upvotes: 1

Mauricio Gracia Gutierrez
Mauricio Gracia Gutierrez

Reputation: 10844

Try this

cb.Text = "fixed value";

If the text is still not shown, then make sure that the checkbox is set to visible and fits the form/windows size

If the text is shown then is a problem related to how z is being read from the file then you can try this cb.Text = y;

If is a text is shown then for sure the node Exten does not exist or contains the value that you need to show

And if all of the above fails call

this.Refresh() ;

Upvotes: 1

8bitcat
8bitcat

Reputation: 2224

I fetched this code from my application works great!

UPDATE! Have you tried the following?

this.Refresh()  //on the form you are adding controls to?!



CheckBox[] chk;

chk = new CheckBox[10];

for (int i = 0; i <= 9; i++)
{

  chk[i] = new CheckBox();

  chk[i].Name = i.ToString();

  chk[i].Text = i.ToString();

  chk[i].TabIndex = i;

 chk[i].AutoCheck=true;

  panel1.Controls.Add(chk[i]);

}

Upvotes: 0

Related Questions