منتدى فيجوال بيسك لكل العرب | منتدى المبرمجين العرب

نسخة كاملة : تحريك
أنت حالياً تتصفح نسخة خفيفة من المنتدى . مشاهدة نسخة كاملة مع جميع الأشكال الجمالية .
السلام عليكم
كيف ممكن اني اعمل شبكة على الفورم (مربعات صغيرة)
ووضع بوتن او بيكتشر وليكن اخذت 16 مربع
اريد كود تحريكها بحيث تثبت على 16 مربع من مربعات الشبكة
غير 16 مربع اللي كانت عليهن
هل تقصد مربعات بهذا الشكل؟


كود :
Const HW As Integer = 40
   Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
       For i As Integer = 0 To Me.Height Step HW
           e.Graphics.DrawLine(Pens.Black, 0, i, Me.Width, i)
       Next
       For j As Integer = 0 To Me.Width Step HW
           e.Graphics.DrawLine(Pens.Black, j, 0, j, Me.Height)
       Next
   End Sub


ووضع ازرار داخل كل مربع؟
ان الفورم تكون شبكة مربعات مثل الصورة
واللون الاحمر هو البوتن او البكتشر
اريد ان المستخدم يستطيع تحريكه الى اللون الازرق او اي مكان يريده داخل الشبكة
بس حصرا على المربعات

اي اخي هاد هو الكود بس كيف ممكن اني احرك بوتن حصرا على المربعات
تفضل اخي

فقط ضع زر بوتون في الفورم وجرب هذا الكود وان شاء الله يكون هو طلبك



كود :
Const HW As Integer = 40
   Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
       For i As Integer = 0 To Me.Height Step HW
           e.Graphics.DrawLine(Pens.Black, 0, i, Me.Width, i)
       Next
       For j As Integer = 0 To Me.Width Step HW
           e.Graphics.DrawLine(Pens.Black, j, 0, j, Me.Height)
       Next
   End Sub

   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       Button1.Size = New Size(80, 80)
       Button1.Location = New Point(80, 80)
       Button1.FlatStyle = FlatStyle.Flat
   End Sub

   Dim b As Boolean = False
   Dim x, y As Integer
   Private Sub Button1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown
       b = True
       x = e.X : y = e.Y
   End Sub

   Private Sub Button1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseMove
       If b Then
           Button1.Location = New Point(Button1.Location.X + e.X - x, Button1.Location.Y + e.Y - y)
       End If
   End Sub

   Private Sub Button1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseUp
       b = False
       Dim x1 As Integer = Button1.Location.X / 40
       Dim y1 As Integer = Button1.Location.Y / 40
       Button1.Location = New Point(x1 * 40, y1 * 40)
   End Sub
مشكور اخي بس ممكن اني اخفي المربعات وعند الضغط على البوتن تظهر المربعات وعند الافلات ينتقل البوتن وتختفي المربعات
كود :
Const HW As Integer = 40
   Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
       If b Then
           For i As Integer = 0 To Me.Height Step HW
               e.Graphics.DrawLine(Pens.Black, 0, i, Me.Width, i)
           Next
           For j As Integer = 0 To Me.Width Step HW
               e.Graphics.DrawLine(Pens.Black, j, 0, j, Me.Height)
           Next
       End If

   End Sub

   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       Button1.Size = New Size(80, 80)
       Button1.Location = New Point(80, 80)
       Button1.FlatStyle = FlatStyle.Flat
   End Sub

   Dim b As Boolean = False
   Dim x, y As Integer
   Private Sub Button1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown
       b = True
       x = e.X : y = e.Y
       Invalidate()
   End Sub

   Private Sub Button1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseMove
       If b Then
           Button1.Location = New Point(Button1.Location.X + e.X - x, Button1.Location.Y + e.Y - y)
       End If
   End Sub

   Private Sub Button1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseUp
       b = False
       Dim x1 As Integer = Button1.Location.X / 40
       Dim y1 As Integer = Button1.Location.Y / 40
       Button1.Location = New Point(x1 * 40, y1 * 40)
       Invalidate()
   End Sub
مشكور اخي ممكن اني البوتن ما يطلع خارج حدود الفورم ولو اني اتعبتك معي
تفضل اخي

لا اخي ماكو أي تعب تدلل
كود :
Const HW As Integer = 40
   Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
       If b Then
           For i As Integer = 0 To Me.Height Step HW
               e.Graphics.DrawLine(Pens.Black, 0, i, Me.Width, i)
           Next
           For j As Integer = 0 To Me.Width Step HW
               e.Graphics.DrawLine(Pens.Black, j, 0, j, Me.Height)
           Next
       End If

   End Sub

   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       Button1.Size = New Size(80, 80)
       Button1.Location = New Point(80, 80)
       Button1.FlatStyle = FlatStyle.Flat
   End Sub

   Dim b As Boolean = False
   Dim x, y As Integer
   Private Sub Button1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown
       b = True
       x = e.X : y = e.Y
       Invalidate()
   End Sub

   Private Sub Button1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseMove
       If b Then
           Button1.Location = New Point(Button1.Location.X + e.X - x, Button1.Location.Y + e.Y - y)
       End If
   End Sub

   Private Sub Button1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseUp
       b = False
       Dim x1 As Integer = Button1.Location.X / 40
       Dim y1 As Integer = Button1.Location.Y / 40
       If x1 * 40 > Me.Width - Button1.Width Then
           x1 = (Me.Width - Button1.Width) / 40
       End If
       If x1 < 0 Then
           x1 = 0
       End If
       If y1 * 40 > Me.Height - Button1.Height Then
           y1 = (Me.Height - Button1.Height) / 40
       End If
       If y1 < 0 Then
           y1 = 0
       End If
       Button1.Location = New Point(x1 * 40, y1 * 40)
       Invalidate()
   End Sub