Move PictureBox with Arrow Keys?
I use the following code to handle positioning of certain controls in my Form;
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
'Sub detects which arrow key is pressed
Dim strControlName As String
' Get the name of the control
strControlName = Me.ActiveControl.Name
Dim aControl = Me.Controls.Item(strControlName)
If strControlName <> "PrintButton" Then
If keyData = Keys.Up Then
aControl.Location = New Point(aControl.Location.X, aControl.Location.Y - 1)
Return True
End If
'detect down arrow ke
If keyData = Keys.Down Then
aControl.Location = New Point(aControl.Location.X, aControl.Location.Y + 1)
Return True
End If
'detect left arrow key
If keyData = Keys.Left Then
aControl.Location = New Point(aControl.Location.X - 1, aControl.Location.Y)
Return True
End If
'detect right arrow key
If keyData = Keys.Right Then
aControl.Location = New Point(aControl.Location.X + 1, aControl.Location.Y)
Return True
End If
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
I also have a PictureBox that I allow a Drag n Drop image into;
Private Sub pbSig_DragDrop(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles pbSig.DragDrop
Dim picbox As PictureBox = CType(sender, PictureBox)
Dim files() As String = CType(e.Data.GetData(DataFormats.FileDrop), String())
If files.Length <> 0 Then
Try
picbox.Image = Image.FromFile(files(0))
pbSig.ImageLocation = files(0)
Catch ex As Exception
MessageBox.Show("Problem opening file ")
End Try
End If
End Sub
Private Sub pbSig_DragEnter(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles pbSig.DragEnter
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub
Is there a way I can make the PictureBox "movable" using the arrow keys? I can't use a KeyPress event on the Form since I am already using it elsewhere. I was hoping I could set a focus on the PictureBox or allow the user to do a "+Arrow" event.
Also, if I make the PictureBox move, is the dropped image going to move with it?
vb.net
add a comment |
I use the following code to handle positioning of certain controls in my Form;
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
'Sub detects which arrow key is pressed
Dim strControlName As String
' Get the name of the control
strControlName = Me.ActiveControl.Name
Dim aControl = Me.Controls.Item(strControlName)
If strControlName <> "PrintButton" Then
If keyData = Keys.Up Then
aControl.Location = New Point(aControl.Location.X, aControl.Location.Y - 1)
Return True
End If
'detect down arrow ke
If keyData = Keys.Down Then
aControl.Location = New Point(aControl.Location.X, aControl.Location.Y + 1)
Return True
End If
'detect left arrow key
If keyData = Keys.Left Then
aControl.Location = New Point(aControl.Location.X - 1, aControl.Location.Y)
Return True
End If
'detect right arrow key
If keyData = Keys.Right Then
aControl.Location = New Point(aControl.Location.X + 1, aControl.Location.Y)
Return True
End If
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
I also have a PictureBox that I allow a Drag n Drop image into;
Private Sub pbSig_DragDrop(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles pbSig.DragDrop
Dim picbox As PictureBox = CType(sender, PictureBox)
Dim files() As String = CType(e.Data.GetData(DataFormats.FileDrop), String())
If files.Length <> 0 Then
Try
picbox.Image = Image.FromFile(files(0))
pbSig.ImageLocation = files(0)
Catch ex As Exception
MessageBox.Show("Problem opening file ")
End Try
End If
End Sub
Private Sub pbSig_DragEnter(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles pbSig.DragEnter
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub
Is there a way I can make the PictureBox "movable" using the arrow keys? I can't use a KeyPress event on the Form since I am already using it elsewhere. I was hoping I could set a focus on the PictureBox or allow the user to do a "+Arrow" event.
Also, if I make the PictureBox move, is the dropped image going to move with it?
vb.net
The code you have is working correctly. You have just one problem: a PictureBox can't be the active control (not using the default class style, it needsControlStyles.Selectable
). If you changeaControl.Location = (...)
with the name of a PictureBox (e.g.,pbSig.Location = (...)
, then the PictureBox will move and will accept a drop (provided you haveset pbSig.AllowDrop = True
somewhere), setting the new image. You have a typo inpbSig_DragDrop
=>pbSig.ImageLocation = files(0)
should bepicbox.ImageLocation = files(0)
.
– Jimi
Nov 13 '18 at 7:42
add a comment |
I use the following code to handle positioning of certain controls in my Form;
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
'Sub detects which arrow key is pressed
Dim strControlName As String
' Get the name of the control
strControlName = Me.ActiveControl.Name
Dim aControl = Me.Controls.Item(strControlName)
If strControlName <> "PrintButton" Then
If keyData = Keys.Up Then
aControl.Location = New Point(aControl.Location.X, aControl.Location.Y - 1)
Return True
End If
'detect down arrow ke
If keyData = Keys.Down Then
aControl.Location = New Point(aControl.Location.X, aControl.Location.Y + 1)
Return True
End If
'detect left arrow key
If keyData = Keys.Left Then
aControl.Location = New Point(aControl.Location.X - 1, aControl.Location.Y)
Return True
End If
'detect right arrow key
If keyData = Keys.Right Then
aControl.Location = New Point(aControl.Location.X + 1, aControl.Location.Y)
Return True
End If
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
I also have a PictureBox that I allow a Drag n Drop image into;
Private Sub pbSig_DragDrop(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles pbSig.DragDrop
Dim picbox As PictureBox = CType(sender, PictureBox)
Dim files() As String = CType(e.Data.GetData(DataFormats.FileDrop), String())
If files.Length <> 0 Then
Try
picbox.Image = Image.FromFile(files(0))
pbSig.ImageLocation = files(0)
Catch ex As Exception
MessageBox.Show("Problem opening file ")
End Try
End If
End Sub
Private Sub pbSig_DragEnter(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles pbSig.DragEnter
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub
Is there a way I can make the PictureBox "movable" using the arrow keys? I can't use a KeyPress event on the Form since I am already using it elsewhere. I was hoping I could set a focus on the PictureBox or allow the user to do a "+Arrow" event.
Also, if I make the PictureBox move, is the dropped image going to move with it?
vb.net
I use the following code to handle positioning of certain controls in my Form;
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
'Sub detects which arrow key is pressed
Dim strControlName As String
' Get the name of the control
strControlName = Me.ActiveControl.Name
Dim aControl = Me.Controls.Item(strControlName)
If strControlName <> "PrintButton" Then
If keyData = Keys.Up Then
aControl.Location = New Point(aControl.Location.X, aControl.Location.Y - 1)
Return True
End If
'detect down arrow ke
If keyData = Keys.Down Then
aControl.Location = New Point(aControl.Location.X, aControl.Location.Y + 1)
Return True
End If
'detect left arrow key
If keyData = Keys.Left Then
aControl.Location = New Point(aControl.Location.X - 1, aControl.Location.Y)
Return True
End If
'detect right arrow key
If keyData = Keys.Right Then
aControl.Location = New Point(aControl.Location.X + 1, aControl.Location.Y)
Return True
End If
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
I also have a PictureBox that I allow a Drag n Drop image into;
Private Sub pbSig_DragDrop(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles pbSig.DragDrop
Dim picbox As PictureBox = CType(sender, PictureBox)
Dim files() As String = CType(e.Data.GetData(DataFormats.FileDrop), String())
If files.Length <> 0 Then
Try
picbox.Image = Image.FromFile(files(0))
pbSig.ImageLocation = files(0)
Catch ex As Exception
MessageBox.Show("Problem opening file ")
End Try
End If
End Sub
Private Sub pbSig_DragEnter(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles pbSig.DragEnter
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub
Is there a way I can make the PictureBox "movable" using the arrow keys? I can't use a KeyPress event on the Form since I am already using it elsewhere. I was hoping I could set a focus on the PictureBox or allow the user to do a "+Arrow" event.
Also, if I make the PictureBox move, is the dropped image going to move with it?
vb.net
vb.net
asked Nov 13 '18 at 5:07
MgfranzMgfranz
227
227
The code you have is working correctly. You have just one problem: a PictureBox can't be the active control (not using the default class style, it needsControlStyles.Selectable
). If you changeaControl.Location = (...)
with the name of a PictureBox (e.g.,pbSig.Location = (...)
, then the PictureBox will move and will accept a drop (provided you haveset pbSig.AllowDrop = True
somewhere), setting the new image. You have a typo inpbSig_DragDrop
=>pbSig.ImageLocation = files(0)
should bepicbox.ImageLocation = files(0)
.
– Jimi
Nov 13 '18 at 7:42
add a comment |
The code you have is working correctly. You have just one problem: a PictureBox can't be the active control (not using the default class style, it needsControlStyles.Selectable
). If you changeaControl.Location = (...)
with the name of a PictureBox (e.g.,pbSig.Location = (...)
, then the PictureBox will move and will accept a drop (provided you haveset pbSig.AllowDrop = True
somewhere), setting the new image. You have a typo inpbSig_DragDrop
=>pbSig.ImageLocation = files(0)
should bepicbox.ImageLocation = files(0)
.
– Jimi
Nov 13 '18 at 7:42
The code you have is working correctly. You have just one problem: a PictureBox can't be the active control (not using the default class style, it needs
ControlStyles.Selectable
). If you change aControl.Location = (...)
with the name of a PictureBox (e.g., pbSig.Location = (...)
, then the PictureBox will move and will accept a drop (provided you have set pbSig.AllowDrop = True
somewhere), setting the new image. You have a typo in pbSig_DragDrop
=> pbSig.ImageLocation = files(0)
should be picbox.ImageLocation = files(0)
.– Jimi
Nov 13 '18 at 7:42
The code you have is working correctly. You have just one problem: a PictureBox can't be the active control (not using the default class style, it needs
ControlStyles.Selectable
). If you change aControl.Location = (...)
with the name of a PictureBox (e.g., pbSig.Location = (...)
, then the PictureBox will move and will accept a drop (provided you have set pbSig.AllowDrop = True
somewhere), setting the new image. You have a typo in pbSig_DragDrop
=> pbSig.ImageLocation = files(0)
should be picbox.ImageLocation = files(0)
.– Jimi
Nov 13 '18 at 7:42
add a comment |
2 Answers
2
active
oldest
votes
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
KeyPreview = True
End Sub
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Up Then
PictureBox1.Top -= 5
End If
If e.KeyCode = Keys.Down Then
PictureBox1.Top += 5
End If
If e.KeyCode = Keys.Left Then
PictureBox1.Left -= 5
End If
If e.KeyCode = Keys.Right Then
PictureBox1.Left += 5
End If
End Sub
You can use this code to move the PictureBox
using Arrow Keys.
The OP is already overridingProcessCmdKey
, which is the right thing to do (one of) in this scenario, since it will allows a correct processing of the Keys when automatic repetition is an option.
– Jimi
Nov 13 '18 at 7:54
And I also stated that I can't use a KeyPress event since I am already using it elsewhere. Unless...
– Mgfranz
Nov 13 '18 at 14:20
@Mgfranz : For the record you can attach multiple event handlers to a single event. In VB.NET you either useAddHandler
or (in cases like this) just create another method and make sure it hasHandles Me.KeyDown
on it. The actual name of the method isn't important. You could name itForm1_KeyDown_2
or just as wellMyHappyBanana
if you'd like.
– Visual Vincent
Nov 13 '18 at 17:37
@Vincent, that is exactly what I was thinking. Just making another event handler to handle KeyDown. Thanks.
– Mgfranz
Nov 13 '18 at 21:48
add a comment |
Here is what I ended up using. The Mouse just much better sense, plus I get to store it to Settings along with the other settings. I think this is a good solution without getting into any DB work. Opinions?
Private Sub CheckForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'...
pbSig.Location = My.Settings.pbSigLoc
'Allow an image to be dropped
pbSig.AllowDrop = True
End Sub
End Sub
' The next three subs control the moving of the pbSig location using the mouse
Dim startX As Integer
Dim startY As Integer
Dim endX As Integer
Dim endY As Integer
Dim mDown As Boolean
Dim valX As Boolean
Dim valY As Boolean
Private Sub pbSig_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles pbSig.MouseDown
startX = MousePosition.X
startY = MousePosition.Y
mDown = True
valX = False
valY = False
End Sub
Private Sub Main_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
End Sub
Private Sub pbSig_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles pbSig.MouseMove
'Check if mouse=down
If mDown = True Then
endX = (MousePosition.X - Me.Left)
endY = (MousePosition.Y - Me.Top)
If valY = False Then
startY = endY - sender.top
valY = True
End If
If valX = False Then
startX = endX - sender.left
valX = True
End If
sender.left = endX - startX
sender.top = endY - startY
End If
End Sub
'If mouseUp=True then End and Save to Settings
Private Sub pbSig_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles pbSig.MouseUp
My.Settings.pbSigLoc = pbSig.Location
mDown = False
valX = False
valY = False
End Sub
This way all the user needs to do is use their mouse to locate the pB and it's contents and I don't need to call the ProcessCmdKey again. And I still have the arrow keys functionality on the Controls I need it to be.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53274172%2fmove-picturebox-with-arrow-keys%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
KeyPreview = True
End Sub
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Up Then
PictureBox1.Top -= 5
End If
If e.KeyCode = Keys.Down Then
PictureBox1.Top += 5
End If
If e.KeyCode = Keys.Left Then
PictureBox1.Left -= 5
End If
If e.KeyCode = Keys.Right Then
PictureBox1.Left += 5
End If
End Sub
You can use this code to move the PictureBox
using Arrow Keys.
The OP is already overridingProcessCmdKey
, which is the right thing to do (one of) in this scenario, since it will allows a correct processing of the Keys when automatic repetition is an option.
– Jimi
Nov 13 '18 at 7:54
And I also stated that I can't use a KeyPress event since I am already using it elsewhere. Unless...
– Mgfranz
Nov 13 '18 at 14:20
@Mgfranz : For the record you can attach multiple event handlers to a single event. In VB.NET you either useAddHandler
or (in cases like this) just create another method and make sure it hasHandles Me.KeyDown
on it. The actual name of the method isn't important. You could name itForm1_KeyDown_2
or just as wellMyHappyBanana
if you'd like.
– Visual Vincent
Nov 13 '18 at 17:37
@Vincent, that is exactly what I was thinking. Just making another event handler to handle KeyDown. Thanks.
– Mgfranz
Nov 13 '18 at 21:48
add a comment |
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
KeyPreview = True
End Sub
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Up Then
PictureBox1.Top -= 5
End If
If e.KeyCode = Keys.Down Then
PictureBox1.Top += 5
End If
If e.KeyCode = Keys.Left Then
PictureBox1.Left -= 5
End If
If e.KeyCode = Keys.Right Then
PictureBox1.Left += 5
End If
End Sub
You can use this code to move the PictureBox
using Arrow Keys.
The OP is already overridingProcessCmdKey
, which is the right thing to do (one of) in this scenario, since it will allows a correct processing of the Keys when automatic repetition is an option.
– Jimi
Nov 13 '18 at 7:54
And I also stated that I can't use a KeyPress event since I am already using it elsewhere. Unless...
– Mgfranz
Nov 13 '18 at 14:20
@Mgfranz : For the record you can attach multiple event handlers to a single event. In VB.NET you either useAddHandler
or (in cases like this) just create another method and make sure it hasHandles Me.KeyDown
on it. The actual name of the method isn't important. You could name itForm1_KeyDown_2
or just as wellMyHappyBanana
if you'd like.
– Visual Vincent
Nov 13 '18 at 17:37
@Vincent, that is exactly what I was thinking. Just making another event handler to handle KeyDown. Thanks.
– Mgfranz
Nov 13 '18 at 21:48
add a comment |
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
KeyPreview = True
End Sub
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Up Then
PictureBox1.Top -= 5
End If
If e.KeyCode = Keys.Down Then
PictureBox1.Top += 5
End If
If e.KeyCode = Keys.Left Then
PictureBox1.Left -= 5
End If
If e.KeyCode = Keys.Right Then
PictureBox1.Left += 5
End If
End Sub
You can use this code to move the PictureBox
using Arrow Keys.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
KeyPreview = True
End Sub
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Up Then
PictureBox1.Top -= 5
End If
If e.KeyCode = Keys.Down Then
PictureBox1.Top += 5
End If
If e.KeyCode = Keys.Left Then
PictureBox1.Left -= 5
End If
If e.KeyCode = Keys.Right Then
PictureBox1.Left += 5
End If
End Sub
You can use this code to move the PictureBox
using Arrow Keys.
answered Nov 13 '18 at 6:07
Richard BaluyutRichard Baluyut
177
177
The OP is already overridingProcessCmdKey
, which is the right thing to do (one of) in this scenario, since it will allows a correct processing of the Keys when automatic repetition is an option.
– Jimi
Nov 13 '18 at 7:54
And I also stated that I can't use a KeyPress event since I am already using it elsewhere. Unless...
– Mgfranz
Nov 13 '18 at 14:20
@Mgfranz : For the record you can attach multiple event handlers to a single event. In VB.NET you either useAddHandler
or (in cases like this) just create another method and make sure it hasHandles Me.KeyDown
on it. The actual name of the method isn't important. You could name itForm1_KeyDown_2
or just as wellMyHappyBanana
if you'd like.
– Visual Vincent
Nov 13 '18 at 17:37
@Vincent, that is exactly what I was thinking. Just making another event handler to handle KeyDown. Thanks.
– Mgfranz
Nov 13 '18 at 21:48
add a comment |
The OP is already overridingProcessCmdKey
, which is the right thing to do (one of) in this scenario, since it will allows a correct processing of the Keys when automatic repetition is an option.
– Jimi
Nov 13 '18 at 7:54
And I also stated that I can't use a KeyPress event since I am already using it elsewhere. Unless...
– Mgfranz
Nov 13 '18 at 14:20
@Mgfranz : For the record you can attach multiple event handlers to a single event. In VB.NET you either useAddHandler
or (in cases like this) just create another method and make sure it hasHandles Me.KeyDown
on it. The actual name of the method isn't important. You could name itForm1_KeyDown_2
or just as wellMyHappyBanana
if you'd like.
– Visual Vincent
Nov 13 '18 at 17:37
@Vincent, that is exactly what I was thinking. Just making another event handler to handle KeyDown. Thanks.
– Mgfranz
Nov 13 '18 at 21:48
The OP is already overriding
ProcessCmdKey
, which is the right thing to do (one of) in this scenario, since it will allows a correct processing of the Keys when automatic repetition is an option.– Jimi
Nov 13 '18 at 7:54
The OP is already overriding
ProcessCmdKey
, which is the right thing to do (one of) in this scenario, since it will allows a correct processing of the Keys when automatic repetition is an option.– Jimi
Nov 13 '18 at 7:54
And I also stated that I can't use a KeyPress event since I am already using it elsewhere. Unless...
– Mgfranz
Nov 13 '18 at 14:20
And I also stated that I can't use a KeyPress event since I am already using it elsewhere. Unless...
– Mgfranz
Nov 13 '18 at 14:20
@Mgfranz : For the record you can attach multiple event handlers to a single event. In VB.NET you either use
AddHandler
or (in cases like this) just create another method and make sure it has Handles Me.KeyDown
on it. The actual name of the method isn't important. You could name it Form1_KeyDown_2
or just as well MyHappyBanana
if you'd like.– Visual Vincent
Nov 13 '18 at 17:37
@Mgfranz : For the record you can attach multiple event handlers to a single event. In VB.NET you either use
AddHandler
or (in cases like this) just create another method and make sure it has Handles Me.KeyDown
on it. The actual name of the method isn't important. You could name it Form1_KeyDown_2
or just as well MyHappyBanana
if you'd like.– Visual Vincent
Nov 13 '18 at 17:37
@Vincent, that is exactly what I was thinking. Just making another event handler to handle KeyDown. Thanks.
– Mgfranz
Nov 13 '18 at 21:48
@Vincent, that is exactly what I was thinking. Just making another event handler to handle KeyDown. Thanks.
– Mgfranz
Nov 13 '18 at 21:48
add a comment |
Here is what I ended up using. The Mouse just much better sense, plus I get to store it to Settings along with the other settings. I think this is a good solution without getting into any DB work. Opinions?
Private Sub CheckForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'...
pbSig.Location = My.Settings.pbSigLoc
'Allow an image to be dropped
pbSig.AllowDrop = True
End Sub
End Sub
' The next three subs control the moving of the pbSig location using the mouse
Dim startX As Integer
Dim startY As Integer
Dim endX As Integer
Dim endY As Integer
Dim mDown As Boolean
Dim valX As Boolean
Dim valY As Boolean
Private Sub pbSig_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles pbSig.MouseDown
startX = MousePosition.X
startY = MousePosition.Y
mDown = True
valX = False
valY = False
End Sub
Private Sub Main_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
End Sub
Private Sub pbSig_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles pbSig.MouseMove
'Check if mouse=down
If mDown = True Then
endX = (MousePosition.X - Me.Left)
endY = (MousePosition.Y - Me.Top)
If valY = False Then
startY = endY - sender.top
valY = True
End If
If valX = False Then
startX = endX - sender.left
valX = True
End If
sender.left = endX - startX
sender.top = endY - startY
End If
End Sub
'If mouseUp=True then End and Save to Settings
Private Sub pbSig_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles pbSig.MouseUp
My.Settings.pbSigLoc = pbSig.Location
mDown = False
valX = False
valY = False
End Sub
This way all the user needs to do is use their mouse to locate the pB and it's contents and I don't need to call the ProcessCmdKey again. And I still have the arrow keys functionality on the Controls I need it to be.
add a comment |
Here is what I ended up using. The Mouse just much better sense, plus I get to store it to Settings along with the other settings. I think this is a good solution without getting into any DB work. Opinions?
Private Sub CheckForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'...
pbSig.Location = My.Settings.pbSigLoc
'Allow an image to be dropped
pbSig.AllowDrop = True
End Sub
End Sub
' The next three subs control the moving of the pbSig location using the mouse
Dim startX As Integer
Dim startY As Integer
Dim endX As Integer
Dim endY As Integer
Dim mDown As Boolean
Dim valX As Boolean
Dim valY As Boolean
Private Sub pbSig_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles pbSig.MouseDown
startX = MousePosition.X
startY = MousePosition.Y
mDown = True
valX = False
valY = False
End Sub
Private Sub Main_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
End Sub
Private Sub pbSig_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles pbSig.MouseMove
'Check if mouse=down
If mDown = True Then
endX = (MousePosition.X - Me.Left)
endY = (MousePosition.Y - Me.Top)
If valY = False Then
startY = endY - sender.top
valY = True
End If
If valX = False Then
startX = endX - sender.left
valX = True
End If
sender.left = endX - startX
sender.top = endY - startY
End If
End Sub
'If mouseUp=True then End and Save to Settings
Private Sub pbSig_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles pbSig.MouseUp
My.Settings.pbSigLoc = pbSig.Location
mDown = False
valX = False
valY = False
End Sub
This way all the user needs to do is use their mouse to locate the pB and it's contents and I don't need to call the ProcessCmdKey again. And I still have the arrow keys functionality on the Controls I need it to be.
add a comment |
Here is what I ended up using. The Mouse just much better sense, plus I get to store it to Settings along with the other settings. I think this is a good solution without getting into any DB work. Opinions?
Private Sub CheckForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'...
pbSig.Location = My.Settings.pbSigLoc
'Allow an image to be dropped
pbSig.AllowDrop = True
End Sub
End Sub
' The next three subs control the moving of the pbSig location using the mouse
Dim startX As Integer
Dim startY As Integer
Dim endX As Integer
Dim endY As Integer
Dim mDown As Boolean
Dim valX As Boolean
Dim valY As Boolean
Private Sub pbSig_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles pbSig.MouseDown
startX = MousePosition.X
startY = MousePosition.Y
mDown = True
valX = False
valY = False
End Sub
Private Sub Main_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
End Sub
Private Sub pbSig_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles pbSig.MouseMove
'Check if mouse=down
If mDown = True Then
endX = (MousePosition.X - Me.Left)
endY = (MousePosition.Y - Me.Top)
If valY = False Then
startY = endY - sender.top
valY = True
End If
If valX = False Then
startX = endX - sender.left
valX = True
End If
sender.left = endX - startX
sender.top = endY - startY
End If
End Sub
'If mouseUp=True then End and Save to Settings
Private Sub pbSig_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles pbSig.MouseUp
My.Settings.pbSigLoc = pbSig.Location
mDown = False
valX = False
valY = False
End Sub
This way all the user needs to do is use their mouse to locate the pB and it's contents and I don't need to call the ProcessCmdKey again. And I still have the arrow keys functionality on the Controls I need it to be.
Here is what I ended up using. The Mouse just much better sense, plus I get to store it to Settings along with the other settings. I think this is a good solution without getting into any DB work. Opinions?
Private Sub CheckForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'...
pbSig.Location = My.Settings.pbSigLoc
'Allow an image to be dropped
pbSig.AllowDrop = True
End Sub
End Sub
' The next three subs control the moving of the pbSig location using the mouse
Dim startX As Integer
Dim startY As Integer
Dim endX As Integer
Dim endY As Integer
Dim mDown As Boolean
Dim valX As Boolean
Dim valY As Boolean
Private Sub pbSig_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles pbSig.MouseDown
startX = MousePosition.X
startY = MousePosition.Y
mDown = True
valX = False
valY = False
End Sub
Private Sub Main_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
End Sub
Private Sub pbSig_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles pbSig.MouseMove
'Check if mouse=down
If mDown = True Then
endX = (MousePosition.X - Me.Left)
endY = (MousePosition.Y - Me.Top)
If valY = False Then
startY = endY - sender.top
valY = True
End If
If valX = False Then
startX = endX - sender.left
valX = True
End If
sender.left = endX - startX
sender.top = endY - startY
End If
End Sub
'If mouseUp=True then End and Save to Settings
Private Sub pbSig_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles pbSig.MouseUp
My.Settings.pbSigLoc = pbSig.Location
mDown = False
valX = False
valY = False
End Sub
This way all the user needs to do is use their mouse to locate the pB and it's contents and I don't need to call the ProcessCmdKey again. And I still have the arrow keys functionality on the Controls I need it to be.
answered Nov 14 '18 at 2:00
MgfranzMgfranz
227
227
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53274172%2fmove-picturebox-with-arrow-keys%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
The code you have is working correctly. You have just one problem: a PictureBox can't be the active control (not using the default class style, it needs
ControlStyles.Selectable
). If you changeaControl.Location = (...)
with the name of a PictureBox (e.g.,pbSig.Location = (...)
, then the PictureBox will move and will accept a drop (provided you haveset pbSig.AllowDrop = True
somewhere), setting the new image. You have a typo inpbSig_DragDrop
=>pbSig.ImageLocation = files(0)
should bepicbox.ImageLocation = files(0)
.– Jimi
Nov 13 '18 at 7:42