开发者

How do you create a Listbox on the fly (at runtime) in VB.net?

开发者 https://www.devze.com 2023-02-11 14:04 出处:网络
I\'m attempting to create a listbox when a button is clicked in Visual Basic 2008. I can\'t seem to find code that works to do this.

I'm attempting to create a listbox when a button is clicked in Visual Basic 2008. I can't seem to find code that works to do this. I found a few examples that were very similar and said this would work:

Dim lstOutput As ListBox

lstOutput = Me.Controls.Add("VB.Label", "List1")

Problem is that both of the things inside the parenthesis generate errors:

For the first one:

Value 开发者_开发百科of type 'String' cannot be converted to 'System.Windows.Forms.Control'.

And the second one:

Too many arguments to 'Public Overridable Sub Add(value As System.Windows.Forms.Control)'.

Any ideas?


This will add a empty listbox as the last control in the page:

Dim lstOutput As New ListBox With { .Id = "List1" }
Page.Controls.Add(lstOutput)


you must use new keyword like this:

Dim lstOutput As New ListBox Me.Controls.Add(lstOutput)


You want this:

Dim lstOutput As New ListBox()
Me.Controls.Add(lstOutut)

Notice the addition of the "New" keyword to actually create your control.


Try this

Dim lstOutput As new ListBox()
Me.Controls.Add(lstOutput)


Every time you press the button (assuming the code is under the a button) the listbox is added at the top left corner. What if every time you want to add a new listbox it adds on top of the other and you can't see the new list box. How do you dynamically add one listbox next to the other without specifying the coordinates?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号