How do I auto-indent new lines using a Windows 8 RichEditBox? - vb.net

I have the following VB code to add a Tab character whenever the key is pressed within the RichEditBox, but I can't figure out how I can make it so when an end user presses the return key, the app will auto-indent so you wouldn't need to press the Tab key however many times to nest code correctly on each new line.
Any help is appreciated, thanks.
Current code for Tab insertion on KeyDown:
Private Sub TextBox_KeyDown(sender As Object, e As KeyRoutedEventArgs) Handles TextBox.KeyDown
If e.Key = Windows.System.VirtualKey.Tab Then
e.Handled = True
Dim SelectionText As String = ""
TextBox.Document.Selection.GetText(Windows.UI.Text.TextGetOptions.None, SelectionText)
TextBox.Document.Selection.TypeText(vbTab + SelectionText)
End If
End Sub

Related

Why won't the button hotkey show up with an underline in VB?

I just started a self-paced course in Visual Basic using Visual Studio. One of my assigned problems is to create a form with two buttons. When the form loads, Button1 is enabled and Button2 is disabled. When you click on Button1, Button1 is disabled and Button2 is enabled. When you then click on Button2, Button2 is disabled and Button1 is enabled.
I got that to work easily, so I decided to add an extra challenge to myself. The challenge is that I want to make the disabled button show the text "Disabled" and the enabled button show "Enabled" with the "E" underlined as the hotkey for the button. I set a string variable for the enabled button containing the string "&Enabled" to enable the "E" as the hotkey. The "E" works as the hotkey, but it does not display with an underline.
I have searched the web for a fix to this issue, but I've come up dry. I also tried resizing the buttons to see if the buttons were too small to display the underline. That did not work. I have scrutinized my code, but I really don't know the language well enough to understand why the "E" does not show up with an underline. I am submitting my code and asking for help. I want to understand why this doesn't work the way I expect it to work.
This is the VB.Net code that I wrote for the form using Visual Studio 2019.
Public Class frmEnabledProblem
Dim blnButton1Enabled As Boolean = True
Dim blnButton2Enabled As Boolean = False
Dim strButton1Enabled As String = "&Enabled"
Dim strButton1Disabled As String = "Disabled"
Dim strButton2Enabled As String = "&Enabled"
Dim strButton2Disabled As String = "Disabled"
Private Sub frmEnabledProblem_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.KeyPreview = True
btnButton2.Text = strButton2Disabled
btnButton2.Enabled = False
btnButton1.Text = strButton1Enabled
btnButton1.Enabled = True
End Sub
Private Sub btnButton1_Click(sender As Object, e As EventArgs) Handles btnButton1.Click
blnButton1Enabled = False
blnButton2Enabled = True
btnButton1.Enabled = blnButton1Enabled
btnButton2.Enabled = blnButton2Enabled
btnButton1.Text = strButton1Disabled
btnButton2.Text = strButton2Enabled
End Sub
Private Sub btnButton2_Click(sender As Object, e As EventArgs) Handles btnButton2.Click
blnButton1Enabled = True
blnButton2Enabled = False
btnButton1.Enabled = blnButton1Enabled
btnButton2.Enabled = blnButton2Enabled
btnButton1.Text = strButton1Enabled
btnButton2.Text = strButton2Disabled
End Sub
End Class
Thanks for your help.
In a nutshell, because this setting in Ease of Access/Keyboard is off on your computer:
Turn it on and restart your app
I wouldn't personally advocate going out of your way to force it on for your app even when it's off elsewhere in the system; just provide the &OK text on your button, and let the user decide if they want it to show like OK or O̲K depnding on their own settings/appetite for how they want their UI

Get Key Pressed in Variable - Visual Studio Basic 2017

I am trying to make a simple program, pasting in whatever you write in a textbox. I am doing this in Visual Studio 2017. It is a Windows-Forms App with Visual Basic. It works in the current state, but I woud like to add the ability to customize the key. Right now, when you press F12, it sends the message. I want a simple way of asking the user for a key, and after he presses it, it gets set as the new paste key. Any idea how I can accomplish this? Thanks in advance.
My code below, where I have Keys.F12 I have/want a variable that holds the key set by the user.
Dim hotkey As Boolean
hotkey = GetAsyncKeyState(Keys.F12)
If hotkey = True Then
SendKeys.Send(TextBox1.Text)
End If
I guess this is what you're looking for:
Private myNewKey As Char
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = Convert.ToChar(myNewKey) Then
e.Handled = True ' to prevent getting the Press Key to be entered
TextBox1.Text = Clipboard.GetText
End If
End Sub
Private Sub TextBox2_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox2.KeyPress
myNewKey = e.KeyChar
Label1.Text = "New paste key set to: " + e.KeyChar.ToString
End Sub
This program changes the pasting key each time user changes it by their own wish. Just simply declare a variable which holds the new specified key and when the user will get asked to enter a new press key, the variable should be assigned to it.
Eventually, when the user hits the same key, the TextBox1 KeyPress event will be triggered and the TextBox's text will be set by Clipboard.GetText.
Key set to 5 and when user hits that during focus on TextBox1 (second box), the text will get pasted.
If e.KeyCode = Keys.F12 Then
SendKeys.Send(TextBox1.Text)
End If

Hide a row in DataGridView

I am a new user to vb.net and need to hide a row when a user right clicks on a contextmenu and selects hide. I have googled this but have yet to find a way to do it.
At the moment, when a user clicks on an entry in the grid, the value is entered into a text box which is fine. What I need to do is hide the entry the user right clicked on and hide the selection. As I am new I am finding it hard going to code this as I have just finished my first course which entailed the basics. Any help would be appreciated or if you need anymore code, then please ask.
Dim value As Object = UserDataGridView.Rows(e.RowIndex).Cells(0).Value
txtCustomerActive.Text = CType(value, String)
Private Sub HideToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles pnlContextMenuStrip1.ItemClicked
'Get the text of the item that was clicked on.
'Dim text As String = txtCustomerActive.Text
Try
'txtCustomerActive.Visible = False
pnlContextMenuStrip1.Visible = False
MessageBox.Show(txtCustomerActive.Text)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
You could use Rows.Item() to hide specific DataGridViewRow, like:
If (UserDataGridView.Rows.Count > 0) Then
For Each row As DataGridViewRow In UserDataGridView.SelectedRows
UserDataGridView.Rows.Item(row.Index).Visible = False
Next
End If
I am assuming you are using FullRowSelect here.
If you are not using FullRowSelect you could have this alternative code which could catch both Cell being Selected or Row being Selected:
If (UserDataGridView.SelectedRows.Count > 0) Then
For Each row As DataGridViewRow In UserDataGridView.SelectedRows
UserDataGridView.Rows.Item(row.Index).Visible = False
Next
ElseIf (UserDataGridView.SelectedCells.Count > 0) Then
For Each cell As DataGridViewTextBoxCell In UserDataGridView.SelectedCells
UserDataGridView.Rows.Item(cell.RowIndex).Visible = False
Next
End If
To Unhide everything let's say from a Button Click you could have this:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
For Each row As DataGridViewRow In UserDataGridView.Rows
If (row.Visible = False) Then
UserDataGridView.Rows.Item(row.Index).Visible = True
End If
Next
End Sub
As far as I know, you can not make a server-side handler for right mouse click (as you did for HideToolStripMenuItem_Click, which works as part of .NET postback mechanism).
However, I believe that such feature could be done with some client-side javascript progamming.
Hope this helps!

DataGridView - ReadOnly Cell KeyDown Event

i have a datagridview with a readonly cell, i would like to show a formdialog window when the user press the space key. but is not possible since the cell is readonly=true.
i'v been using the following code with the EditingControlShowing event. and when the cell is readonly=false it works sometimes.
Private Sub sub_fecha_keydown(ByVal sender As Object, ByVal e As KeyEventArgs)
If e.KeyCode = Keys.Space Then
Dim frm As New frmFecha
frm.fecha_inicial = Me.m_dtp_id_fecha.Fecha
Dim res As DialogResult = frm.ShowDialog()
If res = Windows.Forms.DialogResult.OK Then
Me.m_dgv_detalle.Rows(Me.m_dgv_detalle.CurrentRow.Index).Cells("m_dgv_dtm_documento").Value = frm.fecha_format
Else
Me.m_dgv_detalle.Rows(Me.m_dgv_detalle.CurrentRow.Index).Cells("m_dgv_dtm_documento").Value = ""
End If
End If
End Sub
i would like to keep the cell readonly=true.
is there any other way to do it?
thanks very much for your time and help.
Rather than trying to intercept a keystroke for a readonly cell, which may not be possible, why not add a button column next to the field and when it is pressed, perform your actions.
This way you don't have to worry about whether the cell is readonly or not and it will be easier for your users to understand how the form is to be accessed.
Here is a link to the MSDN documentation on the DataGridViewButtonColumn.

keep track of richtext box text?

I'm new to the VB applications.My basic background is C.I just installed VB and learning stuff from google and microsoft help center. and I'm having fun with the things I'm doing but I got stucked up at one point and thats at richtext box.Is there any way to keep track of Rich textbox text in VB? So that I can append some text when user hits new line (i.e enter ) and do some task when he hits backspace. How do i keep track of richtextbox.?
I found
stringer = RichTextBox1.Lines(0) to read lines
& vbNewLine for new line
How do i read that user hit the new line character or backspace in vb rich textbox? because as far in C i used to do like these
if a = 13; \\ ascii for new line and 8 for backspace
I just want to do some task when user hits new line but I am unable to figure it out what the condition to be made.and Any good links for vb and documents on VB or on its windows application would be appreciated Too. Thank you in advance
You will want to link into the KeyDown event for the RichTextBox. Within this event you can modify the Text of the current line. Sample code for adding text to a line is:
Private Sub RichTextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles RichTextBox1.KeyDown
Dim textToAdd As String = " ** text to add **"
'Fire when the Enter key is pressed
If e.KeyCode = Keys.Enter Then
'Get the current cursor position
Dim cursorPos As Integer = RichTextBox1.SelectionStart
'Get the current line index
Dim index As Integer = RichTextBox1.GetLineFromCharIndex(cursorPos)
'Load all the lines into a string array
'This has to be done since editing via RichTextBox1.Lines(index) = "" doesn't always work
Dim lines() As String = RichTextBox1.Lines
'Add the text to the correct line
lines(index) &= textToAdd
'Assign the text back to the RichTextBox
RichTextBox1.Lines = lines
'Move the cursor to the correct position
RichTextBox1.SelectionStart = cursorPos + textToAdd.Length
End If
End Sub
All you need to do is check if the Enter or BackSpace key was pressed, like this:
Private Sub RichTextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles RichTextBox1.KeyDown
Select Case e.KeyCode
Case Keys.Enter
'Do stuff when Enter was pressed
Case Keys.Back
'Do stuff when BackSpace was pressed
End Select
End Sub
Note that Select Case is the same as the switch in C.