在Visual Basic中连接控件,控制控件 [英] Wiring Controls in Visual Basic, Controlling the Controls

查看:110
本文介绍了在Visual Basic中连接控件,控制控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Visual Basic(Visual Studio 2010)创建动态创建的控件。本质上,我正在做的是创建标签,文本框,标签(将用作秒表)和按钮(以控制所述秒表)。

I'm in the middle of using Visual Basic (Visual Studio 2010) to create dynamically created controls. Essentially what I'm doing is creating a label, a textbox, a label (which will act as a stopwatch), and a button (to control said stopwatch).

每组控件都将像这样连续排列(并命名):

Each set of controls will be arranged (and named) like this in a row:


[LABEL]  [TEXTBOX]         [TIMER]       [BUTTON]
Labelx   ParticipantNamex  RingTimerx    ControlButtonx

对于给定的行,我将看起来像这样:

So for a given row, I will be look like this:


[LABEL]  [TEXTBOX]         [TIMER]       [BUTTON]
Label1   ParticipantName1  RingTimer1    ControlButton1

我已经了解了动态创建元素以及在面板中创建元素的过程,该面板在我创建的表单的名称末尾附加了一个特定的数字。我想做的是为按钮创建一个事件,该按钮是动态创建的,用于控制通过同一事件创建的秒表计时器。

I've gotten the bit about creating the elements dynamically as well as creating them within in a panel with a particular number attached to the end of its name on the form that I've created. What I would like to do is wire an event for the button that was dynamically created to control the stopwatch timer that was created through the same event.

所以,简而言之,我在问您如何连接事件以控制特别动态的按钮?

So in short, I'm asking how do you wire an event to control a particularly dynamic button?

推荐答案

使用匿名子(仅VB2010)内联编写事件处理程序

Using an anonymous sub (VB2010 only) to write the event handler inline

Timer myTimer = New Timer
Button button = New Button
AddHandler button.Click,
    Sub(s As Object, e As EventArgs)
         ' can manipulate the Timer here 
         ' because it is captured in a closure     
         myTimer.Stop    
    End Sub

改编自此处

PS阅读更多关于闭包的信息来自我们自己的Jared。

PS read more about closures from our very own Jared.

这篇关于在Visual Basic中连接控件,控制控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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