Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have label name as label1,label2 and label3 and i need to visible true false when i enter value of label in textbox like 1 or 2 or 3 and i click on button then label1.visible=true
Posted

The question isn't entirely clear. I'm assuming you mean:

"I would like to show or hide label controls depending on the content of a single textbox."

If that's right, I'd solve it (in C#) as follows:

<br />
label1.visible = (Textbox1.Value == "Value for Label1 to show");<br />
label2.visible = (Textbox1.Value == "Value for Label2 to show");<br />
label3.visible = (Textbox1.Value == "Value for Label3 to show");<br />


That way, you are both showing and hiding the labels, so the visibility doesn't stick around after you want it to. Of course, if you want to test against "1", "2" and "3", just change the strings.
 
Share this answer
 
Comments
fjdiewornncalwe 1-Nov-12 14:02pm    
+5.
OK, so set your click event on your button to look at the value of your textbox and then set the appropriate label's visible value to true. Something like this:
VB
SELECT CASE textbox1.Value
   CASE "1"
      Label1.Visible = true
   CASE "2"
      Label2.Visible = true
etc.


Edit
Based upon your comment about having 30 labels, I would say that the only other way to do this would be to use the Find method of the Controls object to find the label that matches the one you are looking for. Basically, you would build a string combining the constant "label" plus the value of the text box. That way you would get "label1" when there was a 1 in the textbox. Next, you put that value into the Find method and put the results into a For Each loop. You should get only one but the benefit to this is that you don't have to do any error checking for zero - nothing will happen if you enter an invalid value. Inside the loop, you will need to cast the control as a label and then set the visible property to true. If you want all the labels to be invisible except the one and you are planning on running this multiple times, you will need to set every label to false before you set the one to true here.

Here is a StackOverflow question that has some details for you, including a link to the MSDN page for Find:
http://stackoverflow.com/questions/1178967/how-do-i-refer-to-a-windows-form-control-by-name-c-vb[^]
 
Share this answer
 
v2
Comments
fjdiewornncalwe 1-Nov-12 14:01pm    
+5.
hareshdgr8 2-Nov-12 1:13am    
i have a 30 labels.... then this code is not perfect....
Tim Corey 2-Nov-12 8:57am    
Well, I'm not sure that I would agree with that statement. A SELECT statement is probably your most efficient option. However, I have given you an alternative in my answer. The best alternative, though, would be to change how you approach this issue if you can. Having 30 labels on a form that are usually hidden seems like a waste of resources and a pain to manage. Can you programmatically add one label based upon the value of the textbox on the button click? That would seem simpler.
Rad.Application 2-Nov-12 9:21am    
+5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900