TabControl shortcut key in vb.net - vb.net

I have a tab control on my form. I would like to add a shortcut key for each page, so it could be used without a mouse or multiple tab key presses.
I can't find any property in controltabs properties to do this. I have tried to use the &-sign in the Text property of tabPage, but it doesn't work.

You can use tabControl1.SelectTab(i) in the KeyDown event of the TabControl to move from one page to another. I'm not sure how to get an underscore in the tabpage label.
You can also use a command box on each tab page to go from one page to the next.

Add code like this to your Form, then modify accordingly:
Public Class Form1
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean
Select Case keyData
Case Keys.F1
TabControl1.SelectedIndex = 0
Return True
Case Keys.F2
TabControl1.SelectedIndex = 1
Return True
Case Keys.F3
TabControl1.SelectedIndex = 2
Return True
End Select
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
End Class

Related

how to set Tab index for tab page windows form

I have a Windows form, inside there is a TabControl with 2 TabPages with TextBoxes. I need navigate all the fields using the Tab key(filling the first TabPage and then to the second).
I have set TabIndex for each TextBox in the TabPages, the issue lies in that while TabControl has the TabIndex property, TabPages dont have that property so i end up having selected the TabControl from the start(giving me the option the select 1 of the TabPages), and then going into that tab, but after finishing that tab it leaves the TabControl(leaving the TabPage i didnt select untouched)
There isn't a clean way to do this, but you can try using the last TextBox in the first TabPage to switch tabs:
Private Sub TextBox2_Leave(sender As Object, e As EventArgs) Handles TextBox2.Leave
TabControl1.SelectedTab = TabPage2
End Sub
The other way would be by overriding the form's ProcessCmdKey:
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean
If keyData = Keys.Tab AndAlso TextBox2.Focused Then
TabControl1.SelectedTab = TabPage2
Return True
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function

Use Left/Right Arrow Keys on Button Click event VS2008

I am trying to use the left and right arrow keys to do performclick on my back and next buttons. Here's the code:
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
If keyData = Keys.Left Then
Me.cmdPrevQ.PerformClick()
Return True
End If
If keyData = Keys.Right Then
Me.cmdNextQ.PerformClick()
Return True
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
I want this function to only work as soon as I click on a listview item and not on formload but I don't know how to do that :(
I tried the KeyDown event for my listview but what happens is even if I haven't clicked on anything in my listview and I press on the left or right arrow keys, it gives me an error as my next and back buttons rely on the items inside my listview.
Please help

How to embed a Console in a Windows Form application?

I'm trying to build a Text Adventure game in VB.net, just like the days of old. The obvious choice would be a Console application, however, I have decided on a Windows Form because I am hoping to include interactive buttons and pictures. Currently, I have already got on my form a picture box and a Rich Text Box. I was hoping that with the Rich Text Box I could achieve something that worked in the same way as a console. Alas, my efforts are futile. Everything I have tried has failed, including: reading Rich_Text_Box.Text and Rich_Text_Box_KeyUp with an if statement for enter being pressed to call the procedure for an enter button.
I was wondering if there was any way to include a Console with standard Console.WriteLine and Console.ReadLine capabilities inside of my Form? This would very much shorten my task and streamline the whole process.
Any ideas?
You could use not one but two Textboxes for your purpose. tbOutput and tbInput. tbOutput would be Multiline and ReadOnly whereas tbInput would be single line, not readonly and placed beneath tbOutput. Then to process inputs you could do something like:
Private Sub Output(s As String)
If s <> "" Then
tbOutput.AppendText(vbCrLf & ">> " & s)
End If
End Sub
Private Sub tbInput_KeyDown(sender As Object, e As KeyEventArgs) Handles tbInput.KeyDown
If e.KeyCode = Keys.Enter Then
If tbInput.Text <> "" Then
Output(tbInput.Text)
' Handle input value
tbInput.Text = ""
End If
End If
End Sub
At the 'Handle input value you would check the user input and handle it according to your needs. Use Lucida Console font in bold in gray and black background for style :-)
Sure, a RichTextBox can be used to emulate a console. Some surgery is required to avoid the user from making it malfunction as a console. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form. Subscribe the InputChanged event to detect when the user presses the Enter key, the Input property gives you the typed text. Use the Write() or WriteLine() methods to add text.
Imports System.Windows.Forms
Public Class RichConsole
Inherits RichTextBox
Public Event InputChanged As EventHandler
Public ReadOnly Property Input() As String
Get
Return Me.Text.Substring(InputStart).Replace(vbLf, "")
End Get
End Property
Public Sub Write(txt As String)
Me.AppendText(txt)
InputStart = Me.SelectionStart
End Sub
Public Sub WriteLine(txt As String)
Write(txt & vbLf)
End Sub
Private InputStart As Integer
Protected Overrides Function ProcessCmdKey(ByRef m As Message, keyData As Keys) As Boolean
'' Defeat backspace
If (keyData = Keys.Back OrElse keyData = Keys.Left) AndAlso InputStart = Me.SelectionStart Then Return True
'' Defeat up/down cursor keys
If keyData = Keys.Up OrElse keyData = Keys.Down Then Return True
'' Detect Enter key
If keyData = Keys.[Return] Then
Me.AppendText(vbLf)
RaiseEvent InputChanged(Me, EventArgs.Empty)
InputStart = Me.SelectionStart
Return True
End If
Return MyBase.ProcessCmdKey(m, keyData)
End Function
Protected Overrides Sub WndProc(ByRef m As Message)
'' Defeat the mouse
If m.Msg >= &H200 AndAlso m.Msg <= &H209 Then Return
MyBase.WndProc(m)
End Sub
End Class

Using combobox from class

I have some questions on using of combobox.
1) I need to reference combobox from class like this:
If Me.ActiveControl.GetType Is GetType(ComboBox) And combodroppeddown = False) Then
something...
End If
From Here I need right from the AND to check if this combobox is dropped down but I don't know how.
2) My actual type of combobox is of "DropDownList" type.
Problem is that if I drop it down and type with up/down keys the value is changed according to selected row. If I then press ESC then last value stays as choosen what is not wanted.
Is here way to return original value from moment of dropping in case if I press ESC when list is dropped?
How to do that?
Here is closer look to my xCombo subclass to get help in second question...
Public Class xAutoCombo
Inherits ComboBox
Private entertext As String
Protected Overrides Sub OnKeyDown(ByVal e As KeyEventArgs)
If e.Control Then ' this dropped a list with keyboard
If e.KeyCode = Keys.Down Then
Me.DroppedDown = True
End If
End If
'' this should return value back if ESC was pressed
'' but don't work!
If Me.DroppedDown And e.KeyCode = Keys.Escape Then
Me.Text = entertext
End If
MyBase.OnKeyDown(e)
End Sub
Protected Overrides Sub OnDropDown(ByVal e As System.EventArgs)
entertext = Me.Text '' this remember text in moment of droping
MyBase.OnDropDown(e)
End Sub
EDIT:
Here I found one issue in functionality which I like to be solved.
When combo is dropped and I navigate through list by keyboard and then press with mouse to form (or outside of combo) it closes a list and set value which is last been selected.
Instead of that I would like that combo set his new value ONLY on click with mouse to list or with pressing Enter key with keyboard.
Examine the DroppedDown property, but it seemed like you have other things you wanted to do while dropped down.
Dim cbo As ComboBox
If Me.ActiveControl.GetType Is GetType(ComboBox) then
cbo=Ctype(Me.ActiveControl, ComboBox)
' make it easier to refernece
if cbo.DroppedDOwn then
....
End iF
End if
' Note:
Ctype(Me.ActiveControl, ComboBox).DroppedDown
' should work, but the above is easier to read and use since you apparently
' will have other things to do/override with it
Note also that I think one of the three combobox dropdown types does not use/support the DroppedDown property.
For the rest of your question, which I dont entirely follow, you could also store the last selected item and restore it in similar fashion. Overriding windows default behavior though, is rarely a good idea because you are creating something the user has never encountered before.
EDIT
To change Escape functionality from CLOSE DROPDOWN to ABORT CHOICE:
NOTE: I just used a std cbo, refs will need to be changed to MyBase, events to On...
Private selectedindex As Integer = -1
Private bEsc As Boolean = False
Private Sub cbo_Enter(....
' reset tracking vars...might not be needed
selectedindex = -1
bEsc = False
End Sub
Private Sub cbo_DropDown(...
' capture starting state
selectedindex = cbo.SelectedIndex
bEsc = False
End Sub
KeyDown:
If cbo.DroppedDown AndAlso e.KeyCode = Keys.Escape Then
bEsc = True
e.Handled = True
' this MUST be last!
cbo.DroppedDown = False
End If
Private Sub cbo_DropDownClosed(...
' rest cbo.selectedindex if escape was pressed
If bEsc Then
' note: SelectedIndexChanged event will fire and any code there will run
' may need to qualify with If Esc = False...
cbo.SelectedIndex = selectedindex
End If
End Sub

Focus on toolstrip button vb.net

I created a toolstrip with some buttons. I have a form with some objects (like textbox,checkbox,button etc.) and when i get the focus on last item,when i finish to compile i need with tab on keyboard to get focus on save button on the toolstrip.
Can anyone tell me how to do that? Because there isn't the focus option on toolstrip.
Thanks.
You'll need to override the Tab key handling. Say your last control is Button3 and the toolstrip button is SaveButton then the code looks like this:
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
If keyData = Keys.Tab AndAlso Me.ActiveControl Is Button3 Then
ToolStrip1.Focus()
SaveButton.Select()
Return True
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
Adjust as necessary to work with your specific controls.