473,738 Members | 10,068 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reference dynamically created control by name

I would like to reference a dynamically created control and I know the name.
I would like to use the following:

Dim strName as String = "txtControl 1"
' This is the ".Name" used when textbox was dynamically created

dim c as control = me.Controls(str Name)

Instead I have to do this:

Public Function ControlByName _
(ByVal strName As String _
, ByVal ctrlCol As Windows.Forms.C ontrol.ControlC ollection _
) As Control
If ctrlCol Is Nothing Then Return Nothing
For Each c As Control In ctrlCol
If c.Name = strName Then Return c
Next
Return Nothing
End Function

dim c as control = ControlByName(s tr, Me.Controls)
Isn't there a better way???

+++++++++++++++ +++++++++++++++ +++++++++++++++ ++

Next Item:

When cloning a control I do something like this:

For Each c0 As Control In t0.Controls
Dim c As System.Windows. Forms.Control
Select Case c0.GetType.ToSt ring
Case "System.Windows .Forms.TextBox"
c = New TextBox
CType(c, TextBox).TextAl ign = CType(c0, TextBox).TextAl ign
strText = c.Text
Case "System.Windows .Forms.Label"
c = New Label
CType(c, Label).TextAlig n = CType(c0, Label).TextAlig n
strText = c.Text
Case "System.Windows .Forms.ComboBox "
c = New ComboBox
For Each p As Object In CType(c0, ComboBox).Items
CType(c, ComboBox).Items .Add(p)
Next
CType(c, ComboBox).Selec tedItem = CType(c0, ComboBox).Selec tedItem
strText = CStr(CType(c0, ComboBox).Selec tedItem)
End Select
c.Visible = True 'c0.Visible '<- BUG IN CONTROL
Next

Note the last line: I have to set ".Visible" to true since the c0 has the
wrong value as retreived from the control collection. Is this a bug or is it
something I'm doing???

Is it because ">Visible" is really
c0.Visible = (c0.Parent.Pare nt.Visible AND c0.Parent.Visib le AND c0.Visible)

Nov 21 '05 #1
7 7585
Mef,

When you want this it is in my opinion easier to create a hashtable where
you place the name in the key and the reference to the control in the value.

http://msdn.microsoft.com/library/de...classtopic.asp

I hope this helps?

Cor

"mef526" <me****@discuss ions.microsoft. com>
I would like to reference a dynamically created control and I know the
name.
I would like to use the following:

Dim strName as String = "txtControl 1"
' This is the ".Name" used when textbox was dynamically created

dim c as control = me.Controls(str Name)

Instead I have to do this:

Public Function ControlByName _
(ByVal strName As String _
, ByVal ctrlCol As Windows.Forms.C ontrol.ControlC ollection _
) As Control
If ctrlCol Is Nothing Then Return Nothing
For Each c As Control In ctrlCol
If c.Name = strName Then Return c
Next
Return Nothing
End Function

dim c as control = ControlByName(s tr, Me.Controls)
Isn't there a better way???

+++++++++++++++ +++++++++++++++ +++++++++++++++ ++

Next Item:

When cloning a control I do something like this:

For Each c0 As Control In t0.Controls
Dim c As System.Windows. Forms.Control
Select Case c0.GetType.ToSt ring
Case "System.Windows .Forms.TextBox"
c = New TextBox
CType(c, TextBox).TextAl ign = CType(c0, TextBox).TextAl ign
strText = c.Text
Case "System.Windows .Forms.Label"
c = New Label
CType(c, Label).TextAlig n = CType(c0, Label).TextAlig n
strText = c.Text
Case "System.Windows .Forms.ComboBox "
c = New ComboBox
For Each p As Object In CType(c0, ComboBox).Items
CType(c, ComboBox).Items .Add(p)
Next
CType(c, ComboBox).Selec tedItem = CType(c0, ComboBox).Selec tedItem
strText = CStr(CType(c0, ComboBox).Selec tedItem)
End Select
c.Visible = True 'c0.Visible '<- BUG IN CONTROL
Next

Note the last line: I have to set ".Visible" to true since the c0 has the
wrong value as retreived from the control collection. Is this a bug or is
it
something I'm doing???

Is it because ">Visible" is really
c0.Visible = (c0.Parent.Pare nt.Visible AND c0.Parent.Visib le AND
c0.Visible)

Nov 21 '05 #2
"mef526" <me****@discuss ions.microsoft. com> schrieb:
I would like to reference a dynamically created control
and I know the name.


Accessing controls by their names or indices
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=controlbyna meindex&lang=en >

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #3
Herfried,

That recursive method is I thought one which I created and showed the first
time in this newsgroups in this way (it is a little bit changed however not
basicly).

I avoided that expresly in this case because with dynamicly created controls
the hasttable is in my opinion better when the controls are not already
placed in an array of controls ( as I do because in that case because I can
add the handlers as well in a loop).

In that case it is finding the name just looping through that array.

Cor

"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at>
"mef526" <me****@discuss ions.microsoft. com> schrieb:
I would like to reference a dynamically created control
and I know the name.


Accessing controls by their names or indices
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=controlbyna meindex&lang=en >

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 21 '05 #4
Thank you for your answer

I think that the method I am now using will work best given that the
ControlContaine r in the code, a TabPage, only has 5 items. It is dynamically
created as a clone of the selected TabPage.

What about Item 2:
Note the last line: I have to set ".Visible" to true since the c0 has the
wrong value as retreived from the control collection. Is this a bug or is it
something I'm doing???

Is it because ".Visible" is really
c0.Visible = (c0.Parent.Pare nt.Visible AND c0.Parent.Visib le AND c0.Visible)
"Cor Ligthert" wrote:
Herfried,

That recursive method is I thought one which I created and showed the first
time in this newsgroups in this way (it is a little bit changed however not
basicly).

I avoided that expresly in this case because with dynamicly created controls
the hasttable is in my opinion better when the controls are not already
placed in an array of controls ( as I do because in that case because I can
add the handlers as well in a loop).

In that case it is finding the name just looping through that array.

Cor

"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at>
"mef526" <me****@discuss ions.microsoft. com> schrieb:
I would like to reference a dynamically created control
and I know the name.


Accessing controls by their names or indices
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=controlbyna meindex&lang=en >

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>


Nov 21 '05 #5
Mef526

When I understand your message right, than I think that I have nothing to
disagree with you. (You can set an "exit for", when you think on more
controls than 5 when found, which needs a little bit change of the "then"
clause). I do not like that inline thens so maybe that is the reason I do
that directly.

:-)

Cor
"mef526" <me****@discuss ions.microsoft. com>
Thank you for your answer

I think that the method I am now using will work best given that the
ControlContaine r in the code, a TabPage, only has 5 items. It is
dynamically
created as a clone of the selected TabPage.

What about Item 2:
Note the last line: I have to set ".Visible" to true since the c0 has the
wrong value as retreived from the control collection. Is this a bug or is
it
something I'm doing???

Is it because ".Visible" is really
c0.Visible = (c0.Parent.Pare nt.Visible AND c0.Parent.Visib le AND
c0.Visible)
"Cor Ligthert" wrote:
Herfried,

That recursive method is I thought one which I created and showed the
first
time in this newsgroups in this way (it is a little bit changed however
not
basicly).

I avoided that expresly in this case because with dynamicly created
controls
the hasttable is in my opinion better when the controls are not already
placed in an array of controls ( as I do because in that case because I
can
add the handlers as well in a loop).

In that case it is finding the name just looping through that array.

Cor

"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at>
> "mef526" <me****@discuss ions.microsoft. com> schrieb:
>> I would like to reference a dynamically created control
>> and I know the name.
>
> Accessing controls by their names or indices
> <URL:http://dotnet.mvps.org/dotnet/faqs/?id=controlbyna meindex&lang=en >
>
> --
> Herfried K. Wagner [MVP]
> <URL:http://dotnet.mvps.org/>
>
>


Nov 21 '05 #6
I'm not sure I understand your reply so let em explain my thought- the
COntrolCOnainer that is serched only HAS 5 items in it so the search would
not be too bad, probably this is FASTER than using a hash, since there are so
few items.

What about the .Visible property?

Is it read differently than set?

"Cor Ligthert" wrote:
Mef526

When I understand your message right, than I think that I have nothing to
disagree with you. (You can set an "exit for", when you think on more
controls than 5 when found, which needs a little bit change of the "then"
clause). I do not like that inline thens so maybe that is the reason I do
that directly.

:-)

Cor
"mef526" <me****@discuss ions.microsoft. com>
Thank you for your answer

I think that the method I am now using will work best given that the
ControlContaine r in the code, a TabPage, only has 5 items. It is
dynamically
created as a clone of the selected TabPage.

What about Item 2:
Note the last line: I have to set ".Visible" to true since the c0 has the
wrong value as retreived from the control collection. Is this a bug or is
it
something I'm doing???

Is it because ".Visible" is really
c0.Visible = (c0.Parent.Pare nt.Visible AND c0.Parent.Visib le AND
c0.Visible)
"Cor Ligthert" wrote:
Herfried,

That recursive method is I thought one which I created and showed the
first
time in this newsgroups in this way (it is a little bit changed however
not
basicly).

I avoided that expresly in this case because with dynamicly created
controls
the hasttable is in my opinion better when the controls are not already
placed in an array of controls ( as I do because in that case because I
can
add the handlers as well in a loop).

In that case it is finding the name just looping through that array.

Cor

"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at>
> "mef526" <me****@discuss ions.microsoft. com> schrieb:
>> I would like to reference a dynamically created control
>> and I know the name.
>
> Accessing controls by their names or indices
> <URL:http://dotnet.mvps.org/dotnet/faqs/?id=controlbyna meindex&lang=en >
>
> --
> Herfried K. Wagner [MVP]
> <URL:http://dotnet.mvps.org/>
>
>


Nov 21 '05 #7
Mef,

That performance is where I did mean that I cannot disagree with that when
we are talking about 5 items.

However it did seem to me a kind of generic function and therefore I added
something more..

Than to complete it to get it about the visible property.

You made it in my opinion as a generic search function . Normally this would
be done as this.
\\\
For Each ctr As Control In ctrlPar.control s
If ctr.Name = strName Then
ctr.visible = false
exit for
end if
Next
///

Using your functions it can be something as
\\\\
dim c as control = ControlByName(" mycontrolname", Panel1.controls )
if Not c Is Nothing then
c.visible = false
end if
////

All is typed in this message so watch typos.

I hope this helps?

Cor

"mef526" <me****@discuss ions.microsoft. com>
I'm not sure I understand your reply so let em explain my thought- the
COntrolCOnainer that is serched only HAS 5 items in it so the search would
not be too bad, probably this is FASTER than using a hash, since there are
so
few items.

What about the .Visible property?

Is it read differently than set?

"Cor Ligthert" wrote:
Mef526

When I understand your message right, than I think that I have nothing to
disagree with you. (You can set an "exit for", when you think on more
controls than 5 when found, which needs a little bit change of the "then"
clause). I do not like that inline thens so maybe that is the reason I do
that directly.

:-)

Cor
"mef526" <me****@discuss ions.microsoft. com>
> Thank you for your answer
>
> I think that the method I am now using will work best given that the
> ControlContaine r in the code, a TabPage, only has 5 items. It is
> dynamically
> created as a clone of the selected TabPage.
>
> What about Item 2:
> Note the last line: I have to set ".Visible" to true since the c0 has
> the
> wrong value as retreived from the control collection. Is this a bug or
> is
> it
> something I'm doing???
>
> Is it because ".Visible" is really
> c0.Visible = (c0.Parent.Pare nt.Visible AND c0.Parent.Visib le AND
> c0.Visible)
>
>
> "Cor Ligthert" wrote:
>
>> Herfried,
>>
>> That recursive method is I thought one which I created and showed the
>> first
>> time in this newsgroups in this way (it is a little bit changed
>> however
>> not
>> basicly).
>>
>> I avoided that expresly in this case because with dynamicly created
>> controls
>> the hasttable is in my opinion better when the controls are not
>> already
>> placed in an array of controls ( as I do because in that case because
>> I
>> can
>> add the handlers as well in a loop).
>>
>> In that case it is finding the name just looping through that array.
>>
>> Cor
>>
>> "Herfried K. Wagner [MVP]" <hi************ ***@gmx.at>
>> > "mef526" <me****@discuss ions.microsoft. com> schrieb:
>> >> I would like to reference a dynamically created control
>> >> and I know the name.
>> >
>> > Accessing controls by their names or indices
>> > <URL:http://dotnet.mvps.org/dotnet/faqs/?id=controlbyna meindex&lang=en >
>> >
>> > --
>> > Herfried K. Wagner [MVP]
>> > <URL:http://dotnet.mvps.org/>
>> >
>> >
>>
>>
>>


Nov 21 '05 #8

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

Similar topics

3
4296
by: crjunk | last post by:
I have a web page that displays multiple records for a company. The input text boxes that display my data are created dynamically. I'm creating these input boxes dynamically because the number of records for each company varies. The name of each input box increments by one for each record (Ex: A1, B1, C1 | A2, B2, C2, etc..) On my web page the user will enter in a number into the txtGrossPayroll(some number) input box. I have added...
2
1618
by: DesignerX | last post by:
I have a user control that contains a custom control, both are loaded dynamically. The custom control has a simple required field validator and a text box to validate. When the submit button in the user control is pressed the textbox does not seem to be validated. How do I even begin to troubleshoot this?
0
2116
by: John Crowley | last post by:
I'm having an odd problem with viewstate and a dynamically created control inside a repeater template. Basically, I have a repeater setup like this in the aspx:
3
1966
by: Jen | last post by:
Hi I have created some controls (HtmlInputText, HtmlGenericControl, TextBox) dynamically. But I have problem getting the values from these controls to save them. Is there a way to do that? Regards and Many thanks Jen
2
2928
by: R Duke | last post by:
I have tried everything I can think of to change the visible property of a design time created control from a dynamically created control's command event handler. Here is the scenario. I have a WebForm with some textboxes, dropdownlists, a panel, imagebutton and so on. When I click on the image button (which was created at design time) I dynamically build a table. In each of row of that new table I put several cells and one cell...
1
1479
by: Mike | last post by:
I have a placeholder object in which I am dynamically placing controls based on a database query. Of course during postback these controls go away. I have seen other questions posed where someone wants the dynamically created controls to appear on the page. However in my case I don't really need to have the control displayed after postback, but I do need its value. When I look at the trace output from the page after postback, it lists the value...
1
1850
by: Nirmalkumar | last post by:
How to attach an event to dynamically created control in VB.NET I have dynamically created dropdown list in code behind (VB.NET). For this control I want to attach an event for action ‘OnSelectedIndexChanged’. What is the property to assigne an event to a dynamically created dropdownlist
3
4306
by: RSH | last post by:
Hi, I have a situation where I have a page built in .Net 1.1 that I have a PlaceHolder in the Design. From the CodeBehind I am dynamically referencing a user control. This is a snippet from the the CodeBehind from that page: Dim mc As MainContent = New MainContent <----User Created Control
5
1499
by: arnabit | last post by:
I have created a dropdown ,which is inside a panel and the panel is inside a place holder . the panel and the dropdown is created dynamically. I do have a button when the button is clicked i am trying to read the values from the dropdown.but the problem i am facing is that i am not getting the id of the dropdown .Please advice me how to get the id of the dropdown .
0
8969
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9476
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9335
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9263
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6751
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6053
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3279
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.