throw KeyUp event in MouseHover event in VB.NET - vb.net

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!

Related

How to select multiple rows OR columns keeping "Ctrl" pressed in DataGridView object

I adapted code from this question
how to select rows on cellclick, and also columns on column header click?
into a single DataGridView1_MouseDown event, because it wasn't allowing me to select multiple rows/columns using the "Ctrl" key.
What I'd like, is to be able to select multiple rows (clicking on the row indices) OR multiple columns (clicking on the column headers) by selecting one after another keeping "Ctrl" pressed. I can easily get one OR the other (setting DataGridViewSelectionMode to either FullRowSelect or ColumnHeaderSelect) and then Ctrl works, but I'd like to have both functionality on the same DataGridView.
I feel like I'm so close. Thanks for any tips!
Private Sub DataGridView1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseDown
Dim ht As DataGridView.HitTestInfo
ht = Me.DataGridView1.HitTest(e.X, e.Y)
If e.Button = Windows.Forms.MouseButtons.Left Then
If ht.Type = DataGridViewHitTestType.Cell Then
DataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect
DataGridView1.CurrentCell.Selected = True
ElseIf ht.Type = DataGridViewHitTestType.RowHeader Then
DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
DataGridView1.Rows(ht.RowIndex).Selected = True
ElseIf ht.Type = DataGridViewHitTestType.ColumnHeader Then
DataGridView1.SelectionMode = DataGridViewSelectionMode.ColumnHeaderSelect
DataGridView1.Columns(ht.ColumnIndex).Selected = True
End If
End If
End Sub
I don't think you will be able to use both full column and full row select at the same time, after running some tests, it seems that changing the datagridview's selectionmode is what's clearing selections when it is set...so unless you were to create a custom control that inherits from datagridview and overrides some of its internals, you may be stuck. Without doing that, the only way I was able to achieve the behavior you're trying to get was to leave the datagridview cellselection mode set to cellselect, and do the row/column selections manually:
Private Sub DataGridView1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles DataGridView1.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim ht As DataGridView.HitTestInfo = Me.DataGridView1.HitTest(e.X, e.Y)
If Not My.Computer.Keyboard.CtrlKeyDown Then DataGridView1.ClearSelection()
If ht.Type = DataGridViewHitTestType.Cell Then
DataGridView1.CurrentCell.Selected = True
ElseIf ht.Type = DataGridViewHitTestType.RowHeader Then
For i As Integer = 0 To DataGridView1.Columns.Count - 1
DataGridView1.Rows(ht.RowIndex).Cells(i).Selected = True
Next
ElseIf ht.Type = DataGridViewHitTestType.ColumnHeader Then
For i As Integer = 0 To DataGridView1.Rows.Count - 1
DataGridView1.Rows(i).Cells(ht.ColumnIndex).Selected = True
Next
End If
End If
End Sub

SendKeys not working in Datagridview keydown event

I have a Datagridview on my form with two columns. I only want the cells in the first column to be selected. So I'm using the datagridview's keydown event to capture the TAB and SHIFT+TAB. So if the first row is selected and the user presses tab, the second row will be selected instead of staying on the first row and selecting the second column's cell.
My if statement for the TAB key is working fine, but for some reason the SHIFT+TAB statement isn't. The cell above the currently selected cell isn't getting selected. Nothing happens.
Private Sub myGrid_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles myGrid.KeyDown
If e.KeyCode = Keys.Tab Then
e.SuppressKeyPress = True
SendKeys.Send("{DOWN}")
End If
If e.Modifiers = Keys.Shift AndAlso e.KeyCode = Keys.Tab Then
e.SuppressKeyPress = True
SendKeys.Send("{UP}")
End If
End Sub
The really weird thing though is if I add a messagebox into the statement it works.
If e.Modifiers = Keys.Shift AndAlso e.KeyCode = Keys.Tab Then
MsgBox("test")
e.SuppressKeyPress = True
SendKeys.Send("{UP}")
End If
Any ideas?
Sendkeys is a last, last resort...
How about something like this:
Private Sub dgv_KeyDown(sender As Object, e As KeyEventArgs) Handles dgv.KeyDown
If e.KeyCode = Keys.Tab Then
e.SuppressKeyPress = True
dgv.CurrentCell = dgv(0, dgv.CurrentCell.RowIndex + 1)
End If
End Sub
With the help from #rheitzman I was able to see the main issue. The issue came from having the TAB and SHIFT+TAB in two different if statements. Here's my updated code:
Private Sub myGrid_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles myGrid.KeyDown
Dim row As String = Me.myGrid.CurrentCellAddress.Y
If e.Modifiers = Keys.Shift AndAlso e.KeyCode = Keys.Tab Then
e.SuppressKeyPress = True
Me.myGrid.CurrentCell = Me.myGrid.Rows(row - 1).Cells(0)
Me.myGrid.Rows(row - 1).Cells(0).Selected = True
ElseIf e.KeyCode = Keys.Tab Then
e.SuppressKeyPress = True
Me.myGrid.CurrentCell = Me.myGrid.Rows(row + 1).Cells(0)
Me.myGrid.Rows(row + 1).Cells(0).Selected = True
End If
End Sub
Tab and Shift+Tab are already handled by Windows Forms and DataGridView. They move the cell highlighter/selector move left(previous) and right(next). You'd have to create a class that inherits DataGridView and manually override their functionality to achieve what you want, then use that new class for your data grid.
Private Sub DataGridView1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown
Dim r As Integer = Me.DataGridView1.CurrentCellAddress.Y
Dim f As Integer = Me.DataGridView1.CurrentCellAddress.X
If e.Modifiers = Keys.Shift AndAlso e.KeyCode = Keys.Tab Then
e.SuppressKeyPress = True
If 0 = r Then
Me.DataGridView1.CurrentCell = Me.DataGridView1.Rows(DataGridView1.RowCount - 1).Cells(f - 1)
Me.DataGridView1.Rows(DataGridView1.RowCount - 1).Cells(f - 1).Selected = True
Else
Me.DataGridView1.CurrentCell = Me.DataGridView1.Rows(r - 1).Cells(f)
Me.DataGridView1.Rows(r - 1).Cells(f).Selected = True
End If
ElseIf e.KeyCode = Keys.Tab Then
e.SuppressKeyPress = True
If DataGridView1.RowCount = (r + 1) Then
Me.DataGridView1.CurrentCell = Me.DataGridView1.Rows(0).Cells(f + 1)
Me.DataGridView1.Rows(0).Cells(f + 1).Selected = True
Else
Me.DataGridView1.CurrentCell = Me.DataGridView1.Rows(r + 1).Cells(f)
Me.DataGridView1.Rows(r + 1).Cells(f).Selected = True
End If
End If
End Sub

VB.NET PictureBox / Controls

So interesting dilemma, I've managed to make a label completely invisible, to where I can use it for a click event on certain parts of a picture.
I then use that click event to call another picturebox into focus using picturebox3.visible = true..
The issue I'm having is when it's calling that picturebox visibility..the controls from the new picturebox (Invisible labels) seem to not function or be missing from the picture in picturebox2 completely.
I need to do this with about 30 different pictures in order to create a kind of "emulator" project for someone.
Any ideas on this? I can post code if needed. Picturebox + controls on picturebox = headache.
Public Class InvisibleLabel
Inherits Label
Public Sub New()
Me.SetStyle(ControlStyles.Opaque, True)
Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, False)
End Sub
Protected Overrides ReadOnly Property CreateParams() As System.Windows.Forms.CreateParams
Get
Dim CC As CreateParams = MyBase.CreateParams
CC.ExStyle = CC.ExStyle Or &H20
Return CC
End Get
End Property
End Class
This is the code for the invisible label, then I'm just using picturebox2.visible = true when certain parts of a picture are clicked.
I made 3 textboxes
textbox1 for X 'just for you to see
textbox2 for Y 'just for you to see
and
CurPicture to compare current image
my picturebox is 300,300
Private Sub PictureBox1_MouseClick(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseClick
Dim LocX As Integer = e.X
Dim LocY As Integer = e.Y
TextBox1.Text = e.X.ToString
TextBox2.Text = e.Y.ToString
If LocX > 200 Then ' click right side op the picture , change LocX With LocY to make it bottom
If CurPicture.Text = "1" Then
PictureBox1.Image = My.Resources.Pic2
CurPicture.Text = "2"
ElseIf CurPicture.Text = "2" Then
PictureBox1.Image = My.Resources.Pic3
CurPicture.Text = "3"
ElseIf CurPicture.Text = "3" Then
PictureBox1.Image = My.Resources.Pic4
CurPicture.Text = "4"
ElseIf CurPicture.Text = "4" Then
PictureBox1.Image = My.Resources.Pic5
CurPicture.Text = "5"
ElseIf CurPicture.Text = "5" Then
PictureBox1.Image = My.Resources.Pic1
CurPicture.Text = "1"
End If
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
PictureBox1.Image = My.Resources.Pic1
CurPicture.Text = "1"
End Sub
Hope this will help you get on the way :)
Use:
Private Sub PictureBox_MouseDown(sender As Object, e As MouseEventArgs) _
Handles PictureBox.MouseDown
'The code to change the picture goes here
End Sub

VB.NET Moving Objects Diagonally using Keys

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.

vb.net deleting lots of dynamically created buttons

I'm a new programmer to vb.net, so apologise for what is likely to be ignorance.
I’m building a simple gui for a database interface, with many parent and child items within it. Upon a form I create buttons depending on how many items (parents/children). I've got the creation of the buttons thus:
For RowNumber As Integer = 0 To NoOfRows
Dim Buttoni As New Button
Buttoni.Location = New Point(LocationX, LocationY)
Buttoni.Width = 100
Buttoni.Height = 40
Buttoni.Visible = True
Buttoni.Text = DatasetA.Tables(0).Rows(RowNumber).Item("Name")
ButtonName = "Button" + RowNumber.ToString
If LocationX < FormWidth - (SpacePerButtonX * 2) Then
LocationX = LocationX + SpacePerButtonX
Else
LocationX = 50
LocationY = LocationY + SpacePerButtonY
End If
AddHandler Buttoni.Click, AddressOf DynamicButtonClick
Me.Controls.Add(Buttoni)
Buttoni.BringToFront() 'brings newest buttons to front!
Next
But I’m struggling with a way to delete the buttons to make way for a new set to replace them... I can delete a single one upon its click, but I’d like to delete all of the buttons that have been created in this way before re-creating them.
I hope that makes sense and there is a fairly simple way to accomplish this..?
I will add, in your creation loop, some value to the Tag property.
This will help to differentiate the buttons created dinamically from the buttons created statically in your form.
Buttoni.Tag = 1
Then, to delete a button, loop in reverse order on the Me.Controls collection,
check if you get a button and if the Tag property IsNot Nothing
For x as Integer = Me.Controls.Count - 1 to 0 step -1)
Dim b as Button = TryCast(Me.Controls(x), Button)
If b IsNot Nothing AndAlso b.Tag IsNot Nothing then
b.Dispose() '' NOTE: disposing the button also removes it
End If
Next
It's hard to know exactly what you want to do. I guess you could just use the same technique in reverse, something like
For i As Integer = Me.Controls.Count - 1 To 0 Step -1
Dim ctrl = Me.Controls(i)
If TypeOf (ctrl) Is Button Then
ctrl.Dispose() '' NOTE: disposing the control also removes it
End If
Next
Create a button and delete it with double click!
Easy Code :
Dim b As New Button
Private btn As Button ' this is a reference object
Private ptX, ptY As Integer
Private drag As Boolean
Private Sub nodebtn_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If e.Clicks = 2 Then
b.Dispose()
End If
If e.Button = MouseButtons.Left Then
drag = True
btn = CType(sender, Button)
ptX = e.X : ptY = e.Y
End If
End Sub
Private Sub nodebtn_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If drag Then
btn.Location = New Point(btn.Location.X + e.X - ptX, btn.Location.Y + e.Y - ptY)
Me.Refresh()
End If
End Sub
Private Sub nodebtn_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
drag = False
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
b.Location = New Point(10, 10)
b.Size = New Size(110, 29)
b.BringToFront()
b.Text = "Button"
AddHandler b.MouseDown, AddressOf nodebtn_MouseDown
AddHandler b.MouseMove, AddressOf nodebtn_MouseMove
AddHandler b.MouseUp, AddressOf nodebtn_MouseUp
Me.Controls.Add(b)
End Sub