|
分類:[.NET 全般]
質問させてください。
空欄補充のクイズフォームを作っています。
空欄に補充はできるのですが、button1で初期状態に戻したいのですが、どのようにコードを書けばよいでしょうか。
よろしくお願いします。
Public Class Form4
Private Label_p4 As Point, Label_p5 As Point, Label_p6 As Point
Private rect1 As Rectangle, rect2 As Rectangle, rect3 As Rectangle
Private sender_Label4 As Label, sender_Label5 As Label, sender_Label6 As Label '移動中のLabel
Private mp As Point 'マウスダウン時の座標
Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Label7.Text = "ああああああああああああああああ"
Label8.Text = "あああああああああああああああああああああああああ"
Label9.Text = "ああああああああああああああああ"
Label10.Text = "あああああああああああああああああああああああああああああああああ"
'flowlayoutpanelの中にラベルを入れる場合、flowlayoutpanelのx,yを加える!!
rect1 = New Rectangle(Label1.Left + 44, Label1.Top + 12, Label1.Width, Label1.Height)
rect2 = New Rectangle(Label2.Left + 44, Label2.Top + 12, Label2.Width, Label2.Height)
rect3 = New Rectangle(Label3.Left + 44, Label3.Top + 12, Label3.Width, Label3.Height)
Label_p4 = New Point(Label4.Left, Label4.Top) '初期位置を保管
Label_p5 = New Point(Label5.Left, Label5.Top) '初期位置を保管
Label_p6 = New Point(Label6.Left, Label6.Top) '初期位置を保管
Me.Controls.Add(Label4)
AddHandler Label4.MouseDown, AddressOf Label4_MouseDown
AddHandler Label4.MouseMove, AddressOf Label4_MouseMove
AddHandler Label4.MouseUp, AddressOf Label4_MouseUp
Me.Controls.Add(Label5)
AddHandler Label5.MouseDown, AddressOf label5_MouseDown
AddHandler Label5.MouseMove, AddressOf label5_MouseMove
AddHandler Label5.MouseUp, AddressOf label5_MouseUp
Me.Controls.Add(Label6)
AddHandler Label6.MouseDown, AddressOf label6_MouseDown
AddHandler Label6.MouseMove, AddressOf label6_MouseMove
AddHandler Label6.MouseUp, AddressOf label6_MouseUp
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Me.Close()
End Sub
Private Sub Label4_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If e.Button = MouseButtons.Left Then '左クリック
mp = Me.PointToClient(Cursor.Position) 'マウスを押した位置の座標を取得
sender_Label4 = DirectCast(sender, Label) '移動させるlabel
sender_Label4.BringToFront() '最前面へ移動
sender_Label4.BackColor = Color.Pink
sender_Label4.Cursor = Cursors.Hand 'ポインターを変更したければ
End If
End Sub
Public Sub Label4_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If e.Button = MouseButtons.Left Then '左クリック
Dim new_mp As Point = Me.PointToClient(Cursor.Position) 'マウスの最新座標
If rect1.Contains(New Point(new_mp.X, new_mp.Y)) Then
'マウスが目的のLabelの領域内に入ったら吸着させる
sender_Label4.Location = New Point(rect1.X, rect1.Y)
ElseIf rect2.Contains(New Point(new_mp.X, new_mp.Y)) Then
'マウスが目的のLabelの領域内に入ったら吸着させる
sender_Label4.Location = New Point(rect2.X, rect2.Y)
ElseIf rect3.Contains(New Point(new_mp.X, new_mp.Y)) Then
'マウスが目的のLabelの領域内に入ったら吸着させる
sender_Label4.Location = New Point(rect3.X, rect3.Y)
Else
'マウスが目的のLabelの領域外であれば自由に移動
sender_Label4.Left += (new_mp.X - mp.X)
sender_Label4.Top += (new_mp.Y - mp.Y)
End If
mp = new_mp 'マウス座標を更新
End If
End Sub
Private Sub Label4_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If e.Button = MouseButtons.Left Then '左クリック
'マウスが目的の領域外の時は移動中のLabelを元の位置に戻す
If Not (rect1.Contains(New Point(mp.X, mp.Y)) Or rect2.Contains(New Point(mp.X, mp.Y)) Or rect3.Contains(New Point(mp.X, mp.Y))) Then
sender_Label4.Location = New Point(Label_p4.X, Label_p4.Y)
End If
sender_Label4.BackColor = Color.PowderBlue
sender_Label4.Cursor = Cursors.Default 'ポインターを変更したければ
End If
End Sub
Private Sub label5_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If e.Button = MouseButtons.Left Then '左クリック
mp = Me.PointToClient(Cursor.Position) 'マウスを押した位置の座標を取得
sender_Label5 = DirectCast(sender, Label) '移動させるlabel
sender_Label5.BringToFront() '最前面へ移動
sender_Label5.BackColor = Color.Pink
sender_Label5.Cursor = Cursors.Hand 'ポインターを変更したければ
End If
End Sub
Public Sub label5_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If e.Button = MouseButtons.Left Then '左クリック
Dim new_mp As Point = Me.PointToClient(Cursor.Position) 'マウスの最新座標
If rect1.Contains(New Point(new_mp.X, new_mp.Y)) Then
'マウスが目的のLabelの領域内に入ったら吸着させる
sender_Label5.Location = New Point(rect1.X, rect1.Y)
ElseIf rect2.Contains(New Point(new_mp.X, new_mp.Y)) Then
'マウスが目的のLabelの領域内に入ったら吸着させる
sender_Label5.Location = New Point(rect2.X, rect2.Y)
ElseIf rect3.Contains(New Point(new_mp.X, new_mp.Y)) Then
'マウスが目的のLabelの領域内に入ったら吸着させる
sender_Label5.Location = New Point(rect3.X, rect3.Y)
Else
'マウスが目的のLabelの領域外であれば自由に移動
sender_Label5.Left += (new_mp.X - mp.X)
sender_label5.Top += (new_mp.Y - mp.Y)
End If
mp = new_mp 'マウス座標を更新
End If
End Sub
Private Sub label5_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If e.Button = MouseButtons.Left Then '左クリック
'マウスが目的の領域外の時は移動中のLabelを元の位置に戻す
If Not (rect1.Contains(New Point(mp.X, mp.Y)) Or rect2.Contains(New Point(mp.X, mp.Y)) Or rect3.Contains(New Point(mp.X, mp.Y))) Then
sender_Label5.Location = New Point(Label_p5.X, Label_p5.Y)
End If
sender_Label5.BackColor = Color.PowderBlue
sender_label5.Cursor = Cursors.Default 'ポインターを変更したければ
End If
End Sub
Private Sub label6_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If e.Button = MouseButtons.Left Then '左クリック
mp = Me.PointToClient(Cursor.Position) 'マウスを押した位置の座標を取得
sender_Label6 = DirectCast(sender, Label) '移動させるlabel
sender_Label6.BringToFront() '最前面へ移動
sender_Label6.BackColor = Color.Pink
sender_Label6.Cursor = Cursors.Hand 'ポインターを変更したければ
End If
End Sub
Public Sub label6_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If e.Button = MouseButtons.Left Then '左クリック
Dim new_mp As Point = Me.PointToClient(Cursor.Position) 'マウスの最新座標
If rect1.Contains(New Point(new_mp.X, new_mp.Y)) Then
'マウスが目的のLabelの領域内に入ったら吸着させる
sender_Label6.Location = New Point(rect1.X, rect1.Y)
ElseIf rect2.Contains(New Point(new_mp.X, new_mp.Y)) Then
'マウスが目的のLabelの領域内に入ったら吸着させる
sender_Label6.Location = New Point(rect2.X, rect2.Y)
ElseIf rect3.Contains(New Point(new_mp.X, new_mp.Y)) Then
'マウスが目的のLabelの領域内に入ったら吸着させる
sender_Label6.Location = New Point(rect3.X, rect3.Y)
Else
'マウスが目的のLabelの領域外であれば自由に移動
sender_Label6.Left += (new_mp.X - mp.X)
sender_Label6.Top += (new_mp.Y - mp.Y)
End If
mp = new_mp 'マウス座標を更新
End If
End Sub
Private Sub label6_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If e.Button = MouseButtons.Left Then '左クリック
'マウスが目的の領域外の時は移動中のLabelを元の位置に戻す
If Not (rect1.Contains(New Point(mp.X, mp.Y)) Or rect2.Contains(New Point(mp.X, mp.Y)) Or rect3.Contains(New Point(mp.X, mp.Y))) Then
sender_Label6.Location = New Point(Label_p6.X, Label_p6.Y)
End If
sender_Label6.BackColor = Color.PowderBlue
sender_Label6.Cursor = Cursors.Default 'ポインターを変更したければ
End If
End Sub
End Class
|