Group,

I've expanded a small app that started with 50 textboxes and labels to 200 of each. To hide and make these visable I use the codes

lblProperty01.Visible = False
tbxProperty01.Visible = True
lblProperty02.Visible = False
tbxProperty02.Visible = True

This means there are 400 lines of code to hide and display the 200 textboxes and labels. I've got to believe there is a more efficient way to write this..... and one that will hide and display these controls much faster. Can you advise me as to what that might be?

As always, thanks for your assistance!

Don

Could you elaborate at what the conditions are that make them visible or hide these?

If you have code like var1= , var2=, var3= etc. That code is just screaming out: "Please put us in an array, or better still in a list!"

Well, he got already the controls array. As I said it depends just on the conditions. To get all labels with a condition from a textbox I would use something like:

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Application.Exit()
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim condi As String = TextBox1.Text
        For Each Cont As Label In Me.Controls.OfType(Of Label)()
            If Cont.Name.EndsWith(condi) Then
                Cont.Visible = False
            Else
                Cont.Visible = True
            End If
        Next
    End Sub

End Class

Minimalist, The "condition" to show the textboxes and hide the labels come via a the event

Private Sub btnEditPropList_Click(sender As System.Object, e As System.EventArgs) Handles btnEditPropList.Click
lblProperty01.Visible = False
tbxProperty01.Visible = True
lblProperty02.Visible = False
tbxProperty02.Visible = True
End Sub

Of course, I'm lising all 400 lines of code to make textboxes visible and labels hidend. Once the edit is done, I then run

Private Sub btnSaveList_Click(sender As System.Object, e As System.EventArgs) Handles btnSaveList.Click
' writing information to file
lblProperty01.Visible = True
tbxProperty01.Visible = False
lblProperty02.Visible = True
tbxProperty02.Visible = False
End Sub

Which hides the textboxes and shows the labels.

I'm trying to understand the code you're writing. However I do not have a textbox called "TextBox1".
Dim condi As String = TextBox1.Text
Should I replace this with Dim condi As String = tbxProperty01.Text? I would have assumed that you'd create a loop or to say something to the effect of
Dim condi As String
For i = 1 to 200
If i < 10 Then
pNo = '0' & i
Else pNo = i
End If
condi = tbxProperty(pNo).Text (or something to this effect. I know writing it that way won't work. I'm not sure how to do it correctly - assuming it can be done).

I've not seen
For Each Cont As Label In Me.Controls.OfType(Of Label)()
before, so I don't know what this means or does. Since it begins with "For", I gather it is a loop.

I hope you understand a little better. Thanks again for the help. I love learning this stuff!!

Don

Every vb.net program has colections of the controls that are contained within. You can get all the controls with something like:

  For Each ctl As Control In Me.Controls
    'do something
     Next

which will loop theough all controls.
With my code I restrict the controls to the type of label:

 For Each Cont As Label In Me.Controls.OfType(Of Label)()
           'do something
        Next

Cont just means control.
So, since we running through all the controls you don't need another loop.
So, cont.name is a controls name,in this xase a label, and we use this to get to the control you want by using the how the name text ends: Inyour case with a number, this what
If Cont.Name.EndsWith(condi) Then
does since cont.name is the name of the label, which is text whixh I feed through textbox1.text.
So the only thing you have to do replace my names with what you want, place a second for .. next loop for the textboxes and the apply your condition.

How about putting the controls you want to show/hide as a set into a groupbox. Then you can just make the groupbox visible or not to show/hide all of the controls in it.

Rev. Jim,

Truthfully, I thought about that as I saw the suggestion somewhere else. But I thought about trying to learn the most efficient way to show and hide the multiple textboxes and labels I'm using.

Thanks everyone for the suggestions. Now to give these a try.

Don

The most efficient way will be hiding a groupbox. It takes only one assignment.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.