Row and ColumnHeader, CellMouseClick event vb.net - sql

I have code on vb.net which is checking radiobutton3 whenever a cell has clicked. But it also works when columnheader or rowheader clicked. How i can make this code inaccesable for columheader and rowheader click?
Private Sub MyDataGridView1_CellMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles MyDataGridView1.CellMouseClick
RadioButton3.Checked = True
End Sub

Private Sub MyDataGridView1_MouseDown(sender As Object, e As MouseEventArgs) Handles MyDataGridView1.MouseDown
Dim ht As DataGridView.HitTestInfo
ht = Me.MyDataGridView1.HitTest(e.X, e.Y)
If ht.Type = DataGridViewHitTestType.Cell Then
RadioButton3.Checked = True
cont()
End If
End Sub
This link helped:
context menu for datagridview cell, rowheader and columnheader

e.RowIndex=-1 represents the row header.
e.ColumnIndex=-1 represents the column header.
You can check as following:
Private Sub shipmentDetailsDataGridView_CellMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs)
RadioButton3.Checked = e.ColumnIndex >= 0 AndAlso e.RowIndex >= 0
End Sub

Related

vb.net DataGridView how to stay in current cell when I press enter or tab key?

Now I'm making code that after I change a text in a cell and then, when I press enter key or tab key, then the currente cell stay in the same cell which I modified. I searched in here about that (Link)
But it dosen' work for me. What did I make a mistake in that?
Public row1, col1 As Integer
Public selectedPart1 As String
Private Sub DataGridView1_KeyDown(sender As Object, e As KeyEventArgs) Handles DataGridView1.KeyDown
row1 = DataGridView1.CurrentCell.RowIndex
col1 = DataGridView1.CurrentCell.ColumnIndex
selectedPart1 = DataGridView1(0, row1).Value.ToString
If e.KeyCode = Keys.Enter Or e.KeyCode = Keys.Tab Then
MsgBox("ok")
DataGridView1.CurrentCell = DataGridView1(col1, row1)
e.SuppressKeyPress = True
End If
End Sub
to disable Tab Key you can set StandardTab property to True:
DataGridView1.StandardTab = True
to disable Enter key after edit use events CellEndEdit and SelectionChanged
Private currentRow, currentCell As Integer
Private resetRow As Boolean = False
Private Sub DataGridView1_SelectionChanged(sender As Object, e As EventArgs) Handles DataGridView1.SelectionChanged
If resetRow Then
resetRow = False
DataGridView1.CurrentCell = DataGridView1.Rows(currentRow).Cells(currentCell)
End If
End Sub
Private Sub DataGridView1_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
resetRow = True
currentRow = e.RowIndex
currentCell = e.ColumnIndex
End Sub

Is there anyway to change ComboBox key inputs in VB.NET?

I have a combobox in my Userform. Whilst this is in focus, I am wanting to use the keyboard for WASD controls. However, I find that when I press WASD the Combobox is bringing up values beginning with these letters. Also, the arrow keys are cycling through the options as well.
Is there anyway to restrict these commands?
I have tried
Tool.AutoCompleteMode = AutoCompleteMode.None
This does not stop is doing this. Does anybody have any clue how I could access more specific controls to stop this kind of autofill happening?
If you have only the Combobox control in your form and no other input controls, such as textbox, you can try the following code:
Please set e.Handled = True in the KeyPress event of the Combobox.
Private Sub ComboBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles ComboBox1.KeyPress
e.Handled = True
End Sub
If you have some input controls, you want to retain the editing function, and you want to not interfere with the KeyPress event of the form.
You could refer to the following steps.
(1) Please set Me.KeyPreview = False in the KeyDown event of each input control.
(2) Please set e.Handled = True in the KeyPress event of ComboBox1.
(3) When double-clicking the form to enter WASD, you can set ComboBox1.Focus().
Sample code:
Public Class Form1
Private Sub Form1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles MyBase.KeyPress
'edit your code
Dim offset As Integer = 10
If e.KeyChar = "a" Then
PictureBox1.Location = New Point(PictureBox1.Location.X - offset, PictureBox1.Location.Y)
ElseIf e.KeyChar = "w" Then
PictureBox1.Location = New Point(PictureBox1.Location.X, PictureBox1.Location.Y - offset)
ElseIf e.KeyChar = "s" Then
PictureBox1.Location = New Point(PictureBox1.Location.X, PictureBox1.Location.Y + offset)
ElseIf e.KeyChar = "d" Then
PictureBox1.Location = New Point(PictureBox1.Location.X + offset, PictureBox1.Location.Y)
End If
e.Handled = True
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.KeyPreview = True
End Sub
Private Sub Form1_DoubleClick(sender As Object, e As EventArgs) Handles MyBase.DoubleClick
ComboBox1.Focus()
Me.KeyPreview = True
End Sub
Private Sub ComboBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles ComboBox1.KeyPress
e.Handled = True
End Sub
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
Me.KeyPreview = False
End Sub
End Class
Result:

Get selected value from DateTimePicker

I would like to get selected value from DateTimePicker in VB (If I select only day value then I would like to get only selected day value.)
In this image I have selected (blue marked) year value from this DateTimePicker. So I need only this year value.
In the case of TextBox I can get selected value using
TextEndTime.SelectedText
Is there any syntax or approach to get selected value from DateTimePicker?
As the DateTimePicker-control can be manipulated by using the arrow keys, you can use SendKeys to change the current selected value.
The following example gets the current DateTime-value of the DateTimePicker and, after sending the ↑ key, compares the value to the new value. At last it resets the DateTimePicker to the original value.
So the variable currSelected will contain the last Selection.
Dim currVal As DateTime
Dim newVal As DateTime
Dim valCheck As Boolean
Dim currSelected As Selection = Selection.None
Public Enum Selection
None = 0
Year = 1
Month = 2
Day = 3
End Enum
Private Sub CheckDTPSelection(dtp As DateTimePicker)
valCheck = True
currVal = dtp.Value
SendKeys.Send("{UP}")
End Sub
Sub RefreshSelection(dtp As DateTimePicker)
If valCheck Then
newVal = dtp.Value
If currVal.Year <> newVal.Year Then
currSelected = Selection.Year
ElseIf currVal.Month <> newVal.Month Then
currSelected = Selection.Month
ElseIf currVal.Day <> newVal.Day Then
currSelected = Selection.Day
End If
dtp.Value = currVal
valCheck = False
End If
End Sub
Private Sub MyDateTimePicker_DropDown(sender As Object, e As EventArgs) Handles MyDateTimePicker.DropDown
RemoveHandler MyDateTimePicker.MouseUp, AddressOf MyDateTimePicker_MouseUp
End Sub
Private Sub MyDateTimePicker_CloseUp(sender As Object, e As EventArgs) Handles MyDateTimePicker.CloseUp
AddHandler MyDateTimePicker.MouseUp, AddressOf MyDateTimePicker_MouseUp
CheckDTPSelection(MyDateTimePicker)
End Sub
Private Sub MyDateTimePicker_KeyUp(sender As Object, e As KeyEventArgs) Handles MyDateTimePicker.KeyUp
If e.KeyValue = Keys.Left OrElse e.KeyValue = Keys.Right Then
CheckDTPSelection(MyDateTimePicker)
End If
End Sub
Private Sub MyDateTimePicker_MouseUp(sender As Object, e As MouseEventArgs) Handles MyDateTimePicker.MouseUp
CheckDTPSelection(MyDateTimePicker)
End Sub
Private Sub MyDateTimePicker_ValueChanged(sender As Object, e As EventArgs) Handles MyDateTimePicker.ValueChanged
Dim dtp As DateTimePicker = DirectCast(sender, DateTimePicker)
RefreshSelection(dtp)
End Sub
Private Sub Btn_WhatsSelected_Click(sender As Object, e As EventArgs) Handles Btn_WhatsSelected.Click
'Show the current selected value in a MessageBox
MessageBox.Show(currSelected.ToString())
End Sub
Hi Everyone and thanks for your tips that the DateTimePicker-control can be manipulated.
I had the selection problem with DateTimePicker , the currently selected item value could not send to a text box, as DTP works only valuechanged event. I spend 4 hours time to find the solution and wrote the following code:
Public MyEventCounter As Integer = 0
Private Sub DTPAcquDt_DropDown(sender As Object, e As EventArgs) Handles DTPAcquDt.DropDown
RemoveHandler DTPAcquDt.MouseUp, AddressOf dtpacqudt_closeup
End Sub
Private Sub dtpacqudt_closeup(sender As Object, e As EventArgs) Handles DTPAcquDt.CloseUp
AddHandler DTPAcquDt.MouseUp, AddressOf dtpacqudt_closeup
'Check the Mouse/Keys event counter
If MyEventCounter > 0 Then
TxtDtAcqu.Text = DTPAcquDt.Value
'RESET The Counter
MyEventCounter = 0
End If
End Sub
Private Sub DTPAcquDt_KeyUp(sender As Object, e As KeyEventArgs) Handles DTPAcquDt.KeyUp
If e.KeyValue = Keys.Left OrElse e.KeyValue = Keys.Right Then
MyEventCounter = MyEventCounter + 1
End If
End Sub
Private Sub DTPAcquDt_MouseUp(sender As Object, e As MouseEventArgs) Handles DTPAcquDt.MouseUp
MyEventCounter = MyEventCounter + 1
End Sub
Private Sub DTPAcquDt_ValueChanged(sender As Object, e As EventArgs) Handles DTPAcquDt.ValueChanged
TxtDtAcqu.Text = DTPAcquDt.Value
'RESET The Counter
MyEventCounter = 0
End Sub

Deselect combobox text on leaving using suggestappend

I have a databound ListItems combobox with AutoComplete.SuggestAppend
and would like to navigate out of the combobox to different controls using the up/down arrow keys rather than scrolling the items.
The issue is that if the text isn't completed the suggested text remains highlighted while the next control has focus.
Link to image example
Here is some code showing a simple example of what I am doing
Public Class Form1
Dim PreventCboBoxChanging As Boolean
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ComboBox1.DataSource = New List(Of String)(New String() {10, 11, 20, 30})
ComboBox1.AutoCompleteSource = AutoCompleteSource.ListItems
ComboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend
End Sub
Private Sub ComboBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles ComboBox1.KeyDown
If PreventCboBoxChanging = True Then
e.Handled = True
End If
PreventCboBoxChanging = False
End Sub
Private Sub ComboBox1_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs) Handles ComboBox1.PreviewKeyDown
If e.KeyCode = Keys.Down Or e.KeyCode = Keys.Up Then
PreventCboBoxChanging = True
TextBox1.Select()
End If
End Sub
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Down Or e.KeyCode = Keys.Up Then
ComboBox1.Select()
End If
End Sub
Private Sub ComboBox1_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles ComboBox1.Validating
Dim index As Integer = sender.FindString(sender.Text)
If index > -1 Then
sender.SelectedIndex = index
Else
e.Cancel = True
Me.Text = ""
Beep()
End If
End Sub
End Class
Is there any way to deselect the text?
I found the solution in another thread. Needed to turn off the Combobox AutoComplete Mode, change focus then reenable SuggestAppend mode under the PreviewKeyDown event.
Private Sub ComboBox1_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs) Handles ComboBox1.PreviewKeyDown
If e.KeyCode = Keys.Down Or e.KeyCode = Keys.Up Then
ComboBox1.AutoCompleteMode = AutoCompleteMode.None
TextBox1.Select()
ComboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend
End If
End Sub
This should make it:
Private Sub ComboBox1_Leave(sender As Object, e As EventArgs) Handles ComboBox1.Leave
ComboBox1.SelectionLength = 0
End Sub

Datagridview - edit selected row?

I have set Datagridview .ReadOnly property to True, and then added a button column. Button column is meant for edit button when clicked, but I wish to edin only currently selected row. This is what I tried:
EDIT:
Public Class Form2
Private Sub Form2_Resize(sender As Object, e As EventArgs) Handles Me.Resize
Me.DataGridView1.Height = 0.8 * Me.Height
End Sub
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'Users._USERS' table. You can move, or remove it, as needed.
Me.USERSTableAdapter.Fill(Me.Users._USERS)
Me.DataGridView1.DefaultCellStyle.Font = New Font("Arial", 7)
End Sub
Protected Overrides Sub OnLoad(e As EventArgs)
MyBase.OnLoad(e)
For i As Integer = 0 To DataGridView1.Rows.Count - 1
DataGridView1.Rows(i).ReadOnly = True
Next
End Sub
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
Dim yourColumnIndex As Int32 = 3
If e.ColumnIndex = yourColumnIndex Then
If MsgBox("Do you wish to edit records?", vbQuestion + vbYesNo, "Edit records") = vbYes Then
DataGridView1.Rows(e.RowIndex).ReadOnly = False
End If
End If
End Sub
Private Sub DataGridView1_RowLeave(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.RowLeave
DataGridView1.Rows(e.RowIndex).ReadOnly = True
End Sub
End Class
You can't set the grid's ReadOnly property to true. After the rows are added to the grid, you would have to loop through your rows and set the ReadOnly property for each row:
Protected Overrides Sub OnLoad(e As EventArgs)
MyBase.OnLoad(e)
For i As Integer = 0 To DataGridView1.Rows.Count - 1
DataGridView1.Rows(i).ReadOnly = True
Next
End Sub
Note: you can't set these properties in the form's constructor, a quirk of the DataGridView control.
Then use the RowIndex property provided by the e parameter:
Private Sub DataGridView1_CellContentClick(sender As Object,
e As DataGridViewCellEventArgs)
Handles DataGridView1.CellContentClick
If e.ColumnIndex = 3 Then
DataGridView1.Rows(e.RowIndex).ReadOnly = False
End If
End Sub
Set it back to true when leaving the row:
Private Sub DataGridView1_RowLeave(sender As Object,
e As DataGridViewCellEventArgs)
Handles DataGridView1.RowLeave
DataGridView1.Rows(e.RowIndex).ReadOnly = True
End Sub
This is working, I just changed your suggestion a little bit (DatagridView Readonly property must be set to False):
Protected Overrides Sub OnLoad(e As EventArgs)
MyBase.OnLoad(e)
For Each band As DataGridViewBand In DataGridView1.Columns
band.ReadOnly = True
Next
End Sub
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
Dim yourColumnIndex As Int32 = 3
If e.ColumnIndex = yourColumnIndex Then
If MsgBox("Do you wish to edit record?", vbQuestion + vbYesNo, "Edit record") = vbYes Then
For Each band As DataGridViewBand In DataGridView1.Columns
band.ReadOnly = False
Next
End If
End If
End Sub
Private Sub DataGridView1_RowLeave(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.RowLeave
For Each band As DataGridViewBand In DataGridView1.Columns
band.ReadOnly = True
Next
End Sub
Thanks for all your help Lars!