Adding a list box to a form that was added during runtime

I am having trouble adding a list box to a form created during runtime

Page 1 of 1

3 Replies - 2638 Views - Last Post: 23 December 2009 - 02:47 PM Rate Topic: -----

#1 notacoder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 16-December 09

Adding a list box to a form that was added during runtime

Posted 22 December 2009 - 01:17 PM

I am having trouble adding a list box to a form that I have added during run time.
Can you help?
Private Sub btnHelp_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnHelp.Click
        'Creates an array that will house the instructions
        Dim instructArray() As String = {"Left key = left", "Right key = right", "Spacebar = shoot"}

        'When the help button is clicked a list of instructions will appear
        'Creates a new form
        Dim Form2 As Form = New Form()
        'Calls the form, showing it during runtime
        Form2.Show()

        'Creates a new list box that will show on the form during runtime
        Dim lstHelp As New ListBox
        'Adds the items from the array to the listbox
        lstHelp.Items.Add(instructArray(0))
        lstHelp.Items.Add(instructArray(1))
        lstHelp.Items.Add(instructArray(2))



    End Sub

This post has been edited by notacoder: 22 December 2009 - 01:29 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Adding a list box to a form that was added during runtime

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6052
  • View blog
  • Posts: 23,487
  • Joined: 23-August 08

Re: Adding a list box to a form that was added during runtime

Posted 22 December 2009 - 01:22 PM

Moving to VB.NET.
Was This Post Helpful? 0
  • +
  • -

#3 LeisureProgrammer  Icon User is offline

  • N00by VB.NET Developer
  • member icon

Reputation: 110
  • View blog
  • Posts: 588
  • Joined: 06-September 09

Re: Adding a list box to a form that was added during runtime

Posted 22 December 2009 - 01:41 PM

Add this line just before End Sub to add this listbox.
Form2.Controls.Add(lstHelp)
Was This Post Helpful? 1
  • +
  • -

#4 clawson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 13-December 09

Re: Adding a list box to a form that was added during runtime

Posted 23 December 2009 - 02:47 PM

View Postnotacoder, on 22 Dec, 2009 - 12:17 PM, said:

I am having trouble adding a list box to a form that I have added during run time.
Can you help?
Private Sub btnHelp_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnHelp.Click
        'Creates an array that will house the instructions
        Dim instructArray() As String = {"Left key = left", "Right key = right", "Spacebar = shoot"}

        'When the help button is clicked a list of instructions will appear
        'Creates a new form
        Dim Form2 As Form = New Form()
        'Calls the form, showing it during runtime
        Form2.Show()

        'Creates a new list box that will show on the form during runtime
        Dim lstHelp As New ListBox
        'Adds the items from the array to the listbox
        lstHelp.Items.Add(instructArray(0))
        lstHelp.Items.Add(instructArray(1))
        lstHelp.Items.Add(instructArray(2))



    End Sub


 :) 'Here is an example from Microsoft:
'Remember Me.Controls.Add(MyText)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
   Dim MyText As New TextBox()
   MyText.Location = New Point(25, 25)
   Me.Controls.Add(MyText)
End Sub

'http://msdn.microsoft.com/en-us/library/0h5y8567(VS.100).aspx




Was This Post Helpful? 0
  • +
  • -

Page 1 of 1