我正在做一個彈丸運動專案,我想要在你按下按鈕時播放實時影片。該按鈕打開另一個表單并讓彈丸移動我需要從第一個表單呼叫一個方法
Public Class Projectile
Public Sub UpdatePosition()
Left = Left 10
End Sub
End Class
這是第一種形式的代碼
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
For Each control As Control In Controls
If TypeOf control Is Projectile Then
End If
Next
End Sub
這就是我想呼叫 UpdatePosition 的地方。
我怎樣才能做到這一點?
uj5u.com熱心網友回復:
我們需要control將For Each回圈If中的Projectile變數強制轉換為變數。我們可以使用以下OfType(Of T)方法簡化它:
For Each control As Projectile In Controls.OfType(Of Projectile)()
control.UpdatePosition()
Next
該OfType()呼叫If為我們處理了演員表和檢查。
uj5u.com熱心網友回復:
您的課程需要Inherits Control,否則將無法在Controls集合中找到。
將控制元件強制轉換為使用UpdatePosition方法的型別。
Public Class Projectile
Inherits Control
Public Sub UpdatePosition()
Left = Left 10
End Sub
End Class
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
For Each control As Control In Controls
If TypeOf control Is Projectile Then
DirectCast(control, Projectile).UpdatePosition()
End If
Next
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/361424.html
