• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 538
  • Last Modified:

Loop Through Similarly Named Controls

So I have a few columns of 11 buttons. I am trying to loop through a single column at a time and hide some of them depending on a value typed in.

I am trying to use a For statement and I just can't get this.

For i = 2 to 11
     Me.Controls("BTN12560" & i).visible = False
Next

This gives me an 'Object reference not set to an instance of an object' error. I know this is probably simple, but what am I missing?

Thanks for any help you can give me.
0
G Scott
Asked:
G Scott
  • 7
  • 7
  • 2
  • +2
1 Solution
 
CluskittCommented:
For i = 2 to 11
     Me.FindControl("BTN12560" & i).visible = False
Next
0
 
käµfm³d 👽Commented:
Hmmm... Do the controls in question really look like this (with respect to their names)?

BTN125602
BTN125603
...
BTN125609
BTN1256010
BTN1256011

...because that is what either code is doing. Should "11" not be:

BTN125611
0
 
CluskittCommented:
If it's as kaufmed suggested, you can do:
For i = 2 to 11
     Me.FindControl("BTN1256" & Format(i,"00")).Visible = False
Next
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
G ScottAuthor Commented:
Cliskitt - I do not have 'FindControl' .

kaufmed -  the buttons are BTN125601 through BTN1256011. What I am trying to do is hide all buttons except for BTN125601. I am using a Select Case statement to set the visibility of buttons based on a textbox value.
0
 
CodeCruiserCommented:
There is no FindControl method on a Form class. You can try Controls.Find

For i = 2 to 11
     Me.Controls.Find("BTN12560" & i, True).visible = False
Next
0
 
G ScottAuthor Commented:
The 12560 portion of the name is a part number.
0
 
CluskittCommented:
Sorry, I'm used to ASP.Net VB. You need to use:
Me.Controls.Find("BTN12560" & i).Visible = False
0
 
G ScottAuthor Commented:
CodeCruiser..when I use that code it doesn't give me the .visible option.
0
 
G ScottAuthor Commented:
Error
0
 
käµfm³d 👽Commented:
Cast the returned control to a button. (That is to say that indexing the Controls collection returns Controls, not Buttons.)
0
 
CluskittCommented:
Again, ASP.Net habits. Sorry. In ASP.Net all controls have the visible property and the FindControl method returns a control type:
For i = 2 to 11
     CType(Me.Controls.Find("BTN12560" & i, True), Button).Visible = False
Next
0
 
G ScottAuthor Commented:
Error2
0
 
CluskittCommented:
Ok, I opened an application on VB.Net and this works:

For i = 2 to 11
     Me.Controls.Item("BTN12560" & i).Visible = False
Next
0
 
G ScottAuthor Commented:
Hello, Cluskitt. Still getting this:

Error3
I double checked all my button names and they are correct.
0
 
CluskittCommented:
When that happens it's because the control hasn't been found. Are you sure that 1- it's the correct name, and 2- your control isn't encapsulated (for example, on a gridview or similar)?
0
 
G ScottAuthor Commented:
Hey Cluskitt, I tested it on a new form and it works as well. I will make some changes to my buttons and this will work. Thanks so much for sticking with me.
0
 
CluskittCommented:
Glad to help.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Your accepted solution will only work if the buttons are contained directly by the Form itself (not in a Panel or GroupBox for instance).

Controls.Find() returns an ARRAY of matches, which is why you were getting the errors.

Here's an example that will work, no matter how deeply the buttons are nested:
        Dim matches() As Control
        For i As Integer = 2 To 11
            matches = Me.Controls.Find("BTN12560" & i, True)
            If matches.Length > 0 AndAlso TypeOf matches(0) Is Button Then
                Dim btn As Button = CType(matches(0), Button)
                ' ... do something with "btn" ...
                btn.Visible = False
            End If
        Next

Open in new window

0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 7
  • 7
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now