Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Populate listbox in Form_Load

Status
Not open for further replies.

drluggo

Programmer
Jan 14, 2003
39
US
Hi,

I am trying to get used to this .NET thing. I am sure that most of my problems are related to trying to do things the old VB way and not used to the OOP model of .NET.

I have a list box that I am populating from an XML file. I would like to populate it in my Form_Load routine. Reading the XML file is not a problem. I want to read the XML nodes into an arraylist object. Therein lies the problem. I cannot create a reference to system.collections within my Form class, therefore I am forced to do so in a module which I call App_Globals. In this module I can define an object varaible that I wish to use when my form loads. However, I cannot do this because I cannot construct the system.collections.arraylist object becuase the form does not know of it.

Am I missing something?

Code:

///////

Imports System.Collections

Module App_Globals

Public g_arlArrList As ArrayList

End Module

///////

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim xmlDoc As New Xml.XmlDocument
Dim xmlNdList As Xml.XmlNodeList

Dim i As Integer

xmlDoc.Load("../xml/units.xml")
xmlNdList = xmlDoc.GetElementsByTagName("Unit")

For i = 0 To xmlNdList.Count - 1
g_arlArrList.Add(xmlNdList.Item(i))
Next

ListBox1.DataSource = g_arlArrList

End Sub

/////

The code fails because I have not constructed the arraylist object. but if I try to add:

g_arlArrList in the Form_Load subroutine, it does not recognize the ararylist type of object.
 
i would do it like this

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim dt As DataSet
dt.ReadXml("filename")

ListBox1.DataSource = dt.Tables(0)

End Sub

Christiaan Baes
Belgium

What a wonderfull world - Louis armstrong
 
Thanks for the input. I wsas able to implement this both my way and yours with one problem. The information that is dispalyed in the list box is the name of the object. The only results I get look something like:

system.xml.xmlelement

in the listbox text field. How do I convert the arraylist object to something that makes sense?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top