Place a cursor (for editing) in datagridview cell text using a right click - vb.net

Im trying to piece together vb.net code that will allow the user to right click in a datagridview and then place a cursor (or is it a caret?) into the text of the cell.
I can already get the row and cell using
Dim hti As DataGridView.HitTestInfo = sender.HitTest(e.X, e.Y)
and isolate the right click on a mouse down
If e.Button = Windows.Forms.MouseButtons.Right Then
I also have the position where the user right clicked using
Dim pt As Point = Me.PointToClient(Control.MousePosition)
what seams to elude me is how to put it all together to place the cursor where the user right clicked.
What I don't want is to place a cursor into a cell just anywhere, I need to place a cursor into the cells text (if any is there) where the user right clicked in the cell.
The point of the code will be to allow the user to place some text into the cell at the clicked position based on a selection from a right click menu. Appreciate any helpful suggestions.
Kind regards
Michael

Something like this possibly... should work reasonably well.
In your DataGridView CellMouseDown event handler:
If e.Button = MouseButtons.Right Then
'Get coordinates within cell
_point = New Point(e.X, e.Y)
'we are going to handle setting the edit state of the cell
_handleEdit = True
'Set current cell to the one we right clicked in (this will trigger the CellEnter event)
dataGridView1.CurrentCell = dataGridView1(e.ColumnIndex, e.RowIndex)
End If
In your DataGridView CellEnter event handler:
If _handleEdit Then
'enter edit state
dataGridView1.BeginEdit(False)
'set the seletionstart property based on position
CType(dataGridView1.EditingControl, TextBox).SelectionStart = CType(dataGridView1.EditingControl, TextBox).GetCharIndexFromPosition(_point)
'Done handling the edit state
_handleEdit = False
End If

Related

Vb.net TableLayoutPanel Rows overlapping while every second alternate row control visible true/false

Small overview:-
(Assuming the row index starts with 1 for better clarity)
I have every odd row (1,3,5...) Drug information.
Every alternate row (2,4,6...) has a special description field which is optional
which i have kept by default visible = false.
So I had kept a button in every first row (1,3,5...) when clicked the appropriate control TextBox (in next row) visible property is assigned true/false (toggle button).
The second row has only one TextBox control.
Hence when TextBox control is false it is expected that the second row height is automatically zero and when its true the appropriate second row should be visible.
I have set all the rows height autosize
Issue:-
When the button is clicked the TextBox comes overlapped on third row (3,5,7....) instead of displaying on its appropriate second row(2,4,6...)
When i set the TextBox default visible = true then it takes every second row correctly. But its unnecessary taking a lot of space. As the TextBox entry is optional as said and not required everytime.
Following are some sreenshots for better more clarity for my issue:-
Fig.1: TableLayout Design
Fig.2: Overlapped on third row instead of taking visible second row
Fig.3: When second row TextBox visible is set true by default
My Code behind the button click:-
Private Sub Button_Drug_Dosage_General_Instructions_Click(
sender As Object, e As EventArgs
) Handles Button_Drug_Dosage_General_Instructions_20.Click,
Button_Drug_Dosage_General_Instructions_19.Click,
Button_Drug_Dosage_General_Instructions_18.Click,
Button_Drug_Dosage_General_Instructions_17.Click,
Button_Drug_Dosage_General_Instructions_16.Click,
Button_Drug_Dosage_General_Instructions_15.Click,
Button_Drug_Dosage_General_Instructions_14.Click,
Button_Drug_Dosage_General_Instructions_13.Click,
Button_Drug_Dosage_General_Instructions_12.Click,
Button_Drug_Dosage_General_Instructions_11.Click,
Button_Drug_Dosage_General_Instructions_10.Click,
Button_Drug_Dosage_General_Instructions_09.Click,
Button_Drug_Dosage_General_Instructions_08.Click,
Button_Drug_Dosage_General_Instructions_07.Click,
Button_Drug_Dosage_General_Instructions_06.Click,
Button_Drug_Dosage_General_Instructions_05.Click,
Button_Drug_Dosage_General_Instructions_04.Click,
Button_Drug_Dosage_General_Instructions_03.Click,
Button_Drug_Dosage_General_Instructions_02.Click,
Button_Drug_Dosage_General_Instructions_01.Click
If Initialization_In_Progress = True Then Exit Sub
'------------------------------------------------------------------------------------------------
'Button_Drug_Dosage_General_Instructions_obj
'------------------------------------------------------------------------------------------------
Dim btn_Button_Drug_Dosage_General_Instructions_obj As Button = CType(sender, Button)
'------------------------------------------------------------------------------------------------
'------------------------------------------------------------------------------------------------
Dim object_name As String = btn_Button_Drug_Dosage_General_Instructions_obj.Name
Dim xCustomer_Selected_srno As Double = object_name.Substring(object_name.Length - 2)
'------------------------------------------------------------------------------------------------
'------------------------------------------------------------------------------------------------
Dim txtbox_TextBox_General_Instructions_obj As TextBox = CType(Me.Controls.Find("TextBox_Drug_Dosage_General_Instructions_" + (xCustomer_Selected_srno).ToString("00"), True)(0), TextBox)
'------------------------------------------------------------------------------------------------
If txtbox_TextBox_General_Instructions_obj.Visible = False Then
txtbox_TextBox_General_Instructions_obj.Visible = True
txtbox_TextBox_General_Instructions_obj.SelectionLength = 0
Else
txtbox_TextBox_General_Instructions_obj.Visible = False
End If
End Sub
Hope I have made my best to put the exact issue. If still any further more information is needed I would definately post.
My other workouts:-
Tried TextBox Dock Fill, None, Anchor Top,Left, also all, RowHeight Absolute, but nothing worked.

Remove previous selection highlighting in RichTextBox without scrolling

I have a form with a RichTextBox (RTB) and a listBox.
When the end user selects an item in the listbox, any matched text in the RTB is highlighted (full code removed for brevity).
re = New Regex(txtToFind)
For Each m In re.Matches(rtbMain.Text)
rtbMain.[Select](m.Index, m.Length)
rtbMain.SelectionColor = Color.White
rtbMain.SelectionBackColor = System.Drawing.SystemColors.Highlight
Next
When the user left mouse clicks in the RTB I want the previously highlighted text to be cleared. This is the standard windows behaviour - If you manually select some text in an RTB with the mouse, it is highlighted, click anywhere back in the RTB and the highlighting disappears. My programatically selected text remains selected.
I have some partially working code (below). I can clear all the highlighted text, but it is by process of selecting everything, changing the colour back and then deselecting it again. I know it is not efficient, the RTB flickers and I am sure it is not the correct way to do it. Can I emulate the standard windows behaviour?
Also using my code, it scrolls to the first line when entering the RTB a second time.
I get around this the first time by returning the top visible line index before clearing the text and then selecting that line again afterwards and using ScrollToCaret(). This only works on the first pass. Subsequent MouseDown events select the top row regardless of where the user has clicked so nothing can be manually highlighted in the RTB.
Private Sub rtbMain_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles rtbMain.MouseDown
Dim topIndex As Integer = rtbMain.GetCharIndexFromPosition(New System.Drawing.Point(1, 1))
Dim topLine As Integer = rtbMain.GetLineFromCharIndex(topIndex)
If e.Button = Windows.Forms.MouseButtons.Right Then
'Do nothing (Context Menu)
Else
rtbMain.SelectAll()
rtbMain.SelectionColor = Color.Black
rtbMain.SelectionBackColor = Color.White
rtbMain.DeselectAll()
rtbMain.Select(topIndex, 0)
rtbMain.ScrollToCaret()
End If
End Sub
I need my code to emulate the standard windows behaviour - clear selected text highlighting on MouseDown and leave the mouse cursor where the user has clicked.
Any help anyone can offer is gratefully appreciated.
I think you may be overthinking this one.
In the right click event, try RtbMain.SelectionLength = 0

DataGridView: programmatically select/highlight entire column on button click

I want to programmatically select/highlight entire column on button click to let users know what they searched.
This is what currently happens on button click ("GO" button)
but this is what I need to happen.
I have tried these so far to no avail:
DataGridView2.SelectionMode = DataGridViewSelectionMode.FullColumnSelect
DataGridView2.Columns(2).Selected = True
Doing so gave me this error: DataGridView control's SelectionMode cannot be set to FullColumnSelect while it has a column with SortMode set to DataGridViewColumnSortMode.Automatic.
I also tried to simply select the whole column. No error, but it didn't work.
DataGridView2.Columns(2).Selected = True
Just set the Selected property of each cell in the column.
Found this thread. I modified the code like so:
Dim row As DataGridViewRow
For Each row In DataGridView2.Rows
row.Cells(1).Selected = True
Next
and placed it inside Private Sub Button1_Click
I noticed that the first cell is automatically selected. This solved that problem.

Select first visible cell of new row in DataGridView

I'm trying to focus input and fire the editing event on each new row that I add to a DataGridView in my form.
This is the code I am trying to user to achieve this.
Private Sub grd_GoldAdders_RowsAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowsAddedEventArgs) Handles grd_GoldAdders.RowsAdded
Dim grid As DataGridView = DirectCast(sender, DataGridView)
grid.ClearSelection()
If grid.Rows(e.RowIndex).Cells("grid_flag").FormattedValue = Constants.[New] Then
For Each cell As DataGridViewCell In grid.Rows(e.RowIndex).Cells
If Not cell.Visible Then Continue For
grid.CurrentCell = cell
grid.BeginEdit(False)
Exit For
Next
End If
End Sub
The "grid_flag" is a hidden cell which is used to store custom states for a row.
Prior to adding a row, this is what we see on the form:
This is what we see when we actually try and add a new row:
Notice that both the column 0,0 and the first visible column of the new row are selected, but the column 0,0 has the focus. I do not wish for 0,0 to either get selected or have the focus. I also see here that the row indicator is pointing at row 0 too...
This is how I would like to see things after clicking my Add button:
Does anyone know where I am going wrong with the code? I've searched SO for the most part of the day trying to solve this one.
Instead of using your DataGridView's RowAdded event to set the CurrentCell, add the following code wherever you're adding a new record to your DGV (in your Add button's Click event I assume):
''# Add the new record to your Data source/DGV.
For Each row As DataGridViewRow In grd_GoldAdders.Rows
If row.Cells("grid_flag").FormattedValue = Constants.[New] Then
grd_GoldAdders.CurrentCell = row.Cells("AssySiteColumn") ''# I'm calling the first column in your DGV 'AssySiteColumn'.
grd_GoldAdders.BeginEdit(False)
Exit For
End If
Next
This code simply loops through all the rows in your DGV and specifies as the CurrentCell the first cell in the first row with your Constants.[New] flag value.

VB.NET ComboBox - Need to force a redraw when a key is pressed when it is dropped down

I am using a DrawItem and MeasureItem events to paint a combobox with a DrawMode of OwnerDrawVariable.
Basically, I'm trying to have the user highlight a selection with the mouse, and then press the space bar to toggle the Save status of a song list. Then I call the Me.Refresh() event for the form in an attempt to redraw the form and the ComboBox.
The problem that I am running into is that only the Combobox itself (not the drop-down area) that is a control on the main form is redrawing, and the text that is behind the mouse-highlighted selection of the drop-down list is not changing from Red to Black as I believe it should. If I move the mouse to another selection, then the color does in fact update.
Here is a snippet of the code.
If (e.KeyCode = Keys.Space) Then
If cmbList.SelectedItem IsNot Nothing Then
With DirectCast(cmbList.SelectedItem, SongTitle)
.bSave = Not .bSave
End With
End If
End If
e.Handled = True
Me.Refresh()
Thanks for any help you can provide.
You need to use .RefreshItem/.RefreshItems instead of .Refresh.
See this question: Dynamically changing the text of items in a Winforms ComboBox