I want to capture the keydown event in a rich text box in VB .Net.
According to the documentation and examples I have seen I just need to use e.Handled = True but this isn't working, I still get the key value placed in the rich text box.
My code is
Private Sub Tagging_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles rtxReference.KeyDown
If ((e.KeyCode = Keys.Z) AndAlso e.Control) Then
'MsgBox("Undo")
rtxReference.Text = sPrevious
Else
If cbxHotKeyTags.Checked Then
If e.KeyCode = Keys.T Then
sTagName = "article-title"
ElseIf e.KeyCode = Keys.V Then
sTagName = "volume"
ElseIf e.KeyCode = Keys.F Then
sTagName = "fpage"
ElseIf e.KeyCode = Keys.L Then
sTagName = "lpage"
ElseIf e.KeyCode = Keys.I Then
sTagName = "issuee"
End If
e.Handled = True
End If
End If
End Sub
Related
I am attempting to create a simple platforming game in Visual Basic as a project for school. Although I have figured out how to move the picture up, down, left, and right using the WASD keys, I am unable to make the PictureBox move diagonally by holding two keys. For example, the PictureBox should move diagonally to the top right when the user holds down the W and D keys. In short, how can I move the PictureBox diagonally when the user holds two keys?
Below is the code so far.
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If DPressed = True Then
PictureBox1.Left = PictureBox1.Left + 9
ElseIf DPressed = True AndAlso WPressed = True Then
PictureBox1.Left = PictureBox1.Left + 9
PictureBox1.Top = PictureBox1.Top - 9
ElseIf APressed = True Then
PictureBox1.Left = PictureBox1.Left - 9
ElseIf SPressed = True Then
PictureBox1.Top = PictureBox1.Top + 9
ElseIf WPressed = True Then
PictureBox1.Top = PictureBox1.Top - 9
End If
End Sub
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.D Then
DPressed = True
ElseIf e.KeyCode = Keys.W Then
WPressed = True
ElseIf e.KeyCode = Keys.A Then
APressed = True
ElseIf e.KeyCode = Keys.S Then
SPressed = True
ElseIf (e.KeyCode And Not Keys.Modifiers) = Keys.D AndAlso e.Modifiers = Keys.W Then
DPressed = True
WPressed = True
End If
End Sub
Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles MyBase.KeyUp
If e.KeyCode = Keys.D Then
DPressed = False
WPressed = False
ElseIf e.KeyCode = Keys.W Then
WPressed = False
ElseIf e.KeyCode = Keys.A Then
APressed = False
ElseIf e.KeyCode = Keys.S Then
SPressed = False
End If
End Sub
E.g.
Private wDown As Boolean = False
Private aDown As Boolean = False
Private sDown As Boolean = False
Private dDown As Boolean = False
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
Select Case e.KeyCode
Case Keys.W
wDown = True
Case Keys.A
aDown = True
Case Keys.S
sDown = True
Case Keys.D
dDown = True
End Select
End Sub
Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
Select Case e.KeyCode
Case Keys.W
wDown = False
Case Keys.A
aDown = False
Case Keys.S
sDown = False
Case Keys.D
dDown = False
End Select
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim xIncrement = 0
Dim yIncrement = 0
If wDown Then
yIncrement -= 9
End If
If aDown Then
xIncrement -= 9
End If
If sDown Then
yIncrement += 9
End If
If dDown Then
xIncrement += 9
End If
PictureBox1.Location += New Size(xIncrement, yIncrement)
End Sub
Maybe try marking the DPressed, WPressed, APressed variables as Shared.
I was wondering if there is a way to allow only some sections of a textbox to be editable in a frozen sentence.
e.g:
He share of ________ to worse. Weddings and any opinions suitable smallest nay.
My he houses or months settle remove ladies appear. Engrossed suffering
supposing he recommend do eagerness.
where the underscore can be edited but the sentences cannot be changed at all.
Thanks
A simple solution for your example
Public Class Form1
Private SelectionStart As Integer = 10
Private SelectionEnd As Integer = 20
Private SelectionCurrent As Integer = 10
Private Sub CursorControl(Optional ByVal e As Object = Nothing)
If SelectionCurrent > SelectionEnd Then
SelectionCurrent = SelectionEnd
ElseIf SelectionCurrent < SelectionStart Then
SelectionCurrent = SelectionStart
End If
If TextBox1.SelectionStart >= SelectionEnd Then
If Not e Is Nothing Then
e.Handled = True
e = Nothing
End If
TextBox1.SelectionStart = SelectionEnd
TextBox1.SelectionLength = 0
Me.SelectionCurrent = TextBox1.SelectionStart
ElseIf TextBox1.SelectionStart <= SelectionStart Then
If Not e Is Nothing Then
e.Handled = True
e = Nothing
End If
TextBox1.SelectionStart = SelectionStart
TextBox1.SelectionLength = 0
Me.SelectionCurrent = TextBox1.SelectionStart
Else
Me.SelectionCurrent = TextBox1.SelectionStart
End If
End Sub
Private Sub TextBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Click
CursorControl()
End Sub
Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus
CursorControl()
End Sub
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
TextBox1.SelectionStart = Me.SelectionCurrent
TextBox1.SelectionLength = 0
If e.KeyCode = Keys.Right Or e.KeyCode = Keys.Left Or e.KeyCode = Keys.Down Or e.KeyCode = Keys.Up Then
e.Handled = True
e = Nothing
End If
CursorControl(e)
End Sub
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
TextBox1.SelectionStart = Me.SelectionCurrent
TextBox1.SelectionLength = 0
If e.KeyChar = "_" Then
e.Handled = True
End If
If TextBox1.SelectionStart >= SelectionEnd Then
e.Handled = True
e = Nothing
ElseIf TextBox1.SelectionStart <= SelectionStart Then
e.Handled = True
e = Nothing
Else
TextBox1.Text = TextBox1.Text.Substring(0, TextBox1.SelectionStart) & e.KeyChar & TextBox1.Text.Substring(TextBox1.SelectionStart + 1, TextBox1.Text.Length - TextBox1.SelectionStart - 1)
TextBox1.SelectionStart = Me.SelectionCurrent
e.Handled = True
e = Nothing
End If
CursorControl()
End Sub
Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
TextBox1.SelectionStart = Me.SelectionCurrent
TextBox1.SelectionLength = 0
If e.KeyCode = 189 Then '_
e.Handled = True
End If
Me.SelectionCurrent = Me.SelectionCurrent + 1
Me.TextBox1.SelectionStart = Me.SelectionCurrent
CursorControl(e)
End Sub
End Class
Put three TextBoxes next to each other and allow only the middle one to be edited. Here's some XAML:
<StackPanel
Orientation="Horizontal">
<TextBox
Text="He share of"
IsReadOnly=True />
<TextBox
Text="<Enter your phrase here>" />
<TextBox
Text="to worse. ......."
IsReadOnly=True />
</StackPanel>
The end user will see the whole line as one unit.
The Winform control RichTextBox via the SelectionProtected Property allows you to mark text ranges a readonly (protected).
Marking the entire text as protected and then selectively unprotecting the desired editable range will produce the desired effect.
Public Class Form1
Private underlinedFont As Font
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
RichTextBox1.Text = "This is protected. This is unprotected."
' Protect all text
RichTextBox1.SelectAll()
RichTextBox1.SelectionProtected = True
' Unprotect the word "unprotected"
Dim startUnProtected As Int32 = RichTextBox1.Find("unprotected", RichTextBoxFinds.WholeWord)
If startUnProtected > -1 Then
RichTextBox1.SelectionProtected = False
If underlinedFont Is Nothing Then underlinedFont = New Font(RichTextBox1.Font.FontFamily, RichTextBox1.Font.Size, FontStyle.Underline, RichTextBox1.Font.Unit)
RichTextBox1.SelectionFont = underlinedFont
End If
End Sub
End Class
I'm trying to use hotkeys on a form to hide/show a textbox, tried many ways and after reading this thread, I did this:
If Control.ModifierKeys = Keys.B Then
If TextBox1.Visible = True Then
TextBox1.Visible = False
Else
TextBox1.Visible = True
End If
and:
If Control.ModifierKeys = Keys.B Then
If TextBox1.Visible = True Then
TextBox1.Hide()
Else
TextBox1.Hide()
End If
End If
but still not working.. help me, please
First you need to set the forms property
KeyPreview = True
Then use the forms KeyDown event
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.B Then TextBox1.Visible = Not TextBox1.Visible
End Sub
If you want to use an ALT or other combination you need to check the modifiers too
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If CBool(e.Modifiers And Keys.Alt) AndAlso e.KeyCode = Keys.B Then TextBox1.Visible = Not TextBox1.Visible
End Sub
If you want to do lots of them then use a select case statement
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
Select Case e.KeyCode
case Keys.B : TextBox1.Visible = Not TextBox1.Visible
'etc
End Select
End Sub
Now I want to get the new value what I put into my datagrid; however, I always get the old value if I keys Up or Down.
Here is my KeyDownEvent Function:
Private Sub grd_MyGridKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles grd.MyGridKeyDown
If e.KeyCode = Keys.Left Or e.KeyCode = Keys.Enter Or e.KeyCode = Keys.Right Or e.KeyCode = Keys.Up Or e.KeyCode = Keys.Down Then
Me.grd.Item(Me.grd.CurrentRowIndex, 4) = Me.grd.Item(Me.grd.CurrentRowIndex, 2) * Me.grd.Item(Me.grd.CurrentRowIndex, 3)
End If
End Sub
On your datagridview CellValidating Event
e.Cancel = False
I just found the solution in Japanese website, so I just post what I used to solve my problem.
Me.grd.Item(Me.grd.CurrentRowIndex, 3) = DirectCast(ColumnMoney, DataGridTextBoxColumn).TextBox.Text
grd is my datagrid, and this is the solution. Hope that this will help others who also meet this problem.
Ok here's my dilemma. here's this code I have:
If e.KeyCode = Keys.A Then
TextBox1.AppendText("C, ")
PictureBox2.Visible = True
My.Computer.Audio.Play(My.Resources.C, AudioPlayMode.Background)
End If
Now when I enter this under Form1_KeyDown, visual basic thinks this:
'KeyCode is not a a member of 'System.EventArgs'
Now I've seen this code work before, but it isn't here. Any help?
Here's the full code:
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
If e.KeyCode = Keys.A Then
TextBox1.AppendText("A, ")
PictureBox2.Visible = True
My.Computer.Audio.Play(My.Resources.C, AudioPlayMode.Background)
End If
If e.KeyCode = Keys.S Then
TextBox1.AppendText("C,")
PictureBox14.Visible = True
My.Computer.Audio.Play(My.Resources.D, AudioPlayMode.Background)
End If
End Sub
Not sure why you method definition declares e as EventArgs, but the fix is simply to make the parameter of type KeyEventArgs. This is because EventArgs (naturally) does not contain a property called KeyCode, but KeyEventArgs does!
Change your event handler method definition to the following:
Private Sub foo_KeyDown(sender As Object, e As KeyEventArgs)
If e.KeyCode = Keys.A Then
TextBox1.AppendText("A, ")
PictureBox2.Visible = True
My.Computer.Audio.Play(My.Resources.C, AudioPlayMode.Background)
ElseIf e.KeyCode = Keys.S Then
TextBox1.AppendText("C,")
PictureBox14.Visible = True
My.Computer.Audio.Play(My.Resources.D, AudioPlayMode.Background)
End If
End Sub
It sounds like your method is using the wrong EventArgs. The Control.KeyDown event sends it as a System.Windows.Forms.KeyEventArgs. So your code should read as
Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs)
// code here
End Sub
I've found sometimes you need to provide the full object type similar to the below:
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
MsgBox(e.KeyCode.ToString()) 'Message what the keycode
If (e.KeyCode = Keys.A) Then
MsgBox("e.KeyCode = Keys.A") 'Message that I've found the A
TextBox1.AppendText("C, ")
PictureBox2.Visible = True
My.Computer.Audio.Play(My.Resources.C, AudioPlayMode.Background)
End If
End Sub
Have you also tried testing this in a textbox on the form similar to the below (textbox in example is named TextBoxKeyTest)?
Private Sub TextBoxKeyTest_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBoxKeyTest.KeyDown
MsgBox(e.KeyCode.ToString())'Message what the keycode
If (e.KeyCode = Keys.A) Then
MsgBox("e.KeyCode = Keys.A")'Message that I've found the A
TextBox1.AppendText("C, ")
PictureBox2.Visible = True
My.Computer.Audio.Play(My.Resources.C, AudioPlayMode.Background)
End If
End Sub
This should work in VB 2010
Private Sub cmdWiden_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
If e.KeyCode = Keys.Up Then Me.Top = Me.Top - 5
If e.KeyCode = Keys.Down Then Me.Top = Me.Top + 5
If e.KeyCode = Keys.Left Then Me.Left = Me.Left - 5
If e.KeyCode = Keys.Right Then Me.Left = Me.Left + 5
End Sub