Picturebox class for Trigers.
Picturebox class for Trigers.
Picturebox class for Trigers.
Picturebox class for Trigers.
Picturebox class for Trigers.
Picturebox class for Trigers. Picturebox class for Trigers. Picturebox class for Trigers. Picturebox class for Trigers. Picturebox class for Trigers. Picturebox class for Trigers. Picturebox class for Trigers. Picturebox class for Trigers.
Picturebox class for Trigers. Picturebox class for Trigers.
Picturebox class for Trigers.
Go Back  Xtreme Visual Basic Talk > > > Picturebox class for Trigers.


Reply
 
Thread Tools Display Modes
  #1  
Old 04-08-2015, 11:52 PM
zacacp zacacp is offline
Newcomer
 
Join Date: Jan 2015
Posts: 6
Default Picturebox class for Trigers.


I dont know how to write an event from form1 into the class, i try with the "withevents" but doesnt work.

the code is simple and explain it better.

Code:
Public Class TRIGER
    Inherits PictureBox

    Public WithEvents CHARACTER As PictureBox = form1.CHARACTER

    Private Sub form1_CHARACTER_locationchanged(sender As Object, e As EventArgs) Handles CHARACTER.LocationChanged

        If form1.CHARACTER.Bounds.IntersectsWith(Me.Bounds) Then

         -SOMETHING

        End If
    End Sub
Reply With Quote
  #2  
Old 04-09-2015, 03:28 AM
PlausiblyDamp's Avatar
PlausiblyDampPicturebox class for Trigers. PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,056
Default

Not entirely sure what you are trying to do with the above code....

Are you creating your own picturebox that you want to be able to raise events that the form can handle or are you wanting your picturebox to handle events raised by the form?
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #3  
Old 04-09-2015, 05:51 AM
passel's Avatar
passelPicturebox class for Trigers. passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,015
Default

I'm think I know, although I wouldn't have thought to do collision testing this way.
I think rather than reference a control by name on the form, you should have either a method that can be called passing the control whose event you want to monitor, or pass the control to the constructor.
Here is a test case passing the picturebox to the constructor.
Code:
Public Class Form1
  Private myTrigger As TRIGER

  Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    myTrigger = New TRIGER(PictureBox1)
    Controls.Add(myTrigger)
    myTrigger.SetBounds(100, 100, 50, 50)
    myTrigger.BackColor = Color.Red

    PictureBox1.BackColor = Color.Green
  End Sub

  Private Sub PictureBox1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
    'Allow dragging the picturebox around
    Static lpos As Point
    If e.Button = Windows.Forms.MouseButtons.Left Then
      PictureBox1.Location += New Size(e.Location - New Size(lpos))
    Else
      lpos = e.Location
    End If
  End Sub
End Class

Public Class TRIGER
  Inherits PictureBox
  Private WithEvents CHARACTER As PictureBox

  Public Sub New(p As PictureBox)
    CHARACTER = p
  End Sub

  Private Sub form1_CHARACTER_locationchanged(sender As Object, e As EventArgs) Handles CHARACTER.LocationChanged
    If CHARACTER.Bounds.IntersectsWith(Me.Bounds) Then
      BackColor = Color.Yellow
    Else
      BackColor = Color.Red
    End If
  End Sub
End Class
I'm still not sure of the efficiency of processing events this way, but since using a dictionary to hold multiple pictureboxes for collision testing against the trigger occurred to me when I first looked at your code, I went ahead and put together a test case showing that as well.
If I had multiple characters and multiple trigger points, all the multiple event processing would probably not be the way to go, but I couldn't say that for sure without testing. It is just an intuitive feeling, which isn't always correct in this age of event driven, object oriented processing.

In any case, here is my first idea of changing the code above to handle multiple characters against a single trigger object.
The example expects that three pictureboxes exist. Since the code already changed the background color of picturebox1, I set the background color of Picturebox3 to another color, and left picuturebox2 with its default background color.

This example reverses whose color gets changed. Instead of the trigger point changing color during a collision, the colliding pictureboxes color gets changed. This shows that each picturebox is being recognized independently from the others, and that all three pictureboxes location changed events are being processed by the TRIGER class.
Code:
Public Class Form1
  Private myTrigger As New TRIGER

  Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Controls.Add(myTrigger)
    myTrigger.SetBounds(100, 100, 50, 50)
    myTrigger.BackColor = Color.Red

    PictureBox1.BackColor = Color.Green

   'Add three pictureboxes that are monitored by the trigger and what color
   ' they will be set to when a collision occurs 
    myTrigger.add(PictureBox1, Color.Yellow)
    myTrigger.add(PictureBox2, Color.Blue)
    myTrigger.add(PictureBox3, Color.Magenta)

  End Sub

 'Modified to handle dragging all three pictureboxes around
  Private Sub PictureBox_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles _
    PictureBox1.MouseMove, PictureBox2.MouseMove, PictureBox3.MouseMove
    'Allow dragging the picturebox around
    Dim pb As PictureBox = DirectCast(sender, PictureBox)
    Static lpos As Point
    If e.Button = Windows.Forms.MouseButtons.Left Then
      pb.Location += New Size(e.Location - New Size(lpos))
    Else
      lpos = e.Location
    End If
  End Sub
End Class

Public Class TRIGER
  Inherits PictureBox
  ' Private WithEvents CHARACTER As PictureBox
  Private Class objColors
    Public defaultColor As Color
    Public collisionColor As Color
  End Class

  Private Characters As New Dictionary(Of PictureBox, objColors)

  'Public Sub New(p As PictureBox)
  '  CHARACTER = p
  'End Sub
  Public Sub add(p As PictureBox, c As Color)
    If Characters.ContainsKey(p) Then
      Characters(p).collisionColor = c       'allow changing collision color
    Else
      Dim objColor As New objColors
      objColor.defaultColor = p.BackColor
      objColor.collisionColor = c
      Characters.Add(p, objColor)
      AddHandler p.LocationChanged, AddressOf Me.CHARACTER_locationchanged
    End If
  End Sub

  Private Sub CHARACTER_locationchanged(sender As Object, e As EventArgs)
    Dim p As PictureBox = DirectCast(sender, PictureBox)

    If p.Bounds.IntersectsWith(Me.Bounds) Then
      p.BackColor = Characters(p).collisionColor
    Else
      p.BackColor = Characters(p).defaultColor
    End If
  End Sub
End Class
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.

Last edited by passel; 04-09-2015 at 07:07 AM.
Reply With Quote
  #4  
Old 04-09-2015, 07:37 PM
zacacp zacacp is offline
Newcomer
 
Join Date: Jan 2015
Posts: 6
Default explaining

You can code "triggers" in one or two levels, but when you are dealing with so many forms(levels) that work turns to nightmare.
example=FORM1
I usually handle with all trigers in this form. Those "triggers" are pictureboxes, so i say that when the main character changes its location and intersects those "triggers" something will happen.
This is easy but you have to consider naming all the pictureboxes(triggers) in orden not to get confused while coding them. Finally you are happy because everything works Fine! but...then...you continue to the second form(level2) and do that same job over and over again.

i think in making a new picturebox class(template trigger) to solve this problem(stress) so i could add this controls and only code Once.

the picture ilustrates the concept i am trying to express in my limited English.
Attached Images
File Type: jpg passel.jpg (135.7 KB, 6 views)
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Picturebox class for Trigers.
Picturebox class for Trigers.
Picturebox class for Trigers. Picturebox class for Trigers.
Picturebox class for Trigers.
Picturebox class for Trigers.
Picturebox class for Trigers. Picturebox class for Trigers. Picturebox class for Trigers. Picturebox class for Trigers. Picturebox class for Trigers. Picturebox class for Trigers. Picturebox class for Trigers.
Picturebox class for Trigers.
Picturebox class for Trigers.
 
Picturebox class for Trigers.
Picturebox class for Trigers.
 
-->