Maze Game In Visual Basic. Cursor Collision With Pictureboxes - collision

I am making a maze game in Visual Basic. How do I make a collision with the cursor and the picturebox, is it something like this?
if curosr.position.intersectswith(picturebox1) then
msgbox("HI")
end if
Then I have 1,000 pictureboxes I have to do that for, so is there an easier way to that? Maybe like this?
for each picturebox in me.controls
if curosr.position.intersectswith(picturebox) then
msgbox("HI")
end if

Not a VB guy but define an event handler
eg.
Private Sub CollisionHandler(ByVal sender As Object, ByVal e As System.EventArgs)
'your code here
End Sub
Then in some thing like your each loop link the picture box to the above handler with
AddHandler picturebox.MouseOver, AddressOf CollisionHandler

Related

How to delay the LostFocus Event in VB6

I'm having an issue with a process that involves the LostFocus event.
When the cursor loses focus from a particular textbox, I'm simply putting the focus back into that box.
My issue is removing focus long enough for the user to click a log out button. Is there a way to intercept the LostFocus event long enough to allow the user to click the log out button?
Obviously I don't know the big picture here. But keeping only with what you said, the following does the trick. Effectively the event is delayed briefly, allowing the button to be clicked:
Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Sub Text1_LostFocus()
Sleep 100
DoEvents
Text1.SetFocus
End Sub
With a combination of a Timer and another control that is outside of the borders of your form, you can achieve this.
Private Sub Text1_LostFocus()
Combo1.SetFocus
ReturnFocusTimer.Enabled = True
End Sub
Private Sub ReturnFocusTimer_Timer()
ReturnFocusTimer.Enabled = False
Text1.SetFocus
End Sub
In this example Combo1 is positioned beyond the bottom of the form. You can control the ReturnFocusTimer interval to however long you need.

How can I change the text of a label while running in visual basic?

Right now my label is having a text "hello", how can I change it to "world" by a button click while running in Visual Basic.
Write a button click event. It can be done either manually or by double clocking on the button in the designer view:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
End If
Notice the end of it how it says "Button1.Click". Change Button1 to whatever the name of your button is.
Now add the code to change your textbox's text:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Textbox1.text = "world"
End If
Again note the name of the textbox and change "Textbox1" to whatever your textbox is named.
I assume you are new to learning how to program since what you asked is pretty basic. Try to find some tutorials online to help you better understand things. In this case you should look up and understand how control properties work along with events.

Button click event to transfer image between 2 Picturebox

There are 25 Pictureboxes containing different image. User can click on the anyone of the pictureboxes and the image will be shown at the confirmation picturebox as a final decision to proceed.
The code I apply on here is PictureBoxConfirmation.Image = My.Resources._XX (XX will be 1-25 in number respectively.). Later on, I will present a cofirmation button for the user to finalize their decision, and the chosen image will be locked in PictureBoxFinal.
The problem is I can't implement the PictureBoxFinal.Image = My.Resources._XX anymore since the selection will be on randomize input by user. So, how do I code for the button to utilize this functionality. I had tried to put a tag for the PictureBoxConfirmaton when the image is being inserted to it. But, after that I am stuck. I Googled everything but still no result could help my situation.
You need to create a Event listener for the clik in all your PictureBoxes (this is a method/function that listen all you selectable pictureboxes). Inside the method, you get the Image from the clicked picturebox... and assign it to the PictureBoxFinal one.
To add the handler for the Click in all PictureBoxes:
AddHandler PictureBox1.Click, AddressOf pic_Click
AddHandler PictureBox2.Click, AddressOf pic_Click
AddHandler PictureBox3.Click, AddressOf pic_Click
...
The method that does the magic:
Private Sub pic_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim picClicked As PictureBox = DirectCast(sender, PictureBox)
PictureBoxFinal.Image = picClicked.Image
End Sub

How can I capture key ups/downs no matter what control on my form is the target?

I want to capture ctrl/alt/etc key ups and downs, no matter which control on my form gets the keyup or keydown event. Since I have about 100 controls on my form, it would be really ugly if I were to add code to each individual control. How can I accomplish this without having to do that?
PS: What's the difference between SetWindowsHook and SetWindowsHookEx?
You need to set the KeyPreview property of each Form to True. Subsequently, you can catch the keyboard events at the form level, in addition to the individual control level:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Debug.Print "Form_KeyDown"
End Sub
Private Sub Form_KeyPress(KeyAscii As Integer)
Debug.Print "Form_KeyPress"
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
Debug.Print "Form_KeyUp"
End Sub
Essentially, the form gets a "preview" of each keyboard event before the control, e.g.
Form_KeyDown
Control_KeyDown
Form_KeyUp
Control_KeyUp
As for SetWindowsHook & SetWindowsHookEx, the former is the original Win16 API call, and the latter is the Win32 and Win64 API call. SetWindowsHook is deprecated, and isn't in the current MSDN library, as far as I know.

Powerpoint VBA App_SlideShowBegin

In order to use the SlideShowBegin event in Powerpoint, you have to have a Class Module configured the following way:
Public WithEvents App As Application
Private Sub App_SlideShowBegin(ByVal Wn As SlideShowWindow)
MsgBox "SlideShowBegin"
End Sub
Then, inside of a non-class module, you have to create an object of that type and set the App to Application.
Dim X As New Class1
Sub InitializeApp()
Set X.App = Application
End Sub
Now, the only issue I have is, if you don't manually called InitializeApp with the Macro Menu in Powerpoint, the events don't work. You have to call this sub before anything can called at the beginning of a slideshow INCLUDING this sub.
How can I go about calling this sub before running my powerpoint? Is there a better way to do this?
EDIT:
I've tried using Class_Initialize but it only gets called once it is first used or you make a statement like Dim X as Class1; X = new Class1
Usually event handlers are installed as part of an add-in, where you'd initialize the class in the Auto_Open subroutine, which always runs when the add-in loads. If you want to include an event handler in a single presentation, one way to cause it to init is to include a shape that, when moused over or clicked fires a macro, which inits your event handler and goes to the next slide.
Answering to an old question, but I hope my solution might helpt somebody ending up at this question.
The general advice for this issue is using a plug-in or placing some element on the slide and when that is clicked or hovered perform the initialization. Both are not always desired so I have the following approach:
In some module:
Dim slideShowRunning As Boolean
-----------------------------
Sub SlideShowBegin(ByVal Wn As SlideShowWindow)
' Your code for start-up
End Sub
-----------------------------
Public Sub OnSlideShowPageChange(ByVal Wn As SlideShowWindow)
If TypeName(slideShowRunning) = "Empty" Or slideShowRunning = False Then
slideShowRunning = True
SlideShowBegin Wn
End If
End Sub
----------------------------
Public Sub OnSlideShowTerminate(ByVal Wn As SlideShowWindow)
slideShowRunning = False
End Sub
For me this works perfectly. NOTE I am by no means a vba expert, actually I might have less than 50 hours of vba programming (maybe only 8 in powerpoint). So this might be an horrible solution. I don't know, but for me it works so I liked to share.
In fact, OnSlideShowPageChange runs when slideshow begins. Just make sure it doesn't work in the subsequent page changes if not needed using a global variable. See answer from C. Binair for details.

Resources