按名称查找计时器 [英] Find Timers by Name

查看:32
本文介绍了按名称查找计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我正在与视觉工作室合作,但遇到了一些障碍.基本情况是我有一堆彼此对应的按钮和计时器.例如当 button1 被点击时 Timer1 应该启动.

Okay I'm working with visual studio and I've hit a bit of a snag. The basic situation is I have a bunch of buttons and timers that correspond to each other. For example when button1 is clicked Timer1 should start.

目前我使用一种方法来处理所有的按钮点击.它标识 CR(1、2、3 等...)并为与之配套的正确计时器的名称构造一个字符串,dim timername as string = "timer" &cr.ToString.然后当我使用 Me.Controls(cr).Enabled = True 它返回一个空指针错误.

Currently I'm using one method to handle all of the button clicks. Which identifies the CR (1, 2, 3, etc...) and constructs a string for the name of the correct Timer that goes along with it, dim timername as string = "timer" & cr.ToString. Then when I use Me.Controls(cr).Enabled = True it returns an a null pointer error.

我知道这个问题与定时器的识别有关,有什么建议吗?

I know the issue has to do with the identification of the timer, suggestions?

推荐答案

计时器是 Component 不是 Control,因此它不会位于控件集合中.在这种情况下,最好使用通用按钮点击处理程序,因为它不会简化任何事情.

A Timer is a Component not a Control so it will not be located in the Control Collection. This is a case where it is probably better to not use a common button click handler since it is not simplifying anything.

但是,从 Object 继承的所有东西,例如 Button,都有一个 Tag 属性,您可以使用该属性将事物与该对象相关联.在表单加载中:

However, everything which inherits from Object, such as a Button, has a Tag property which you can use to associate things with that object. In form load:

Button1.Tag = Timer1
Button2.Tag = Timer2
Button3.Tag = Timer3

然后点击事件:

Private Sub ButtonX_Click(... etc ) Handles Button1.Click, Button2.Click ...

   Dim thisBtn As Button = CType(sender, Button)
   Dim thisTmr As Timer = Ctype(thisBtn.Tag, Timer)

   thisTmr.Start
End Sub

这篇关于按名称查找计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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