Solved

Display comboxes created at runtime

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

757 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

23 Experts available now in Live!

Get 1:1 Help Now