那不是我的代碼
Private Sub SetAllLabelsForeColor(ByVal parent As Control)
For Each c As Control In parent.Controls
If TypeOf (c) Is Button And c.BackColor = Color.Red Then
c.FlatAppearance.MouseOverBackColor = Color.White 'I AM STUCK HERE
Else
If c.HasChildren Then
SetAllLabelsForeColor(c)
Dim b = DirectCast(c, Button)
b.FlatAppearance.MouseOverBackColor = Color.White 'I AM STUCK HERE
End If
End If
Next
End Sub
uj5u.com熱心網友回復:
您使用的是 WinForms 還是 WPF?
如果您使用 WPF,則按鈕具有
擴展方式
Public Module ControlExtensions
<Runtime.CompilerServices.Extension>
Public Iterator Function Descendants(Of T As Class)(control As Control) As IEnumerable(Of T)
For Each child As Control In control.Controls
Dim thisControl As T = TryCast(child, T)
If thisControl IsNot Nothing Then
Yield thisControl
End If
If child.HasChildren Then
For Each descendant As T In child.Descendants(Of T)()
Yield descendant
Next
End If
Next
End Function
End Module
表格代碼
Public Class Form1
Private Sub ChangePropertiesButton_Click(sender As Object, e As EventArgs) _
Handles ChangePropertiesButton.Click
Dim Panel1Buttons = Panel1.Descendants(Of Button).Where(
Function(b) b.FlatStyle = FlatStyle.Flat AndAlso b.BackColor = Color.Red).ToList()
For Each panel1Button As Button In Panel1Buttons
panel1Button.FlatAppearance.MouseOverBackColor = Color.White
Next
ListBox1.DataSource = Panel1Buttons.Select(Function(button) button.Name).ToList()
End Sub
End Class
回到您的代碼, SetAllLabelsForeColor 是該方法功能的壞名稱,即使您從某處復制粘貼總是為方法和控制元件提供有意義的名稱。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/393720.html
標籤:网络
上一篇:我遇到例外,請幫我解決
