JCreationsInc Posted July 8, 2003 Posted July 8, 2003 I am making a skin program that will allow a user to preview his/her progress on a form created programatically. Well what I was wondering is if any knows a way of creating the controls for that form using a string for the name?? Dim ProgramName as String ProgramName = "PoopList" Dim ProgramName as New ComboBox ProgramName.Show Anybody got any ideas?? Quote When in doubt.... use the sledge Hammer!!
Heiko Posted July 8, 2003 Posted July 8, 2003 Are you talking of the name property or the name of the variable that points to the object? Quote .nerd
georg Posted July 8, 2003 Posted July 8, 2003 JCreationsInc Here is something to get you started: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Call ComboCreator() End Sub Private Sub ComboCreator() Dim x As Integer Dim sProgramName As String = "PoopList" For x = 1 To 10 Dim cmbProgramName As New ComboBox() cmbProgramName.Top = x * 20 cmbProgramName.Name = sProgramName & x Me.Controls.Add(cmbProgramName) Next End Sub Create a new form, add a button to it and then put the above code in. georg Quote
*Experts* mutant Posted July 8, 2003 *Experts* Posted July 8, 2003 And if you use what georg showed you and you want to access them later with code use this: Dim ctrl As Control For Each ctrl In Me.Controls If TypeOf ctrl Is ComboBox If ctrl.Name = "Combo1" 'or whatever number and name you set to it 'Do something to the combobox End If End If Next Quote
Heiko Posted July 8, 2003 Posted July 8, 2003 Not exactly :-) You need to program a recursion, I think. me.controls only contains the "top level" controls. More often than not these are GroupBoxes and panels. Quote .nerd
*Experts* mutant Posted July 8, 2003 *Experts* Posted July 8, 2003 Me.Controls contains all controls on the form :) Quote
Heiko Posted July 8, 2003 Posted July 8, 2003 I have experienced differently :) (Not starting at form-level, though, but on a tabpage) Quote .nerd
*Experts* mutant Posted July 8, 2003 *Experts* Posted July 8, 2003 Oh, you mean tab controls. It wont contain the tabs as they belong to the tabcontrol, not the form. I thought you meant that you need recursion for every control thats not a container. You only need recursion for those that have their own child controls. Quote
a_jam_sandwich Posted July 8, 2003 Posted July 8, 2003 same for anything that is a parant to other controls these inc Groupbox, panel... Andy Quote Code today gone tomorrow!
*Gurus* divil Posted July 9, 2003 *Gurus* Posted July 9, 2003 Every control can be a container, so you'd want to check every control's Controls collection. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
JCreationsInc Posted July 9, 2003 Author Posted July 9, 2003 woa nelly I should check my posted threads more often. I found the solution right after posting this thread here is what I used... 'In the load event of the form: Dim DynamicCombo As New ComboBox() 'Set the properties and display the control DynamicCombo.Name = "PoopList" DynamicCombo.DropDownStyle = ComboBoxStyle.DropDownList DynamicCombo.Items.Add("First List Item") DynamicCombo.Items.Add("Second List Item") DynamicCombo.Items.Add("Third List Item") DynamicCombo.SelectedIndex = 0 Me.Controls.Add(DynamicCombo) DynamicCombo.Show() 'This code is in the skin drawing procedure of my class: 'Loop through all the controls on the window and get our control Dim OurCombo As Integer, ComboName As ComboBox For OurCombo = 0 To ControlCount - 1 If Window.Controls.Item(OurCombo).Name = "PoopList" Then ComboName = Window.Controls.Item(OurCombo) End If Next 'Get the combobox Properties ComboName.Left = "10" ComboName.Top = "10" ComboName.Width = "100" ComboName.BackColor = color.black ComboName.ForeColor = color.white ComboName.Font = font.arial It seems like it is working pretty good. Thank you anyways for your help you dudes are groovy baby yeah! Quote When in doubt.... use the sledge Hammer!!
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.