Windows Develop Bookmark and Share   
 index > Windows Forms General > Combo Box
 

Combo Box

When creating a Combo Box, Can I populate the drop down options from an XML file?
Duncan McC  Wednesday, January 17, 2007 12:55 PM

If the XML is Dataset compatible, the resulting XmlDataDocument object has a Dataset property exposed. You can use your standard combo-box binding to a dataset.

A simple Dataset compatible XML

<document>
  <table>
    <name>Table 1</name>
    <field1>Field 1 Value</field1>
    . . .
    <fieldn>Field n Value</fieldn> 
  </table>
  . . .
  <table>
    <name>Table N</name>
    <field1>Field 1 Value</field1>
    . . .
    <fieldn>Field n Value</fieldn> 
  </table>
</document>

JRQ  Wednesday, January 17, 2007 5:52 PM

Hi,

In theory can load it from any kind of source but not via the designer. These kind of things need to be done via code. Here is an example: http://www.functionx.com/vcsharp/xml/comboboxitems.htm

Greetz,

Geert

Geert Verhoeven
Consultant @ Ausy Belgium

My Personal Blog

Geert Verhoeven  Wednesday, January 17, 2007 1:48 PM

If the XML is Dataset compatible, the resulting XmlDataDocument object has a Dataset property exposed. You can use your standard combo-box binding to a dataset.

A simple Dataset compatible XML

<document>
  <table>
    <name>Table 1</name>
    <field1>Field 1 Value</field1>
    . . .
    <fieldn>Field n Value</fieldn> 
  </table>
  . . .
  <table>
    <name>Table N</name>
    <field1>Field 1 Value</field1>
    . . .
    <fieldn>Field n Value</fieldn> 
  </table>
</document>

JRQ  Wednesday, January 17, 2007 5:52 PM
Hi folllow is an example add all <Name> item into combobox
xml file as follow
<?xml version="1.0" encoding="utf-8"
?>
<
Employees
>
<
NO1
>
<
Name>May</Name
>
<
ZIP> 239000 </ZIP
>
<
Address>NO1 Street</Address
>
<
City>NO1 City</City
>
<
State> NO1 </State
>
</
NO1
>
<
NO2
>
<
Name>July</Name
>
<
ZIP> 239000 </ZIP
>
<
Address>No2 Street</Address
>
<
City>No2 City</City
>
<
State> No2 </State
>
</
NO2
>
<
NO3
>
<
Name>Any</Name
>
<
ZIP> 100000 </ZIP
>
<
Address>No3 Street</Address
>
<
City>No3 City</City
>
<
State>No3</State
>
</
NO3
>
</
Employees>

add code at form load event

string
path = Environment.CurrentDirectory;
path += "\\XMLFile1.xml";
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNodeReader reader = new XmlNodeReader(doc);
string s = "";
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
s = reader.Name;
break;
case XmlNodeType.Text:
if (s.Equals("Name"))
this.comboBox1.Items.Add(reader.Value);

break;
}
}
if (reader != null)
reader.Close();


Bob zhu - SJTU  Thursday, January 18, 2007 5:24 AM

You can also do this;

Change NO1, NO2 and so forth to just NO. The xml now represent a dataset with table name NO. The original XML will just give you a dataset with multiple tables named NO1, NO2 and so forth.

System.Xml.XmlDataDocument xmlDoc = null;

xmlDoc.Load("<path to the xml file>");

Now you can have fun with xmlDoc.Dataset object.

You also manipulate the XML nodes using the Dataset itself as if it's a database table.

 

JRQ  Thursday, January 18, 2007 8:50 PM

You can use google to search for other answers

Custom Search

More Threads

• When is MTAThread safe?
• Non-repeating random?
• very urgent?Rich textbox in vb.net
• Communicating with user controls
• passing values from one form to another form problme
• Menu Command Service
• using folder browser dialog on a camera - can it be done?
• Browser object within a windows form ... how?
• Windows form application blinking
• How to get login time and logout time of the user?