Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Display comboxes created at runtime

Posted on 2011-03-10
5
Medium Priority
?
321 Views
Last Modified: 2013-11-27
I am generating comboboxes as:
 Public Combos() As ComboBox
later I use:
 ReDim Combos(5)
      For J = 0 To 5
         Combos(J) = New ComboBox
      Next
I use Combos(0).Items.Add(Item) for example to populate them.
However I cannot display the comboboxes on the form.
What is missing?
0
Comment
Question by:hlominac
  • 2
  • 2
5 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 35094166
You still need to place them somewhere on the form (through coding)

Example:

For J = 0 To 5
         ComboBox x = New ComboBox
         x.Size = New System.Drawing.Size(60, 15)  
         x.Location = New System.Drawing.Point(30 * J, 200)  
         Me.Controls.Add(x)  
         Combos(J) = x
      Next


http://www.dreamincode.net/forums/topic/63445-dynamic-form-and-controls-generation/
0
 
LVL 9

Expert Comment

by:mayank_joshi
ID: 35094192
This Article may help you in creating dynamic combobox in windows form:-

http://aspalliance.com/1385_Building_Dynamic_NET_Controls_with_Windows_Forms.2
0
 

Author Comment

by:hlominac
ID: 35095944
Dhaest:,
When I ran your code I got an error message at: Me.Controls.Add(x)  
Controls is not a member of MyProgram.Data where Data is the subroutine containing the code.
How do I declare Controls?
Harold
0
 
LVL 53

Accepted Solution

by:
Dhaest earned 2000 total points
ID: 35106155
I see, you are creating the controls on a class.

When you call this class, you probably return the array of the comboboxes. In that case, you'll need to loop over this array and place them in your form.

Small example below
// This is your form-code
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim x As New MyProgram
        Dim combos() As ComboBox
        combos = x.GetCombos()

        Dim j As Integer = 0
        For Each combo In combos
            combo.Size = New System.Drawing.Size(60, 15)
            combo.Location = New System.Drawing.Point(30, 30 * j)
            j += 1
            Me.Controls.Add(combo)
        Next
    End Sub
End Class


// This is your class code
Public Class MyProgram

    Public Function GetCombos() As ComboBox()
        Dim Combos(5) As ComboBox
        ReDim Combos(5)
        For J = 0 To 5
            Combos(J) = New ComboBox
        Next
        Return Combos
    End Function


End Class

Open in new window

0
 

Author Closing Comment

by:hlominac
ID: 35127587
Using your example I learned a lot about creating in runtime.  Thank you!
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question