473,387 Members | 1,624 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to deal with controls created programmatically

Siv
Hi,
I have an application that reads information from a database and depending
on that information creates controls on a form. Depending on where the user
is at in a process there may be one item or many items. I create check
boxes, one for each entry I get back from the database.

The code therefore does stuff like this:

cb = New CheckBox
tpMain.Controls.Add(cb)

x = 50 : y += 25
cb.Left = x
cb.Top = y
cb.Text = CStr(dr("DocumentName"))
cb.Tag = CStr(dr("TemplatePath"))
cb.Name = "chkS" & CStr(dr("StageDocBelongsTo")) & "D" &
CStr(dr("DocumentNumber")) 'ie chkS1D1 Stage 1 Doc 1
Which works fine, but later on I want to reference these created controls
and say change the tag or other properties.
Because the controls don't exist until they are created whilst the program
is running, when I write the code to modify these properties the VB Parser
quite correctly states that the object has not been declared and won't let
me compile the code.

Is there a way that you can write code that references controls that don't
exist until the program is running. The only way I can see to do this is to
put every possible control that might be needed and hide it on the form and
then rather than creating the controls programmatically, just unhide them
and move them to the required location on the form. Which will work, but
kind of defeats what I am trying to do??

Any help appreciated.

Siv

Jun 27 '08 #1
4 1469

You can store references to them into an array when you create them (or
hash, if you want to be clever, hash them by name, i.e. "combobox1",
"combobox2", then access like this: comboHash("combobox1").Checked, etc.).


"Siv" <g@removethistextsivill.comwrote in message
news:E4**********************************@microsof t.com...
Hi,
I have an application that reads information from a database and depending
on that information creates controls on a form. Depending on where the
user is at in a process there may be one item or many items. I create
check boxes, one for each entry I get back from the database.

The code therefore does stuff like this:

cb = New CheckBox
tpMain.Controls.Add(cb)

x = 50 : y += 25
cb.Left = x
cb.Top = y
cb.Text = CStr(dr("DocumentName"))
cb.Tag = CStr(dr("TemplatePath"))
cb.Name = "chkS" & CStr(dr("StageDocBelongsTo")) & "D" &
CStr(dr("DocumentNumber")) 'ie chkS1D1 Stage 1 Doc 1
Which works fine, but later on I want to reference these created controls
and say change the tag or other properties.
Because the controls don't exist until they are created whilst the program
is running, when I write the code to modify these properties the VB Parser
quite correctly states that the object has not been declared and won't let
me compile the code.

Is there a way that you can write code that references controls that don't
exist until the program is running. The only way I can see to do this is
to put every possible control that might be needed and hide it on the form
and then rather than creating the controls programmatically, just unhide
them and move them to the required location on the form. Which will work,
but kind of defeats what I am trying to do??

Any help appreciated.

Siv

Jun 27 '08 #2
Siv
Thanks for the reply, could you explain it with a simple example, as I am
not sure why what you recommend would not lead to the same problem as I am
having already, i.e. it would give me an object not declared warning?

Does using an array tip the parser off somehow that this isn't an actual
control just yet and will only be when the code runs??

Siv

"Jameson" <rt******@hotmail.comwrote in message
news:g3*******************@news.demon.co.uk...
>
You can store references to them into an array when you create them (or
hash, if you want to be clever, hash them by name, i.e. "combobox1",
"combobox2", then access like this: comboHash("combobox1").Checked, etc.).


"Siv" <g@removethistextsivill.comwrote in message
news:E4**********************************@microsof t.com...
>Hi,
I have an application that reads information from a database and
depending on that information creates controls on a form. Depending on
where the user is at in a process there may be one item or many items. I
create check boxes, one for each entry I get back from the database.

The code therefore does stuff like this:

cb = New CheckBox
tpMain.Controls.Add(cb)

x = 50 : y += 25
cb.Left = x
cb.Top = y
cb.Text = CStr(dr("DocumentName"))
cb.Tag = CStr(dr("TemplatePath"))
cb.Name = "chkS" & CStr(dr("StageDocBelongsTo")) & "D" &
CStr(dr("DocumentNumber")) 'ie chkS1D1 Stage 1 Doc 1
Which works fine, but later on I want to reference these created controls
and say change the tag or other properties.
Because the controls don't exist until they are created whilst the
program is running, when I write the code to modify these properties the
VB Parser quite correctly states that the object has not been declared
and won't let me compile the code.

Is there a way that you can write code that references controls that
don't exist until the program is running. The only way I can see to do
this is to put every possible control that might be needed and hide it on
the form and then rather than creating the controls programmatically,
just unhide them and move them to the required location on the form.
Which will work, but kind of defeats what I am trying to do??

Any help appreciated.

Siv

Jun 27 '08 #3
Is this what you want to do?

Dim cb As CheckBox = tpMain.Controls.Item("chkS1D1")
cb.Checked = True
Siv wrote:
>>
>>The code therefore does stuff like this:

cb = New CheckBox
tpMain.Controls.Add(cb)

x = 50 : y += 25
cb.Left = x
cb.Top = y
cb.Text = CStr(dr("DocumentName"))
cb.Tag = CStr(dr("TemplatePath"))
cb.Name = "chkS" & CStr(dr("StageDocBelongsTo")) & "D" &
CStr(dr("DocumentNumber")) 'ie chkS1D1 Stage 1 Doc 1
Is there a way that you can write code that references controls that
don't exist until the program is running.

Jun 27 '08 #4
You can access the controls by keeping the cb variable in scope. Probably the
easiest is to have a module-level array in the form.

You'll need to use AddHandler if you want to respond to events generated by
the added controls.
--
David Streeter
Synchrotech Software
Sydney Australia
"Siv" wrote:
Hi,
I have an application that reads information from a database and depending
on that information creates controls on a form. Depending on where the user
is at in a process there may be one item or many items. I create check
boxes, one for each entry I get back from the database.

The code therefore does stuff like this:

cb = New CheckBox
tpMain.Controls.Add(cb)

x = 50 : y += 25
cb.Left = x
cb.Top = y
cb.Text = CStr(dr("DocumentName"))
cb.Tag = CStr(dr("TemplatePath"))
cb.Name = "chkS" & CStr(dr("StageDocBelongsTo")) & "D" &
CStr(dr("DocumentNumber")) 'ie chkS1D1 Stage 1 Doc 1
Which works fine, but later on I want to reference these created controls
and say change the tag or other properties.
Because the controls don't exist until they are created whilst the program
is running, when I write the code to modify these properties the VB Parser
quite correctly states that the object has not been declared and won't let
me compile the code.

Is there a way that you can write code that references controls that don't
exist until the program is running. The only way I can see to do this is to
put every possible control that might be needed and hide it on the form and
then rather than creating the controls programmatically, just unhide them
and move them to the required location on the form. Which will work, but
kind of defeats what I am trying to do??

Any help appreciated.

Siv
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Invalidlastname | last post by:
Hi, We are developing an asp.net application, and we dynamically created certain literal controls to represent some read-only text for certain editable controls. However, recently we found an issue...
8
by: Donald Xie | last post by:
Hi, I noticed an interesting effect when working with controls that are dynamically loaded. For instance, on a web form with a PlaceHolder control named ImageHolder, I dynamically add an image...
9
by: james.e.coleman | last post by:
Hello, I have created a custom dropdownlist that is used multiple times within a single page. When trying to set the values of the controls with the page in which they are being used, they all...
0
by: TB | last post by:
Hi All: I have this page where a rows / cells are programmatically added to to table by pushing a button. The rows contain a textbox and a associated button. What I want to is to be able to...
6
by: TB | last post by:
Hi All: I have this page where a rows / cells are programmatically added to to table by pushing a button. The rows contain a textbox and a associated button. What I want to is to be able to...
0
by: mark.norgate | last post by:
Hi I'm having a problem in adding controls to a page programmatically in response to a button click. Composite user controls added programmatically in the CreateChildControls() method work...
8
by: mark.norgate | last post by:
I've asked this question before, but still haven't solved it, so am asking again. I am programmatically adding a user control to the page in response to a button click. The user control consists...
9
by: netasp | last post by:
hi all, how can I populate one aspx form when page is loading based on page ID? for example: loading page A (to search for VB code) would display labels and texboxes, dropdown lists all related...
8
by: hunanwarrior | last post by:
I added textbox controls to a form when user selects amount to create from a combobox as follows: 'Load up the combobox Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.