Creating objects at Runtime

C

cmdolcet69

How can I create a dropdown box at runtime? I want my dropdown box to
appear when my user enters the program and the it would load the save
data from an xml file. How can i do this?
 
C

cfps.Christian

How can I create a dropdown box at runtime? I want my dropdown box to
appear when my user enters the program and the it would load the save
data from an xml file. How can i do this?

if you know where you want the drop down and what is going to be in it
I would make its ddl.Visible = false and make it = true when you are
ready for it.

If you MUST dynamically place the drop down then you will: (assuming
this is not ASP.NET in which case you would use DropDownList)
dim cbo as new System.Windows.Forms.ComboBox
'Add items to combo
cbo.Location = new Point([x], [y])
cbo.Name = cboName
Me.Controls.Add(cbo)
 
P

Phill W.

cmdolcet69 said:
How can I create a dropdown box at runtime?

In exactly the same way that the Forms Designer does! :)

No; Seriously. ;-)

Have a look in the "Designer Generated Code" region or the *.designer.vb
file (depending on your version of Visual Basic). These contain the
code that your program runs that creates every control in your program.

Read and Learn ...
(then simplify; /some/ of the code it generates is just /rubbish/).
I want my dropdown box to appear when my user enters the program and
then it would load the save data from an xml file.

Dim cb as New ComboBox()

With cb
.Location = New Point( 0, 0 )
.Size = New Size( 80, 20 ) ' say
.Visible = True
.Items.Add( "X" )
End With

AddHandler cb.SelectedIndexChanged _
, AddressOf AnyComboBox_SelectedIndexChanged

Me[.container].Controls.add( cb )


(Remember that each Control (and Form) acts as a container for other
Controls placed "on" it).

For the Xml stuff:

Dim doc as New Xml.XmlDocument
doc.Load( "file" )

With cb
For Each eNode as XmlNode _
In doc.SelectNodes( "xPath" )
cb.Items.Add( eNode.GetAttribute( Value ) ) ' say
If eNode.GetAttribute( "current" ) <> "" Then
cb.SelectedIndex = cb.Count - 1
End If
Next
End With

HTH,
Phill W.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top