473,386 Members | 1,621 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,386 software developers and data experts.

Trouble adding control to control collection

11
Im trying to add the textboxes in a form to a control collection which I will test later. For some reason my control collection ends up empty. None of the controls are added. Any help would be greatly appreciated. Code follows.

Expand|Select|Wrap|Line Numbers
  1. Dim contcoll As ControlCollection
  2. Dim cont As Control
  3.  
  4. contcoll = New ControlCollection(Me)
  5.  
  6. For Each cont In Me.Controls
  7.      If TypeOf cont Is TextBox Then
  8.           contcoll.Add(cont)
  9.      End If
  10. Next
Oct 23 '07 #1
8 2035
Plater
7,872 Expert 4TB
If your controls are in a Container control (such as a groupbox) they will not show up as being in Me.Controls.
You would have to examine all the controls in Me.Controls[] for the .HasChildren and if so, search through them(and their children and so on)
Oct 23 '07 #2
Suikwan
11
If your controls are in a Container control (such as a groupbox) they will not show up as being in Me.Controls.
You would have to examine all the controls in Me.Controls[] for the .HasChildren and if so, search through them(and their children and so on)
The controls were added directly to the fom and are not in any container.
Oct 23 '07 #3
Plater
7,872 Expert 4TB
Have you tried seing whats in there?

With say this (I might not have the foreach declration correct)
Expand|Select|Wrap|Line Numbers
  1. For Each Control c In Me.Controls
  2.    MessageBox.Show(c.GetType().Name);
  3. Next
  4.  
Oct 23 '07 #4
Suikwan
11
Have you tried seing whats in there?

With say this (I might not have the foreach declration correct)
Expand|Select|Wrap|Line Numbers
  1. For Each Control c In Me.Controls
  2.    MessageBox.Show(c.GetType().Name);
  3. Next
  4.  
Yes, I did it by outputting the name of each control to a text file and the list was correct.

Expand|Select|Wrap|Line Numbers
  1. For i = 0 To (me.controls.Count - 1)
  2.        File.AppendAllText("c:\testingemailallcontrols.txt", me.controls.Item(i).Name & vbCrLf)
  3.         'Next
Oct 23 '07 #5
Plater
7,872 Expert 4TB
Hmm, well I don't know why your collection is empty.

Do you need to make it be:
Expand|Select|Wrap|Line Numbers
  1. For Each cont In Me.Controls.Items
  2.  
?

Don't you need to compare the types of either side?
Expand|Select|Wrap|Line Numbers
  1. If TypeOf cont Is TypeOf TextBox Then
  2.  
Did you then try to add EVERY control from Me.Controls into your collection?
Oct 23 '07 #6
Suikwan
11
Hmm, well I don't know why your collection is empty.

Do you need to make it be:
Expand|Select|Wrap|Line Numbers
  1. For Each cont In Me.Controls.Items
  2.  
?

Don't you need to compare the types of either side?
Expand|Select|Wrap|Line Numbers
  1. If TypeOf cont Is TypeOf TextBox Then
  2.  
Did you then try to add EVERY control from Me.Controls into your collection?
The original syntax is correct. I tried adding all the controls to the control collection and then removing the ones that are not textboxes but not all the non-textbox controls are removed. The two buttons are removed but only six of the nine labels are removed.

Expand|Select|Wrap|Line Numbers
  1. Dim contcoll As ControlCollection = Me.Controls
  2. Dim cont As Control
  3.  
  4. For Each cont In contcoll
  5.    If Not TypeOf cont Is TextBox Then
  6.        contcoll.Remove(cont)
  7.    End If
  8. Next
By the way, how do you get it to recognize that the text posted is vb code?
Oct 23 '07 #7
Plater
7,872 Expert 4TB
I think there is something else wrong here, I will investigate reproducing it.




EDIT: AHHA!!

It's because of the nature of ControlCollection! If you look at the description on the .Add() you will see that it says "Adds a control to the form". Which explains why the contructor requires a Form argument.
All you're doing is adding the control back to the same form.

Try this:
Expand|Select|Wrap|Line Numbers
  1. Dim contcoll As New List(Of Control)() 
  2. For Each cont As Control In Me.Controls 
  3.     If TypeOf cont Is TextBoxThen 
  4.         contcoll.Add(cont) 
  5.  
  6.     End If 
  7. Next 
  8.  
You can use:
[code=vbnet] instead of [code]
Oct 23 '07 #8
Suikwan
11
I think there is something else wrong here, I will investigate reproducing it.




EDIT: AHHA!!

It's because of the nature of ControlCollection! If you look at the description on the .Add() you will see that it says "Adds a control to the form". Which explains why the contructor requires a Form argument.
All you're doing is adding the control back to the same form.

Try this:
Expand|Select|Wrap|Line Numbers
  1. Dim contcoll As New List(Of Control)() 
  2. For Each cont As Control In Me.Controls 
  3.     If TypeOf cont Is TextBoxThen 
  4.         contcoll.Add(cont) 
  5.  
  6.     End If 
  7. Next 
  8.  
You can use:
[code=vbnet] instead of [code]
SWEEEEEEEEEEEEET!!!!

Thanks for the help. Hopefully the bruises on my head will heal soon :)
Oct 23 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
1
by: Mike Malter | last post by:
I take it that the only way to add rows to a DataList control is through DataBind? If not, how do I manually add rows to a DataList control? I have searched everywhere and can't find an example...
4
by: DotNetJunky | last post by:
I have built a control that runs an on-line help system. Depending on the category you selected via dropdownlist, it goes out and gets the child subcategories, and if there are any, adds a new...
3
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
16
by: scorpion53061 | last post by:
Well as some of you know I was using a tab control for a project I was building for my boss. Today he tells me that he wants: When he switches tabs to be able to switch back and see whatever...
9
by: Kadett | last post by:
Hi all, I have following problem: I'm creating a ListView (Details) control at run-time and filling it with some records (let's say 10 000). This operation seems to be quite fast, but when I call...
2
by: moondaddy | last post by:
I'm trying to do something real simple. Add a thumb to a grid at the end of a line using c#. the code executes but I dont see the thumb. Can anyone explain what and what I need to do to properly...
1
by: 12jumper | last post by:
Hi All :) I've created an UserControl, which has a couple of properties that are collections (generic lists of some objects). It would be nice, if control refreshed each time I add a new item to...
3
by: Nunzio | last post by:
I am trying to add specific form controls to a VBA collection, so that I can pass the entire collection to another function, and access the individual controls later. I have tried several...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
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.