I am new to VB.net and am doing some coding on a simple game to teach myself some of the basic elements of the language. I am stuck on one small thing right now, and have searched for a solution, with no luck. Here is my problem:
I want to be able to change the text property of a label using variables. For example, the label text that I want to access is:
lblIndustrials130.text
But I want to be able to access it using variables within the program:
"lbl" & strCommodity & intCommodityWorth & ".text"
I want to access a certain label based on certain variables. I hope this is clear enough, can someone help a new person?
Access Label through variables
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Ezdehar Noor43 pointsPosted Apr 16, 2006, 1:57 PM EDT
Hi
Can u explain more plz
also to change the text in your label i think we must write
"lbl" & strCommodity & intCommodityWorth & lblIndustrials130.text
Solitaire85 pointsPosted Apr 18, 2006, 4:22 PM EDT
Did you try assigning a value to the text property?
First you need to convert intCommodityWorth to a string and trim any extra spaces. Don't add ".text" to the string. That is the property, not the string itself. Both the strCommodity and the intCommodityWorth variables need to have values assigned to them beforehand. For example, this works:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strCommodityWorth As String, strCommodity As String, intCommodityWorth As Integer
intCommodityWorth = 990
strCommodity = "Commodity"
strCommodityWorth = Convert.ToString(intCommodityWorth).Trim()
lblIndustrials130.Text = "lbl" & strCommodity & strCommodityWorth
End Sub