C# と VB.NET の質問掲示板

ASP.NET、C++/CLI、Java 何でもどうぞ

C# と VB.NET の入門サイト

Re[3]: 空欄補充されたラベルの正誤を判断するには?


(過去ログ 132 を表示中)

[トピック内 4 記事 (1 - 4 表示)]  << 0 >>

■77891 / inTopicNo.1)  空欄補充されたラベルの正誤を判断するには?
  
□投稿者/ ごうき (12回)-(2015/11/27(Fri) 23:20:36)

分類:[.NET 全般] 

vb2015を使用しています。
まだまだ初心者ですが、よろしくお願いします。

質問内容ですが、今、空欄補充のクイズを作成しています。
空欄にラベルが補充されていない、つまり何も入っていないときに警告文を出すことまではできました。
ここから質問内容です。
空欄にラベルを補充して、正しいものが入っている場合は○、間違っているものが入っている場合は×といった正誤を判断し、最後に
”正しいもの ○こ   間違っているもの ○こ”といったものを表示したいのです。

使用しているコントロールはラベルとボタンのみです。配列読み込みについては、q1やq2は問題分、a1やa2は答えです。
if文を使えばいいかなとは考えたのですが、コードが思いつかないのです。
突っ込みどころ満載のコードですが、ご教授をお願いします。



Public Structure bb
Dim id As Integer
Dim q1 As String
Dim q2 As String
Dim q3 As String
Dim q4 As String
Dim a1 As String
Dim a2 As String
Dim a3 As String
Dim a4 As String
Dim d1 As String
Dim d2 As String
End Structure
Public Class Form4
Private Label_p4 As Point, Label_p5 As Point, Label_p6 As Point, Label_p7 As Point, Label_p8 As Point, Label_p9 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, sender_Label7 As Label, sender_Label8 As Label, sender_Label9 As Label '移動中のLabel
Private mp As Point 'マウスダウン時の座標
Dim x1 As Integer, k As Integer, x3 As Integer, x4 As Integer
Dim b0 As Integer, b1 As Integer, b2 As Integer, b As Integer, s As Integer
Dim c0 As Integer
Private n0 As Integer = 0
Private sd() As bb
Private nn As Integer
Private i As Integer
Private ss As Integer = 0 '済の数
Private m As Integer = 0 '未了の数

Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.FormBorderStyle = FormBorderStyle.FixedSingle
'CSVファイルを読み込み構造体の配列に保存する
Dim fn As String = "date/date1/data1.csv" 'ファイルのパス
Dim csvNum As String = 1
Dim fno As Integer = FreeFile() 'ファイル番号を取得
'ファイルを入力モードで開く
FileOpen(fno, Replace(fn, "#", csvNum), OpenMode.Input)
Do Until EOF(fno) 'ファイルの最後までループ
ReDim Preserve sd(n0)
Input(fno, sd(n0).id) 'ファイルから読み込み
Input(fno, sd(n0).q1)
Input(fno, sd(n0).q2)
Input(fno, sd(n0).q3)
Input(fno, sd(n0).q4)
Input(fno, sd(n0).a1)
Input(fno, sd(n0).a2)
Input(fno, sd(n0).a3)
Input(fno, sd(n0).a4)
Input(fno, sd(n0).d1)
Input(fno, sd(n0).d2)
Debug.WriteLine(sd(n0).id & vbTab & sd(n0).q1 & vbTab &
sd(n0).q2 & vbTab & sd(n0).q3 & vbTab & sd(n0).q4 & vbTab &
sd(n0).a1 & vbTab & sd(n0).a2 & vbTab & sd(n0).a3 & vbTab &
sd(n0).a4 & vbTab & sd(n0).d1 & vbTab & sd(n0).d2)
n0 += 1
Loop
FileClose(fno) 'ファイルを閉じる
For i As Integer = 0 To n0 - 1
If sd(i).d1 = "済" Then
ss = ss + 1
End If
If sd(i).d1 = "未了" Then
m = m + 1
End If
Next
Label_p4 = New Point(Label4.Left, Label4.Top) '初期位置を保管
Label_p5 = New Point(Label5.Left, Label5.Top) '初期位置を保管
Label_p6 = New Point(Label6.Left, Label6.Top) '初期位置を保管
Label_p7 = New Point(Label7.Left, Label7.Top) '初期位置を保管
Label_p8 = New Point(Label8.Left, Label8.Top) '初期位置を保管
Label_p9 = New Point(Label9.Left, Label9.Top) '初期位置を保管

End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Label4.Text = ""
Label5.Text = ""
Label6.Text = ""
Label7.Text = ""
Label4.Location = New Point(Label_p4.X, Label_p4.Y)
Label5.Location = New Point(Label_p5.X, Label_p5.Y)
Label6.Location = New Point(Label_p6.X, Label_p6.Y)
Label7.Location = New Point(Label_p7.X, Label_p7.Y)
Label8.Location = New Point(Label_p8.X, Label_p8.Y)
Label9.Location = New Point(Label_p9.X, Label_p9.Y)
Label1.BackColor = Color.Pink
Label2.BackColor = Color.Pink
Label3.BackColor = Color.Pink
Label4.BackColor = Color.PowderBlue


Randomize()
x1 = Int(Rnd() * n0)
Label10.Text = sd(x1).q1
Label11.Text = sd(x1).q2
Label12.Text = sd(x1).q3
Label13.Text = sd(x1).q4

k = Int(Rnd() * 24) + 1
If k = 1 Then
Label4.Text = sd(x1).a1
Label5.Text = sd(x1).a2
Label6.Text = sd(x1).a3
Label7.Text = sd(x1).a4
ElseIf k = 2 Then
Label4.Text = sd(x1).a1
Label5.Text = sd(x1).a2
Label7.Text = sd(x1).a3
Label6.Text = sd(x1).a4
ElseIf k = 3 Then
Label4.Text = sd(x1).a1
Label6.Text = sd(x1).a2
Label5.Text = sd(x1).a3
Label7.Text = sd(x1).a4
ElseIf k = 4 Then
Label4.Text = sd(x1).a1
Label6.Text = sd(x1).a2
Label7.Text = sd(x1).a3
Label5.Text = sd(x1).a4
ElseIf k = 5 Then
Label4.Text = sd(x1).a1
Label7.Text = sd(x1).a2
Label5.Text = sd(x1).a3
Label6.Text = sd(x1).a4
ElseIf k = 6 Then
Label4.Text = sd(x1).a1
Label7.Text = sd(x1).a2
Label6.Text = sd(x1).a3
Label5.Text = sd(x1).a4
ElseIf k = 7 Then
Label5.Text = sd(x1).a1
Label4.Text = sd(x1).a2
Label6.Text = sd(x1).a3
Label7.Text = sd(x1).a4
ElseIf k = 8 Then
Label5.Text = sd(x1).a1
Label4.Text = sd(x1).a2
Label7.Text = sd(x1).a3
Label6.Text = sd(x1).a4
ElseIf k = 9 Then
Label5.Text = sd(x1).a1
Label6.Text = sd(x1).a2
Label4.Text = sd(x1).a3
Label7.Text = sd(x1).a4
ElseIf k = 10 Then
Label5.Text = sd(x1).a1
Label6.Text = sd(x1).a2
Label7.Text = sd(x1).a3
Label4.Text = sd(x1).a4
ElseIf k = 11 Then
Label5.Text = sd(x1).a1
Label7.Text = sd(x1).a2
Label4.Text = sd(x1).a3
Label6.Text = sd(x1).a4
ElseIf k = 12 Then
Label5.Text = sd(x1).a1
Label7.Text = sd(x1).a2
Label6.Text = sd(x1).a3
Label4.Text = sd(x1).a4
ElseIf k = 13 Then
Label6.Text = sd(x1).a1
Label4.Text = sd(x1).a2
Label5.Text = sd(x1).a3
Label7.Text = sd(x1).a4
ElseIf k = 14 Then
Label6.Text = sd(x1).a1
Label4.Text = sd(x1).a2
Label7.Text = sd(x1).a3
Label5.Text = sd(x1).a4
ElseIf k = 15 Then
Label6.Text = sd(x1).a1
Label5.Text = sd(x1).a2
Label4.Text = sd(x1).a3
Label7.Text = sd(x1).a4
ElseIf k = 16 Then
Label6.Text = sd(x1).a1
Label5.Text = sd(x1).a2
Label7.Text = sd(x1).a3
Label4.Text = sd(x1).a4
ElseIf k = 17 Then
Label6.Text = sd(x1).a1
Label7.Text = sd(x1).a2
Label4.Text = sd(x1).a3
Label5.Text = sd(x1).a4
ElseIf k = 18 Then
Label6.Text = sd(x1).a1
Label7.Text = sd(x1).a2
Label5.Text = sd(x1).a3
Label4.Text = sd(x1).a4
ElseIf k = 19 Then
Label7.Text = sd(x1).a1
Label4.Text = sd(x1).a2
Label5.Text = sd(x1).a3
Label6.Text = sd(x1).a4
ElseIf k = 20 Then
Label7.Text = sd(x1).a1
Label4.Text = sd(x1).a2
Label6.Text = sd(x1).a3
Label5.Text = sd(x1).a4
ElseIf k = 21 Then
Label7.Text = sd(x1).a1
Label5.Text = sd(x1).a2
Label4.Text = sd(x1).a3
Label6.Text = sd(x1).a4
ElseIf k = 22 Then
Label7.Text = sd(x1).a1
Label5.Text = sd(x1).a2
Label6.Text = sd(x1).a3
Label4.Text = sd(x1).a4
ElseIf k = 23 Then
Label7.Text = sd(x1).a1
Label6.Text = sd(x1).a2
Label4.Text = sd(x1).a3
Label5.Text = sd(x1).a4
ElseIf k = 24 Then
Label7.Text = sd(x1).a1
Label6.Text = sd(x1).a2
Label5.Text = sd(x1).a3
Label4.Text = sd(x1).a4
End If


引用返信 編集キー/
■77892 / inTopicNo.2)  Re[1]: 空欄補充されたラベルの正誤を判断するには?
□投稿者/ ごうき (13回)-(2015/11/27(Fri) 23:22:07)
'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) '初期位置を保管
Label_p7 = New Point(Label7.Left, Label7.Top) '初期位置を保管
Label_p8 = New Point(Label8.Left, Label8.Top) '初期位置を保管
Label_p9 = New Point(Label9.Left, Label9.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
Me.Controls.Add(Label7)
AddHandler Label7.MouseDown, AddressOf label7_MouseDown
AddHandler Label7.MouseMove, AddressOf label7_MouseMove
AddHandler Label7.MouseUp, AddressOf label7_MouseUp
Me.Controls.Add(Label8)
AddHandler Label8.MouseDown, AddressOf label8_MouseDown
AddHandler Label8.MouseMove, AddressOf label8_MouseMove
AddHandler Label8.MouseUp, AddressOf label8_MouseUp
Me.Controls.Add(Label9)
AddHandler Label9.MouseDown, AddressOf label9_MouseDown
AddHandler Label9.MouseMove, AddressOf label9_MouseMove
AddHandler Label9.MouseUp, AddressOf label9_MouseUp
Button1.Enabled = False

End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If Not (rect1.Contains(New Point(Label4.Left, Label4.Top)) Or rect1.Contains(New Point(Label5.Left, Label5.Top)) _
Or rect1.Contains(New Point(Label6.Left, Label6.Top)) Or rect1.Contains(New Point(Label7.Left, Label7.Top))) Then
b0 = 1
Label1.BackColor = Color.Yellow
Else
b0 = 0
End If
If Not (rect2.Contains(New Point(Label4.Left, Label4.Top)) Or rect2.Contains(New Point(Label5.Left, Label5.Top)) _
Or rect2.Contains(New Point(Label6.Left, Label6.Top)) Or rect2.Contains(New Point(Label7.Left, Label7.Top))) Then
b1 = 1
Label2.BackColor = Color.Yellow
Else
b1 = 0
End If
If Not (rect3.Contains(New Point(Label4.Left, Label4.Top)) Or rect3.Contains(New Point(Label5.Left, Label5.Top)) _
Or rect3.Contains(New Point(Label6.Left, Label6.Top)) Or rect3.Contains(New Point(Label7.Left, Label7.Top))) Then
b2 = 1
Label3.BackColor = Color.Yellow
Else
b2 = 0
End If
b = b0 + b1 + b2
If b > 0 Then
MsgBox("全てに解答しましょう。")
ElseIf b = 0 Then


"ここをどう書けばいいのでしょうか・・・・・・"




End If
Button1.Enabled = True
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
If Label4.Location = Label5.Location Or Label4.Location = Label6.Location Or Label4.Location = Label7.Location Or Label4.Location = Label8.Location Or Label4.Location = Label9.Location 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
If Label5.Location = Label4.Location Or Label5.Location = Label6.Location Or Label5.Location = Label7.Location Or Label5.Location = Label8.Location Or Label5.Location = Label9.Location 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


引用返信 編集キー/
■77893 / inTopicNo.3)  Re[2]: 空欄補充されたラベルの正誤を判断するには?
□投稿者/ ごうき (14回)-(2015/11/27(Fri) 23:23:10)
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
If Label6.Location = Label4.Location Or Label6.Location = Label5.Location Or Label6.Location = Label7.Location Or Label6.Location = Label8.Location Or Label6.Location = Label9.Location 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

引用返信 編集キー/
■77894 / inTopicNo.4)  Re[3]: 空欄補充されたラベルの正誤を判断するには?
□投稿者/ ごうき (15回)-(2015/11/27(Fri) 23:23:22)
Private Sub label7_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If e.Button = MouseButtons.Left Then '左クリック
mp = Me.PointToClient(Cursor.Position) 'マウスを押した位置の座標を取得
sender_Label7 = DirectCast(sender, Label) '移動させるlabel
sender_Label7.BringToFront() '最前面へ移動
sender_Label7.BackColor = Color.Pink
sender_Label7.Cursor = Cursors.Hand 'ポインターを変更したければ
End If
End Sub

Public Sub label7_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_Label7.Location = New Point(rect1.X, rect1.Y)
ElseIf rect2.Contains(New Point(new_mp.X, new_mp.Y)) Then
'マウスが目的のLabelの領域内に入ったら吸着させる
sender_Label7.Location = New Point(rect2.X, rect2.Y)
ElseIf rect3.Contains(New Point(new_mp.X, new_mp.Y)) Then
'マウスが目的のLabelの領域内に入ったら吸着させる
sender_Label7.Location = New Point(rect3.X, rect3.Y)
Else
'マウスが目的のLabelの領域外であれば自由に移動
sender_Label7.Left += (new_mp.X - mp.X)
sender_Label7.Top += (new_mp.Y - mp.Y)
End If
mp = new_mp 'マウス座標を更新
End If
End Sub

Private Sub label7_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_Label7.Location = New Point(Label_p7.X, Label_p7.Y)
End If
If Label7.Location = Label4.Location Or Label7.Location = Label5.Location Or Label7.Location = Label6.Location Or Label7.Location = Label8.Location Or Label7.Location = Label9.Location Then
sender_Label7.Location = New Point(Label_p7.X, Label_p7.Y)
End If
sender_Label7.BackColor = Color.PowderBlue
sender_Label7.Cursor = Cursors.Default 'ポインターを変更したければ
End If
End Sub

Private Sub label8_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If e.Button = MouseButtons.Left Then '左クリック
mp = Me.PointToClient(Cursor.Position) 'マウスを押した位置の座標を取得
sender_Label8 = DirectCast(sender, Label) '移動させるlabel
sender_Label8.BringToFront() '最前面へ移動
sender_Label8.BackColor = Color.Pink
sender_Label8.Cursor = Cursors.Hand 'ポインターを変更したければ
End If
End Sub

Public Sub label8_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_Label8.Location = New Point(rect1.X, rect1.Y)
ElseIf rect2.Contains(New Point(new_mp.X, new_mp.Y)) Then
'マウスが目的のLabelの領域内に入ったら吸着させる
sender_Label8.Location = New Point(rect2.X, rect2.Y)
ElseIf rect3.Contains(New Point(new_mp.X, new_mp.Y)) Then
'マウスが目的のLabelの領域内に入ったら吸着させる
sender_Label8.Location = New Point(rect3.X, rect3.Y)
Else
'マウスが目的のLabelの領域外であれば自由に移動
sender_Label8.Left += (new_mp.X - mp.X)
sender_Label8.Top += (new_mp.Y - mp.Y)
End If
mp = new_mp 'マウス座標を更新
End If
End Sub

Private Sub label8_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_Label8.Location = New Point(Label_p8.X, Label_p8.Y)
End If
If Label8.Location = Label4.Location Or Label8.Location = Label5.Location Or Label8.Location = Label6.Location Or Label8.Location = Label7.Location Or Label8.Location = Label9.Location Then
sender_Label8.Location = New Point(Label_p8.X, Label_p8.Y)
End If
sender_Label8.BackColor = Color.PowderBlue
sender_Label8.Cursor = Cursors.Default 'ポインターを変更したければ
End If
End Sub

Private Sub label9_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If e.Button = MouseButtons.Left Then '左クリック
mp = Me.PointToClient(Cursor.Position) 'マウスを押した位置の座標を取得
sender_Label9 = DirectCast(sender, Label) '移動させるlabel
sender_Label9.BringToFront() '最前面へ移動
sender_Label9.BackColor = Color.Pink
sender_Label9.Cursor = Cursors.Hand 'ポインターを変更したければ
End If
End Sub

Public Sub label9_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_Label9.Location = New Point(rect1.X, rect1.Y)
ElseIf rect2.Contains(New Point(new_mp.X, new_mp.Y)) Then
'マウスが目的のLabelの領域内に入ったら吸着させる
sender_Label9.Location = New Point(rect2.X, rect2.Y)
ElseIf rect3.Contains(New Point(new_mp.X, new_mp.Y)) Then
'マウスが目的のLabelの領域内に入ったら吸着させる
sender_Label9.Location = New Point(rect3.X, rect3.Y)
Else
'マウスが目的のLabelの領域外であれば自由に移動
sender_Label9.Left += (new_mp.X - mp.X)
sender_Label9.Top += (new_mp.Y - mp.Y)
End If
mp = new_mp 'マウス座標を更新
End If
End Sub

Private Sub label9_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_Label9.Location = New Point(Label_p9.X, Label_p9.Y)
End If
If Label9.Location = Label4.Location Or Label9.Location = Label5.Location Or Label9.Location = Label6.Location Or Label9.Location = Label7.Location Or Label9.Location = Label8.Location Then
sender_Label9.Location = New Point(Label_p9.X, Label_p9.Y)
End If
sender_Label9.BackColor = Color.PowderBlue
sender_Label9.Cursor = Cursors.Default 'ポインターを変更したければ
End If
End Sub
End Class
引用返信 編集キー/


トピック内ページ移動 / << 0 >>

このトピックに書きこむ

過去ログには書き込み不可

管理者用

- Child Tree -