Question Change visibility property of textfield using loop

Erika_R

Member
Joined
May 18, 2023
Messages
6
Programming Experience
Beginner
Hello,

I want to change the textbox property visible depending on the value of I, something like this:
the names of my textboxes are LBL1 LBL2 LBL3 …..

depending of the result of a value I want to set the textbox property to visible.

i = 5 (5 is the result of a test)

”LBL” + i.visible= true

Is it possible?

Thanks in advance,

Erika
 
Last edited:
As for the question, you can retrieve a control by name from its parent's Controls collection, so you can do this:
VB.NET:
Dim ctrl = Me.Controls("LBL" & i)
That will give you a Control reference, so you can access any members of that type, which Visible is. If you need something specific to a TextBox, you can cast as that type:
VB.NET:
Dim ctrl = DirectCast(Me.Controls("LBL" & i), TextBox)
Note that, if the control is in a container like a Panel or GroupBox, use that instead of the form, i.e. in place of Me.
 
Hello, thank u for your reply.
I’m a vb.net beginner, can you tell me why me.controls is underlined in red? Is it because of a missing library?
 
A WPF TextBox doesn't have a Visible property. It has an IsVisible property. A WinForms TextBox has a Visible property, so I naturally assumed that that was what you were using. Using the actual names of things is important. So is posting in the most appropriate forum for the topic. Given that this site has a both WPF and WinForms forums, this thread should not have been posted in the VB.NET General forum in the first place.
 
A WPF TextBox doesn't have a Visible property. It has an IsVisible property. A WinForms TextBox has a Visible property, so I naturally assumed that that was what you were using. Using the actual names of things is important. So is posting in the most appropriate forum for the topic. Given that this site has a both WPF and WinForms forums, this thread should not have been posted in the VB.NET General forum in the first place.

I’m sorry but i did not know that there was a major difference between winforms and wpf forms. Like i said i’m not that experienced.
Do you think there is a solution for my wpf form problem?
 
I'm sure there is but I have hardly used WPF and not for some time. I'll look into it when I get the time, if someone else doesn't first.

Having said that, you probably shouldn't even need to. If you use WPF properly then you'd be using MVVM or some similar pattern and you would be making changes to the view model bound to the view. Ideally, you would not be working directly with controls. That's how things were generally done in WinForms but WPF was created from the ground up with the idea of not doing that right from the start.
 
I was forced to use wpf because the application uses OPC to communicate with a plc otherwise I used winforms, this is wat i also used in vba and I also better understand. Thanks, I hope that you maybe find a solution otherwise I change my code to an other solution.
 

Latest posts

Back
Top