Can you help me with this in VB.Net
Im trying to have a shortcut here for my AnnotateViewer
when i press "Ctrl + Down"
the view must scroll down.
Here's the code that i tried
Dim pt As Point = iView.ScrollPosition
If e.Control And e.KeyCode = Keys.NumPad0 Then
pt.Y -= 99999
Msg("Scroll Down")
End If
iView.ScrollPosition = new Point(x,y)
Change the value of x for horizontal scroll, and y for vertical scroll bar.
yeah now it works.
I used these codes
If e.Control And e.KeyCode = Keys.NumPad2 Then
iView.ScrollPosition += New Point(0, -90)
ElseIf e.Control And e.KeyCode = Keys.NumPad8 Then
iView.ScrollPosition += New Point(0, 90)
ElseIf e.Control And e.KeyCode = Keys.NumPad4 Then
iView.ScrollPosition += New Point(50, 0)
ElseIf e.Control And e.KeyCode = Keys.NumPad6 Then
iView.ScrollPosition += New Point(-50, 0)
End If
Related
I am trying to make a game in Win forms using VB from scratch which i know is a bad idea but i like the challenge. Therefore i have been testing movement systems so i can choose my favourite , however i have ran into an issue as trying to move with a picture box as when i press the movement key the picture box just starts erasing the image in the direction i wanted to move until it disappears.
I am using
Public Class Form1
Dim RightM As Boolean
Dim LeftM As Boolean
Dim UpM As Boolean
Dim DownM As Boolean
Sub Movement()
Do While UpM = True
PictureBox1.Top += -5
Threading.Thread.Sleep(20)
Loop
Do While LeftM = True
PictureBox1.Left += -5
Threading.Thread.Sleep(20)
Loop
Do While DownM = True
PictureBox1.Top += 5
Threading.Thread.Sleep(20)
Loop
Do While RightM = True
PictureBox1.Left += 5
Threading.Thread.Sleep(20)
Loop
Do While (UpM = True) And (RightM = True)
PictureBox1.Top += -5
PictureBox1.Left += 5
Threading.Thread.Sleep(20)
Loop
Do While (UpM = True) And (LeftM = True)
PictureBox1.Top += -5
PictureBox1.Left += -5
Threading.Thread.Sleep(20)
Loop
Do While (DownM = True) And (RightM = True)
PictureBox1.Top += 5
PictureBox1.Left += 5
Threading.Thread.Sleep(20)
Loop
Do While (DownM = True) And (LeftM = True)
PictureBox1.Top += 5
PictureBox1.Left += -5
Loop
End Sub
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.A Then
LeftM = True
Movement()
ElseIf e.KeyCode = Keys.D Then
RightM = True
Movement()
ElseIf e.KeyCode = Keys.W Then
UpM = True
Movement()
ElseIf e.KeyCode = Keys.S Then
DownM = True
Movement()
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
If e.KeyCode = Keys.A Then
LeftM = False
Movement()
ElseIf e.KeyCode = Keys.D Then
RightM = False
Movement()
ElseIf e.KeyCode = Keys.W Then
UpM = False
Movement()
ElseIf e.KeyCode = Keys.S Then
DownM = False
Movement()
End If
End Sub
End Class
The question took me back to the Z80 days, I got a little nostalgic so decided to post a simplified answer.
'Position of PictureBox
Private Plocation As Point
'PictureBox Movement Boundries
Private XL As Integer 'X - Left
Private XR As Integer 'X - Right
Private YT As Integer 'Y - Top
Private YB As Integer 'Y - Bottom
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
'Set Boundries
XL = 0
XR = Me.ClientSize.Width - PictureBox1.Width
YT = 0
YB = Me.ClientSize.Height - PictureBox1.Height
'Position PictureBox roughly in the centre of the Form
Plocation = New Point((Me.ClientSize.Width / 2) - (PictureBox1.Width / 2), (Me.ClientSize.Height / 2) - (PictureBox1.Height / 2))
PictureBox1.Location = Plocation
End Sub
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
'Change X,Y depending on KeyPress
If e.KeyCode = Keys.A Then Plocation.X -= 5
If e.KeyCode = Keys.D Then Plocation.X += 5
If e.KeyCode = Keys.W Then Plocation.Y -= 5
If e.KeyCode = Keys.S Then Plocation.Y += 5
'Check for X,Y boundries
If Plocation.X < XL Then Plocation.X = XL
If Plocation.X > XR Then Plocation.X = XR
If Plocation.Y < YT Then Plocation.Y = YT
If Plocation.Y > YB Then Plocation.Y = YB
'Update Position
PictureBox1.Location = Plocation
End Sub
I'm intentionally checking all key presses one after another, helps with diagonal movements. It's a little choppy and basic but may give you some ideas. Happy programming...
I am trying to capture a KeyUp event when the mouse is over the map. I am using GMaps library.
I have tried using the keypress, keydown and keyup events of my gmap control (where the map is placed), and I have tried to use the keypress, keydown and keyup from the form events, but nothing happens.
In my form, I have also some buttons, and what I see when I press one of the rows keys is that the focus is changing among the buttons, The effect is as if you wanted to select a button using the arrows on the keyboard, and I don't know why is doing that, if I have a specific code in the key events of the program.
In the MouveHover event I am using this code:
Private Sub GMapControl_MouseHover(sender As Object, e As EventArgs) Handles GMapControl.MouseHover
If Keys.Up <> Keys.None Then
GMapControl.Position = New PointLatLng(GMapControl.Position.Lat + 1, GMapControl.Position.Lng)
End If
End Sub
But I have not the behavior I am looking for. Also I am trying to use the KeyUp event of the form:
Private Sub BMS_KeyUp(sender As Object, e As KeyEventArgs) Handles MyBase.KeyUp
If e.KeyCode.ToString = "Up" Then
GMapControl.Position = New PointLatLng(GMapControl.Position.Lat + 1, GMapControl.Position.Lng)
ElseIf e.KeyCode.ToString = "Down" Then
GMapControl.Position = New PointLatLng(GMapControl.Position.Lat - 1, GMapControl.Position.Lng)
ElseIf e.KeyCode.ToString = "Left" Then
GMapControl.Position = New PointLatLng(GMapControl.Position.Lat, GMapControl.Position.Lng - 1)
ElseIf e.KeyCode.ToString = "Right" Then
GMapControl.Position = New PointLatLng(GMapControl.Position.Lat, GMapControl.Position.Lng + 1)
End If
End Sub
Also I have tried the same in the KeyUp event of the GMap control:
Private Sub GMapControl_KeyUp(sender As Object, e As KeyEventArgs) Handles GMapControl.KeyUp
If e.KeyCode.ToString = "Up" Then
GMapControl.Position = New PointLatLng(GMapControl.Position.Lat + 1, GMapControl.Position.Lng)
ElseIf e.KeyCode.ToString = "Down" Then
GMapControl.Position = New PointLatLng(GMapControl.Position.Lat - 1, GMapControl.Position.Lng)
ElseIf e.KeyCode.ToString = "Left" Then
GMapControl.Position = New PointLatLng(GMapControl.Position.Lat, GMapControl.Position.Lng - 1)
ElseIf e.KeyCode.ToString = "Right" Then
GMapControl.Position = New PointLatLng(GMapControl.Position.Lat, GMapControl.Position.Lng + 1)
End If
End Sub
And nothing...
Maybe someone has some ideas to do this?
Thanks!
Sorry, I have continued finding an answer and I have found the response of what I was looking for on this thread
https://social.msdn.microsoft.com/Forums/windows/en-US/ffeeea42-f6ba-420f-827e-74879fd29b26/how-to-detect-arrow-keys-in-vbnet?forum=winforms
By using the property:
Me.KeyPreview = True
In the Load method of the form, and using this code in the KeyUp of the form:
Private Sub BMS_KeyUp(sender As Object, e As KeyEventArgs) Handles MyBase.KeyUp
Select Case e.KeyCode
Case Keys.Right
GMapControl.Position = New PointLatLng(GMapControl.Position.Lat, GMapControl.Position.Lng + 0.1)
e.Handled = True
Case Keys.Left
GMapControl.Position = New PointLatLng(GMapControl.Position.Lat, GMapControl.Position.Lng - 0.1)
e.Handled = True
Case Keys.Up
GMapControl.Position = New PointLatLng(GMapControl.Position.Lat + 0.1, GMapControl.Position.Lng)
e.Handled = True
Case Keys.Down
GMapControl.Position = New PointLatLng(GMapControl.Position.Lat - 0.1, GMapControl.Position.Lng)
e.Handled = True
End Select
End Sub
Anyway I would like to ask one thing I don't understand. I have tried this before but without using the Me.KeyPreview property, and without using the e.Handled = true property.
Could someone explain to me why using this two lines I have the behavior I want, and why without using them I was not able to do it?
Thanks!
I'm writing a "quick 'n dirty" image editor. The user can load an image into pbOriginal, drag a cropping square in it, and have it displayed in pbAltered. The functionality works well for my purposes.
However I'm having problems coding when the user mouses into the picturebox with the mouse button down. (Kind of like a drag event, but I'm not trying to drag anything in; I just want to capture the enter with the mouse down.) Ideally I'd like to have it start the selection process as soon as it detects entry and detects the active LMB, but nothing I've thrown at it so far seems to work.
Here are two code snippets I have. The first detects the MouseDown event, the second detects the MouseMove event. I'm looking to have something mimicking the actions of the MouseDown function with MouseEnter. (The second function is irrelevant, but I'm including it for the sake of completeness.)
Private Sub pbOriginal_MouseDown(sender As Object, e As MouseEventArgs) Handles pbOriginal.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
cropX = e.X
cropY = e.Y
cropPen = New Pen(Color.Red, 2)
cropPen.DashStyle = DashStyle.DashDotDot
Altered = Nothing
End If
pbOriginal.Refresh()
End Sub
Private Sub pbOriginal_MouseMove(sender As Object, e As MouseEventArgs) Handles pbOriginal.MouseMove
Cursor = Cursors.Cross
If pbOriginal.Image Is Nothing Then Exit Sub
If cropX < 0 Or cropY < 0 Then Exit Sub
If e.Button = Windows.Forms.MouseButtons.Left And e.X <= pbOriginal.Right - pbOriginal.Left And e.X >= 0 And e.Y <= pbOriginal.Bottom - pbOriginal.Top And e.Y >= 0 Then
pbOriginal.Refresh()
Dim OldWidth As Integer = cropWidth
Dim OldHeight As Integer = cropHeight
cropWidth = e.X - cropX
cropHeight = e.Y - cropY
'Verify that we're maintaining a square
If cropWidth > cropHeight Then
If cropWidth > OldWidth Then
cropWidth = cropHeight
Else
cropHeight = cropWidth
End If
End If
If cropHeight > cropWidth Then
If cropHeight > OldHeight Then
cropHeight = cropWidth
Else
cropWidth = cropHeight
End If
End If
If cropWidth <= 0 Or cropHeight <= 0 Then
Altered = Nothing
btnCropNext.Enabled = False
Exit Sub
End If
pbOriginal.CreateGraphics.DrawRectangle(cropPen, cropX, cropY, cropWidth, cropHeight)
If cropWidth <> 0 And cropHeight <> 0 Then
Dim WRatio As Double = Original.Width / pbOriginal.Width
Dim HRatio As Double = Original.Height / pbOriginal.Height
Dim rect As Rectangle = New Rectangle(cropX * WRatio, cropY * HRatio, cropWidth * WRatio, cropHeight * HRatio)
'First we define a rectangle with the help of already calculated points
Dim OriginalImage As Bitmap = New Bitmap(Original, Original.Width, Original.Height)
'Original image
Dim _img As New Bitmap(CInt(cropWidth * WRatio), CInt(cropHeight * HRatio)) ' for cropinf image
Dim g As Graphics = Graphics.FromImage(_img) ' create graphics
g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
g.PixelOffsetMode = Drawing2D.PixelOffsetMode.Default
g.CompositingQuality = Drawing2D.CompositingQuality.HighQuality
'set image attributes
g.DrawImage(OriginalImage, 0, 0, rect, GraphicsUnit.Pixel)
Altered = _img
btnCropNext.Enabled = True
OriginalImage.Dispose()
Else
Altered = Nothing
btnCropNext.Enabled = False
End If
End If
End Sub
Why dont you use MouseEnter Event , and test if the LMB if its clicked, if so make your selection progress, other wise neglect.
More Info About MouseEnter
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mouseenter(v=vs.110).aspx
if you dont want to use MouseEnter , in MouseMove test each picture if MouseOver and the LMB . but i think the first easier and faster to handle
I have a problem when I try to make Keyboard Tester application for my small office.
I cant detect print screen keycode e.keycode = keys.PrintScreen.
I will do something like change picturebox back color when a key down, but it seems doesnt work with print screen, nothing happen.
my code is:
Private Sub keyboardmenu_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
'Esc + Function Keys -----------------------------------------
If e.KeyCode = Keys.Escape Then
EscBox.BackColor = Color.Red
End If
If e.KeyCode = Keys.F1 Then
F1Box.BackColor = Color.Red
End If
If e.KeyCode = Keys.F2 Then
F2Box.BackColor = Color.Red
End If
If e.KeyCode = Keys.F3 Then
F3Box.BackColor = Color.Red
End If
If e.KeyCode = Keys.F4 Then
F4Box.BackColor = Color.Red
End If
If e.KeyCode = Keys.F5 Then
F5Box.BackColor = Color.Red
End If
If e.KeyCode = Keys.F6 Then
F6Box.BackColor = Color.Red
End If
If e.KeyCode = Keys.F7 Then
F7Box.BackColor = Color.Red
End If
If e.KeyCode = Keys.F8 Then
F8Box.BackColor = Color.Red
End If
If e.KeyCode = Keys.F9 Then
F9Box.BackColor = Color.Red
End If
If e.KeyCode = Keys.F10 Then
F10Box.BackColor = Color.Red
End If
If e.KeyCode = Keys.F11 Then
F11Box.BackColor = Color.Red
End If
If e.KeyCode = Keys.F12 Then
F12Box.BackColor = Color.Red
End If
'End of Esc + Function Keys -----------------------------------------
Private Sub keyboardmenu_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
'Esc + Function Keys ----------------------------------------
If e.KeyCode = Keys.F1 Then
F1Box.BackColor = Color.Transparent
End If
If e.KeyCode = Keys.F2 Then
F2Box.BackColor = Color.Transparent
End If
If e.KeyCode = Keys.F3 Then
F3Box.BackColor = Color.Transparent
End If
If e.KeyCode = Keys.F4 Then
F4Box.BackColor = Color.Transparent
End If
If e.KeyCode = Keys.F5 Then
F5Box.BackColor = Color.Transparent
End If
If e.KeyCode = Keys.F6 Then
F6Box.BackColor = Color.Transparent
End If
If e.KeyCode = Keys.F7 Then
F7Box.BackColor = Color.Transparent
End If
If e.KeyCode = Keys.F8 Then
F8Box.BackColor = Color.Transparent
End If
If e.KeyCode = Keys.F9 Then
F9Box.BackColor = Color.Transparent
End If
If e.KeyCode = Keys.F10 Then
F10Box.BackColor = Color.Transparent
End If
If e.KeyCode = Keys.F11 Then
F11Box.BackColor = Color.Transparent
End If
If e.KeyCode = Keys.F12 Then
F12Box.BackColor = Color.Transparent
End If
'End of Esc + Function Keys -----------------------------------------
End Sub
Please help me. Idk if there are more keys like print screen problem.
Thank You
I'm not sure of the real reason, but I've read about it before and came to the conclusion that Windows is protecting that key's event from being easily handled. Someone else probably knows better, but this works:
Protected Overrides Function ProcessKeyEventArgs(ByRef msg As Message) As Boolean
If msg.WParam = Keys.PrintScreen Then
MessageBox.Show("PrintScreen key press detected!")
End If
Return MyBase.ProcessKeyEventArgs(msg)
End Function
Also, you should put all of those if statements in a Select Case statement:
Private Sub keyboardmenu_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles Me.KeyDown
Select Case e.KeyCode
Case Keys.Escape
EscBox.BackColor = Color.Red
Case Keys.F1
F1Box.BackColor = Color.Red
Case Keys.F2
F2Box.BackColor = Color.Red
'Etc
End Select
End Sub
This comment is on the IF statements and CASE suggestion, otherwise I concur with the answer by Keith.
I use this sort of thing fairly often, but it's crazy to put all those IF statements, OR the Case statement:
Simply name the boxes to match the enumeration, then (assuming Controls is the parent container of all the boxes)
Controls(e.keycode.tostring & "box").backcolor = Color.Red
and
Controls(e.keycode.tostring & "box").backcolor = Color.Transparent
This one line will replace everything (if you rename the escbox to escapebox)
Of course, you might want to do some checking like
If Controls.ContainsKey(e.keycode.tostring & "box") Then ...
How can I Move objects diagonally when two directional keys are pressed? How to do that?
I tried adding elseif statement for Handling Up and Right together but It still move up or right when i press them together
Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.Right Then
Label1.Location = New Point(Label1.Location.X + 5, Label1.Location.Y)
ElseIf e.KeyCode = Keys.Left Then
Label1.Location = New Point(Label1.Location.X - 5, Label1.Location.Y)
ElseIf e.KeyCode = Keys.Down Then
Label1.Location = New Point(Label1.Location.X, Label1.Location.Y + 5)
ElseIf e.KeyCode = Keys.Up Then
Label1.Location = New Point(Label1.Location.X, Label1.Location.Y - 5)
ElseIf e.KeyCode = Keys.Up And e.KeyCode = Keys.Right Then
Label1.Location = New Point(Label1.Location.X + 5, Label1.Location.Y + 5)
End If
CheckIntersections()
If Label1.Location.Y < 0 Then
Label1.Location = New Point(Label1.Location.X, Me.Height)
ElseIf Label1.Location.Y > Me.Height Then
Label1.Location = New Point(Label1.Location.X, Me.Bottom = 0)
ElseIf Label1.Location.X < 0 Then
Label1.Location = New Point(Me.Width, Label1.Location.Y)
ElseIf Label1.Location.X > Me.Width Then
Label1.Location = New Point(0, Label1.Location.Y)
End If
End Sub
The KeyDown event occurs each time a single key is pressed. Therefore you'll need to remember when a cursor key is pressed on once occurrence of KeyDown to check in another occurrence for another cursor key being pressed.
Remember to subscribe to KeyUp events to clear the state because the use could press one cursor key, release it and then press another.
Your code will never work:
ElseIf e.KeyCode = Keys.Up And e.KeyCode = Keys.Right Then
because this cannot be true. KeyCode is a single key code, it cannot be both one key and another.