简体   繁体   English

单击时注册为按钮的表单的vb.net背景

[英]vb.net background of form registered as a button when clicked

I have created a system in vb.net that allows you to click buttons and it will change colour accordingly. 我在vb.net中创建了一个系统,该系统允许您单击按钮,并且它将相应地更改颜色。 It uses code that will be used for each button without referencing each button individually. 它使用的代码将用于每个按钮,而无需单独引用每个按钮。

Dim btn As Button = Nothing  
For Each ctrl As Control In Me.Controls  
    If TypeOf ctrl Is Button Then  
       btn = DirectCast(ctrl, Button)  
       AddHandler btn.Click, AddressOf Me.btn_Click  
    End   
Next  

This works correctly for the buttons, however when you miss-click and click the background of the form it registers the background as a button and breaks the system. 这对于按钮来说是正确的,但是当您单击并单击表单的背景时,它将背景注册为按钮并中断了系统。

Hope this makes sense. 希望这是有道理的。

You can use the Me.Controls.OfType() function which will return a collection with the Controls of the specified Type: 您可以使用Me.Controls.OfType()函数,该函数将返回具有指定Type的控件的集合:

Me.Controls.OfType(Of Button)().ToList.
   ForEach(Sub(bt As Button) AddHandler bt.Click, AddressOf Me.btn_Click)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM