By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,384 Members | 2,092 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,384 IT Pros & Developers. It's quick & easy.

Create controls programmatically

P: n/a
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

*** Sent via Developersdex http://www.developersdex.com ***
Feb 17 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On 2007-02-17, William Foster <no****@devdex.comwrote:
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

*** Sent via Developersdex http://www.developersdex.com ***
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 Shelton
Feb 17 '07 #2

P: n/a
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

*** Sent via Developersdex http://www.developersdex.com ***
Feb 18 '07 #3

P: n/a
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.
----------------------------------------
"William Foster" <no****@devdex.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
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

*** Sent via Developersdex http://www.developersdex.com ***

Feb 18 '07 #4

P: n/a
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

*** Sent via Developersdex http://www.developersdex.com ***
Feb 18 '07 #5

P: n/a
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 Developersdex http://www.developersdex.com ***
Feb 18 '07 #6

P: n/a
"William Foster" <no****@devdex.comschrieb
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/lib...94(VS.71).aspx

especially sub topic:
http://msdn2.microsoft.com/en-us/lib...93(VS.71).aspx
Armin

Feb 18 '07 #7

P: n/a
On Feb 18, 5:09 am, William Foster <nos...@devdex.comwrote:
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

Feb 18 '07 #8

P: n/a

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/lib...67(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.
-----------------------------------------------
"William Foster" <no****@devdex.comwrote in message
news:uH**************@TK2MSFTNGP06.phx.gbl...
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

*** Sent via Developersdex http://www.developersdex.com ***

Feb 18 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.