The PictureBox control (red) is located on the right panel of SplitContainer on vb.net.I want to move only this picture box using the keyboard, but every time I press the keyboard button, the SplitContainer border also moves.
How do I move only PictureBox without moving the boundaries?
Development Environment Visual Studio community 2017
.NetFramework 4.5.2
Code
Public Class Form 1
Private SubForm1_KeyDown (sender As Object, eAs KeyEventArgs) Handles MyBase.KeyDown
If.KeyCode=Keys.Up Then
Me.PictureBox1.Location=New Point (Me.PictureBox1.Location.X, Me.PictureBox1.Location.Y-1)
ElseIf.KeyCode=Keys.Right Then
Me.PictureBox1.Location=New Point (Me.PictureBox1.Location.X+1, Me.PictureBox1.Location.Y)
ElseIf.KeyCode=Keys.Down Then
Me.PictureBox1.Location=New Point (Me.PictureBox1.Location.X, Me.PictureBox1.Location.Y+1)
ElseIf.KeyCode=Keys.Left Then
Me.PictureBox1.Location=New Point (Me.PictureBox1.Location.X-1, Me.PictureBox1.Location.Y)
End If
End Sub
Private SubForm1_Load (sender As Object, eAs EventArgs) Handles MyBase.Load
'Me.SplitContainer1.FixedPanel=FixedPanel.Panel1'SplitContainer1 border moves when uncommented
End Sub
End Class
-----Additional ----
Based on Kunif's answer, I was able to move only PictureBox with the boundaries fixed.
The point is
1.Set the IsSplitterFixed property of SplitContainer1 control to True
2. Write the processing code of the keyboard written in Form1_KeyDown function to SplitContainer1_KeyDown function
Public Class Form 1
Private SubForm1_Load (sender As Object, eAs EventArgs) Handles MyBase.Load
Me.SplitContainer 1.IsSplitterFixed=True
End Sub
Private Sub SplitContainer 1_KeyDown (sender As Object, eAs KeyEventArgs) Handles SplitContainer 1.KeyDown
If.KeyCode=Keys.Up Then
Me.PictureBox1.Location=New Point (Me.PictureBox1.Location.X, Me.PictureBox1.Location.Y-1)
ElseIf.KeyCode=Keys.Right Then
Me.PictureBox1.Location=New Point (Me.PictureBox1.Location.X+1, Me.PictureBox1.Location.Y)
ElseIf.KeyCode=Keys.Down Then
Me.PictureBox1.Location=New Point (Me.PictureBox1.Location.X, Me.PictureBox1.Location.Y+1)
ElseIf.KeyCode=Keys.Left Then
Me.PictureBox1.Location=New Point (Me.PictureBox1.Location.X-1, Me.PictureBox1.Location.Y)
End If
End Sub
End Class
Thank you.
vb.net
This article is C#, but it will be helpful.
[C#]How to secure SplitContainer Panel
Also, set the IsSplitterFixed property to True to prevent the panel from being resized by mouse operation.
Setting the FixedPanel properties in the above article did not work.
Simply set the IsSplitterFixed property to True.
add
The KeyPreview property in Form 1 had to be true to work with the KeyDown event in Form 1 at the beginning of the question.
Whether you want SplitContainer 1 KeyDown event or not is up to you.
On the contrary, this is an article about a question that I couldn't move by dividing it into four parts.
I hope you'll find something useful in the future.
Resize control up, down, left, and right on SplitContainer
916 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
574 Who developed the "avformat-59.dll" that comes with FFmpeg?
619 Uncaught (inpromise) Error on Electron: An object could not be cloned
613 GDB gets version error when attempting to debug with the Presense SDK (IDE)
© 2024 OneMinuteCode. All rights reserved.