点击事件的标签控件 [英] Label Controls on click event

查看:81
本文介绍了点击事件的标签控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的标签名称为label1,label2和label3,当我在文本框(例如1或2或3)中输入label的值,然后单击按钮然后label1.visible = true


如果是正确的话,我可以按以下方法解决(在C#中):

<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 />



这样,您既可以显示也可以隐藏标签,因此可见性不会在您想要的时候停留.当然,如果要针对"1","2"和"3"进行测试,只需更改字符串即可.


确定,因此在按钮上设置click事件以查看文本框的值,然后将相应标签的可见值设置为true.像这样的东西:

  SELECT   CASE  textbox1.Value
   案例 " 
      Label1.Visible = 
   案例 " 
      Label2.Visible =  true 
等.



编辑
基于您对拥有30个标签的评论,我想说的另一种方法是使用Controls 对象的Find 方法找到与您要查找的标签匹配的标签.基本上,您将构建一个将常量标签"与文本框的值组合在一起的字符串.这样,当文本框中有1时,您将获得"label1".接下来,将该值放入Find方法中,然后将结果放入For Each循环中.您应该只得到一个,但是这样做的好处是您不必对零进行任何错误检查-如果输入无效值,则不会发生任何事情.在循环内部,您需要将控件转换为标签,然后将visible属性设置为true.如果您希望除标签之外的所有标签均不可见,并且您打算多次运行,则在此处将每个标签设置为true之前,需要将每个标签设置为false.

这是一个StackOverflow问题,为您提供一些详细信息,包括指向Find的MSDN页面的链接:
http://stackoverflow. com/questions/1178967/how-do-i-refer-to-a-windows-form-control-by-name-c-vb [ 解决方案

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.


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:

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[^]


这篇关于点击事件的标签控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆