Visual Basic 限定的サンプル |
このサンプルはフォーム上に50個の青い四角形を配置します。これらの四角形はマウスが上に来たときには赤くなります。
サンプルはVB2005のものですが、「Windows フォームデザイナで生成されたコード」という部分がないだけで、他の部分はVB.NET2002, VB.NET2003でも完全に動作します。
必要なコントロール |
なし |
Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Const XCount As Integer = 10 '横方向の■の数 Const YCount As Integer = 5 '縦方向の■の数 Const Length As Integer = 20 '■の一辺の長さ Const Space As Integer = 2 '■と■の隙間の幅 Const Edge As Integer = 10 'フォームの左上と一番左上の■との距離 Dim ColorBoxes(XCount * YCount - 1) As ColorBox For X As Integer = 0 To XCount - 1 For Y As Integer = 0 To YCount - 1 ColorBoxes(Y * XCount + X) = New ColorBox(Color.Blue, Color.Red) ColorBoxes(Y * XCount + X).SetBounds(Edge + (Length + Space) * X, Edge + (Length + Space) * Y, Length, Length) Next Next Me.Controls.AddRange(ColorBoxes) End Sub End Class |
Public Class
ColorBox Inherits System.Windows.Forms.PictureBox Public NormalColor As Color Public ActiveColor As Color |
Public
Sub New(ByVal
NormalColor As Color,
ByVal ActiveColor As Color) Me.NormalColor = NormalColor Me.ActiveColor = ActiveColor Me.BackColor = NormalColor End Sub |
Protected
Overrides Sub
OnMouseEnter(ByVal eventargs
As System.EventArgs) Me.BackColor = ActiveColor MyBase.OnMouseEnter(eventargs) End Sub |
Protected Overrides
Sub OnMouseLeave(ByVal
eventargs As System.EventArgs) Me.BackColor = NormalColor MyBase.OnMouseLeave(eventargs) End Sub End Class |