PC Review


Reply
Thread Tools Rate Thread

Create controls programmatically

 
 
William Foster
Guest
Posts: n/a
 
      17th Feb 2007
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 ***
 
Reply With Quote
 
 
 
 
Tom Shelton
Guest
Posts: n/a
 
      17th Feb 2007
On 2007-02-17, William Foster <(E-Mail Removed)> wrote:
> 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
 
Reply With Quote
 
William Foster
Guest
Posts: n/a
 
      18th Feb 2007
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 ***
 
Reply With Quote
 
RobinS
Guest
Posts: n/a
 
      18th Feb 2007
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" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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 ***



 
Reply With Quote
 
William Foster
Guest
Posts: n/a
 
      18th Feb 2007
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 ***
 
Reply With Quote
 
William Foster
Guest
Posts: n/a
 
      18th Feb 2007
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 ***
 
Reply With Quote
 
Armin Zingler
Guest
Posts: n/a
 
      18th Feb 2007
"William Foster" <(E-Mail Removed)> schrieb
> 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

 
Reply With Quote
 
rowe_newsgroups
Guest
Posts: n/a
 
      18th Feb 2007
On Feb 18, 5:09 am, William Foster <nos...@devdex.com> wrote:
> 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

 
Reply With Quote
 
RobinS
Guest
Posts: n/a
 
      18th Feb 2007

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.
-----------------------------------------------
"William Foster" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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 ***



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Programmatically create .NET Controls Chris Microsoft ASP .NET 0 21st May 2008 01:49 PM
Move controls programmatically =?Utf-8?B?UGV0ZXIgU3RvbmU=?= Microsoft Access Form Coding 3 3rd Apr 2006 09:09 AM
adding controls programmatically johnmmcparland Microsoft Access Forms 1 20th Dec 2005 02:28 PM
basic: asp.net user controls: how to programmatically add attribute to all text box controls? Microsoft ASP .NET 4 9th Nov 2005 02:14 AM
Rendering ASP controls programmatically =?Utf-8?B?WmFub25p?= Microsoft C# .NET 2 11th Mar 2004 06:55 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:38 AM.