Im writing a piece of software with over 200 text box's in it, most of which are not displayed until certain criteria are met. What im trying todo is be able to use the name of a variable inplace of the text box title in conjunction with visible = true to save myself a heap of code. EG:
Private Sub BoxShow()
LabelValue.Visible = True <- which dosnt work ofcourse
Count = +1
End Sub
LabelValue is being set as the name of the text box, but it wont even compile, it really dosnt like me doing that(labelvalue is a string too).. Any ideas would be appreciated.

Using a Variable inplace of a Textbox name...
dbough
Steven Rosenthal
Now I dont know the scope of the variable LabelValue and I can see that you are not using option explicit on as you are not having to declare the variable prior to using.
Private Sub BoxShow(LabelName as string)
Me.Controls(LabelName).Visible = True
End Sub
Private Sub ArtifactRarityLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ArtifactRarityLabel.Click
LabelValue = "ArtifactRarity"
BoxShow(LabelValue)
End Sub
This way you know your passing the name correctly to the BoxSHow routine. Otherwise you would have to look at the scope of the labelvalue variable. Its definately set in the ArtifactRarityLabel_Click event but is its scope local to this routine and hence in boxshow the labelvalue has no value.
In the code above you are actually pasing a control name to box show.
nikkah
Russell Mangel
Verify that the control 'ArtifactRarity' actually exists. First change the code to this
Private Sub BoxShow()
Count = +1
Me.ArtifactRarity.Visible = True
End Sub
If this works (if the control doesn't exist, it won't even compile), try this
Private Sub BoxShow()
Count = +1
Me.Controls("ArtifactRarity").Visible = True
End Sub
If both of these work, you're at least sure you're not dealing with a typing error
rezord
Object reference not set to an instance of an object.
BUT it is... i think
Private Sub BoxShow()
Count = +1
Me.Controls(LabelValue).Visible = True
End Sub
Private Sub ArtifactRarityLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ArtifactRarityLabel.Click
LabelValue = "ArtifactRarity"
BoxShow()
Count = +1
End Sub <- this IS running and setting the variable(i checked with break points), and the text box is called exactly that... so now im really confused...
Beatriz Costa - MSFT
Every control on a form is added to the controls collection when the form loads. You can use the controls collection to retrieve a control by it's name. Since all controls are stored in the collection, they items in the collection are of type Control, you can access the Visible property directly.
Dim
ControlName As StringControlName =
"Textbox1" Me.Controls(ControlName).Visible = FalseIf you would need a property of the TextBox control, you would have to cast the control first.
yingfeng
Module Module1
Public Count As Integer
Public LabelValue As String
End Module
so the variables (should be) are always there... this is getting really confusing - i havnt been able to test today - just finished a 12 hour day at work and im buggered(another tomorrow) so ill crash now and try to work on it tomorrow after work.
PS - for the record being a chef is NOT a good idea - especially a head chef :sigh:
DD100
The code you have 'seems' ok (assuming ArtifactRarity exists), but Spotty has a very good point there. You should add an 'Option Explicit' (Require Variable Declaration) to you module/class. This way, any out-of-scope issues will be caught at compile time.
Also passing the name of the control to the BoxShow method makes more sense than using a public declared variable.