กลับไปรายการกระดานข่าว ตอบกลับ โพสใหม่

[VB6] เรื่องของ Button (มีปัญหา สงสัย?)

NOTE: หัวข้อถูกปิด เจ้าของถูกแบนหรือถูกลบ
ผมจะไปให้ถึงฝัน...
NOTE: หัวข้อถูกปิด เจ้าของถูกแบนหรือถูกลบ
ผมจะไปให้ถึงฝัน...
คำถามนอกโลก

You need to upgrade your Adobe Flash Player to watch this video.
Download it from Adobe.
ถามต่อเลยได้ไหมครับ...ผมก็มีปัญหาเรื่อง Control Button เหมือนกัน...
คือ ผมจะลองทำโปรเเกรมดูแผนที่แบบง่ายๆอะนะครับ (เหมือนกับ Google earth) แล้วเราสามารถปักจุดพิกัดได้อะไรทำนองนี้ครับ
ผมใส่ Button ลองไปในฟอร์ม 2 ตัวครับ (ในฟอร์มก็จะมี Pic..Box อันนึงนะครับ)
Button ตัวแรกเมื่อคลิกแล้ว ถ้าเอา Cursor ไปคลิก(ซ้าย)ตรงไหนใน Pic..Box จะมีไอคอน ตัวหมุดออกมา
Button ตัวที่สองเหมือนกับตัวเเรกครับ แต่ไอคอนที่เเสดงจะเป็นรูปบ้านนะครับ

ปัญหาคือ เมื่อผมคลิก ที่ Button ตัวแรกไปปักหมุดแล้ว พอไปคลิกที่ button ตัวที่สอง มันไม่คืนค่าจากหมุดตัวเเรกครับครับ มันยังคงเป็นรูปตัวหมุดเหมือนเดิม (Code ครับ)

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown

        PictureBox1.Cursor = Cursors.Hand
        gr = PictureBox1.CreateGraphics
        End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button2.MouseDown
        PictureBox1.Cursor = Cursors.Cross
        gr = PictureBox1.CreateGraphics
        End Sub
   
    Private Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        Try
            If Button1.Enabled = True Then
                gr.DrawIcon(ตัวหมุด, e.X - 3, e.Y - 32)
            ElseIf Button2.Enabled = True Then
                gr.DrawIcon(บ้าน, e.X, e.Y)
            End If
           Catch ex As Exception
        End Try
    End Sub

ประมาณนี้ครับ.....

หลังจากนั้นผมเลยเเก้ปัญหาอีกแบบนึงคือ ใช้ คลิกซ้ายแสดงตัวหมุด คลิกขวาเเสดงบ้าน  (Code ครับ)
Private Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        Try
                If e.Button = Windows.Forms.MouseButtons.Left Then
                gr.DrawIcon(ตัวหมุด, e.X - 3, e.Y - 32)
            ElseIf e.Button = Windows.Forms.MouseButtons.Right Then
                gr.DrawIcon(บ้าน, e.X - 3, e.Y - 32)
            End If
        Catch ex As Exception
        End Try
    End Sub

ซึ่งดูไปแล้วเหมือนจะเข้าท่า แต่ไม่เหมาะสมซักเท่าไหร่ ผมเลยเเก้ปัญหาโดยใช้ ToolStrip ออกมาดูดีมากครับ เมื่อเราคลิกที่ปุ่มใดปุ่มนึงมันจะคืนค่าได้เลย

Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ToolStripButton1.MouseDown
        ToolStripButton1.Checked = Not ToolStripButton1.Checked
        PictureBox1.Cursor = Cursors.Hand
        gr = PictureBox1.CreateGraphics
    End Sub

    Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ToolStripButton2.MouseDown
        ToolStripButton2.Checked = Not ToolStripButton2.Checked
        PictureBox1.Cursor = Cursors.Cross
        gr = PictureBox1.CreateGraphics
    End Sub

     แต่อย่างที่เล่ามาว่า ถ้าหากว่าจะใช้ Control Button จะมีวิธีใดไหมครับที่จะให้ความสามาารถได้เท่ากับ ToolStrip
อันนี้น่าจะผิดครับ ค่าจะเป็นจริงเสมอจนกว่าจะสั่งให้เป็นเท็จดังนั้น เลยได้แต่หมุด
  1.             If Button1.Enabled = True Then
  2.                 gr.DrawIcon(ตัวหมุด, e.X - 3, e.Y - 32)
  3.             ElseIf Button2.Enabled = True Then
  4.                 gr.DrawIcon(บ้าน, e.X, e.Y)
  5.             End If
คัดลอกโค๊ด
ผมว่าวิธีที่ดีที่สุดใช้ ContextMenuStrip ครับ ประมาณว่า คลิ๊กขวาบนแผนที่เมื่อไหร่
ให้แสดง ContextMenuStrip และตรวจสอบว่าเคยสร้างหมุดหรือยัง ถ้ายังใช้เมนู ปักหมุด ถ้าเคย ใช้เมนู ลบหมุด
ตรวจสอบต่อไปอีกว่าเคยสร้างบ้านหรือยัง ถ้ายัง ใช้เมนู สร้างบ้าน ถ้าเคย ใช้เมนู ลบบ้าน
.....
อะไรที่สร้างไปแล้ว ลากได้ทันที กดบันทึกเมื่อไหร่ถึงเริ่มเก็บค่า
หาวิธีใช้ ContextMenuStrip เองก่อนนะ
วิธีใช้ เบื้องต้น ให้ลองสร้างฟรอม1 เปล่าๆ แล้วใส่โค็ดนี้ ใส่แล้วไปลบ code ที่ Designerที่มันerror ออกซะ

.................
Public Class Form1
#Region "form1"
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container()
Me.ContextMenuStrip1 = New System.Windows.Forms.ContextMenuStrip(Me.components)
Me.ToolStripMenuItem1 = New System.Windows.Forms.ToolStripMenuItem()
Me.ToolStripMenuItem2 = New System.Windows.Forms.ToolStripMenuItem()
Me.ToolStripMenuItem3 = New System.Windows.Forms.ToolStripMenuItem()
Me.ToolStripMenuItem4 = New System.Windows.Forms.ToolStripMenuItem()
Me.ContextMenuStrip1.SuspendLayout()
Me.SuspendLayout()
'
'ContextMenuStrip1
'
Me.ContextMenuStrip1.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.ToolStripMenuItem1, Me.ToolStripMenuItem2, Me.ToolStripMenuItem3, Me.ToolStripMenuItem4})
Me.ContextMenuStrip1.Name = "ContextMenuStrip1"
Me.ContextMenuStrip1.Size = New System.Drawing.Size(153, 114)
'
'ToolStripMenuItem1
'
Me.ToolStripMenuItem1.Name = "ToolStripMenuItem1"
Me.ToolStripMenuItem1.Size = New System.Drawing.Size(152, 22)
Me.ToolStripMenuItem1.Text = "home"
'
'ToolStripMenuItem2
'
Me.ToolStripMenuItem2.Name = "ToolStripMenuItem2"
Me.ToolStripMenuItem2.Size = New System.Drawing.Size(152, 22)
Me.ToolStripMenuItem2.Text = "Stick"
'
'ToolStripMenuItem3
'
Me.ToolStripMenuItem3.Name = "ToolStripMenuItem3"
Me.ToolStripMenuItem3.Size = New System.Drawing.Size(152, 22)
Me.ToolStripMenuItem3.Text = "del home"
'
'ToolStripMenuItem4
'
Me.ToolStripMenuItem4.Name = "ToolStripMenuItem4"
Me.ToolStripMenuItem4.Size = New System.Drawing.Size(152, 22)
Me.ToolStripMenuItem4.Text = "del Stick"
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(284, 262)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ContextMenuStrip1.ResumeLayout(False)
Me.ResumeLayout(False)
End Sub
Friend WithEvents ContextMenuStrip1 As System.Windows.Forms.ContextMenuStrip
Friend WithEvents ToolStripMenuItem1 As System.Windows.Forms.ToolStripMenuItem
Friend WithEvents ToolStripMenuItem2 As System.Windows.Forms.ToolStripMenuItem
Friend WithEvents ToolStripMenuItem3 As System.Windows.Forms.ToolStripMenuItem
Friend WithEvents ToolStripMenuItem4 As System.Windows.Forms.ToolStripMenuItem
#End Region
 
Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
If e.Button = Windows.Forms.MouseButtons.Right Then
ContextMenuStrip1.Show(Me, e.Location)
End If
End Sub
Private Sub ToolStripMenuItem1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ToolStripMenuItem1.MouseUp
MsgBox(
"home")
End Sub
Private Sub ToolStripMenuItem2_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ToolStripMenuItem2.MouseUp
MsgBox(
"Stick")
End Sub
Private Sub ToolStripMenuItem3_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ToolStripMenuItem3.MouseUp
MsgBox(
"del home")
End Sub
Private Sub ToolStripMenuItem4_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ToolStripMenuItem4.MouseUp
MsgBox(
"del Stick")
End Sub
End
Class


You need to upgrade your Adobe Flash Player to watch this video.
Download it from Adobe.
กลับไปรายการกระดานข่าว