Create controls programmatically

  • Thread starter Thread starter William Foster
  • Start date Start date
W

William Foster

Good evening all,

I am creating a program with the ability to select an option from a
ComboBox which will then create a secondary list of options in a
CheckedListBox. Previously when I have needed to create multiples of
this type of user option I have simply hard coded three or four set
options for the user. However, I would like to have the option for the
user to have as many options available as they like. In order to do
this I have to either hard code a number of these options (there are
approximately 50 of them), or find some way to dynamically create these
controls depending on the user input.

I have looked on DevelopersDex and the Microsoft Visual Studio .NET 2003
help, but I have been unable to find how to do this.

If anyone has had any experience with creating this type of a program,
or any alternatives that have worked better please let me know.

Yours sincerely,

William Foster
 
Good evening all,

I am creating a program with the ability to select an option from a
ComboBox which will then create a secondary list of options in a
CheckedListBox. Previously when I have needed to create multiples of
this type of user option I have simply hard coded three or four set
options for the user. However, I would like to have the option for the
user to have as many options available as they like. In order to do
this I have to either hard code a number of these options (there are
approximately 50 of them), or find some way to dynamically create these
controls depending on the user input.

I have looked on DevelopersDex and the Microsoft Visual Studio .NET 2003
help, but I have been unable to find how to do this.

If anyone has had any experience with creating this type of a program,
or any alternatives that have worked better please let me know.

Yours sincerely,

William Foster

William...

I'm not quite sure what your asking? You say the select an option from a
ComboBox, and then that populates a secondary CheckedListBox. Why do you need
to dynamically create controls? Or, are you planning to do something like the
optons screen in VS - you select from a treeview on the left, and a different
screen appears on the right?
 
Tom,

Thanks for the reply, I probably didn't explain my question very well.

It is for a Reporting Program which I need to crete data limits for.
Bascially, what I want to do is once the user selects one option from
the ComboBox and CheckedListBox another set of ComboBox and CheckListBox
are generated, this process would be repeated as many times as the user
wants.

I know that I can create them all and just hide them from the user until
they are required, but I would prefer not to do this as I would
potentially have to create around 50 of each.

For example: the use selects age from the first combobox, then less than
21 from the CheckedListBox, then another ComboBox is created below the
first, and the user has the option to choose something else etc etc.

Yours sincerely,

William Foster
 
You can create controls programmatically on the fly in VB2005. I would
assume it was the same or similar in VB2003. It's something like this:

Dim txt As TextBox = New TextBox()
myForm.Controls.Add(txt)

Robin S.
 
Robin,

Thanks for the help, that worked just as I needed.

I am going to have to undertake a course in Microsoft Help I think, I
can't believe it is that simple yet I couldn't find it in on either
their online or core help files.

Thanks once again !

Yours sincerely,

William Foster
 
Robin,

One last question; how do you capture events of the newly created
control; what do you address it as in the programming. Usually you just
select the item and the 'Click' event. But how do you know what the
program has called the control?

Yours sincerely,

William Foster
 
William Foster said:
Robin,

One last question; how do you capture events of the newly created
control; what do you address it as in the programming. Usually you
just select the item and the 'Click' event. But how do you know
what the program has called the control?

The name doesn't matter. The "Handles" clause connects the event and the
procedure. If not declared "Withevents":

http://msdn2.microsoft.com/en-us/library/aa903294(VS.71).aspx

especially sub topic:
http://msdn2.microsoft.com/en-us/library/6yyk8z93(VS.71).aspx


Armin
 
Robin,

One last question; how do you capture events of the newly created
control; what do you address it as in the programming. Usually you just
select the item and the 'Click' event. But how do you know what the
program has called the control?

Yours sincerely,

William Foster

*** Sent via Developersdexhttp://www.developersdex.com***

One last question; how do you capture events of the newly created
control; what do you address it as in the programming.

Use the AddHandler keyword. ie:

<pseudocode>

' This code adds the button and maps it's click event to a handler
Dim button as New Button()
AddHandler button.Click, AddressOf MyDynamicButton_Click(sender as
Object, e as ClickEventArgs)
me.Controls.Add(button)

' This is the sub that will handle the click event
private sub MyDynamicButton_Click(sender as object, e as
ClickEventArgs)
MessageBox.Show("Hello World!")
end sub

</pseudocode>

Also, if you're not sure what property values you need to set in order
to get your control the way you want it, here's a simple trick. First
create the control in design mode just like you normally do. Then,
pull up Solution Explorer and be sure that the "Show All" option is
selected and then expand the node for the form/usercontrol/whatever
you just laid out the control on. Then open up the MyForm.Designer.Vb
file and look in the region labeled "Component Generated Code" for the
settings of the control you just created - this is the code that is
generated by the designer. Then you can just copy and paste (and
slighty modify) this code and use it when you create the dynamic
control. This is somewhat overkill for simple controls, but can be a
lifesaver when you start dealing with more complex controls.

I hope all that made sense...

Thanks,

Seth Rowe
 
Well, *that* explains what your problem is. You're using Microsoft Help?!?!

I opened up my Visual Studio help and typed in "add controls" and got this
link.

http://msdn2.microsoft.com/en-us/library/0h5y8567(VS.80).aspx

You have to give it the exact right search criteria. Or use Google.
Frequently, their search capabilities against Microsoft's own content are
better than Microsoft's.

Good luck.
Robin S.
Ts'i mahnu uterna ot twan ot geifur hingts uto.
 
Back
Top