Solved

Display comboxes created at runtime

Posted on 2011-03-10
5
308 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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now