While in edit mode, advance to next cell and stay in edit mode for VB.NET DataGridView? - vb.net

I have a DataGridView in which one column has data that the user needs to align by adding spaces. For example, the first two rows might contain:
kumbu
kuimbiu
And the user needs to be able to line up the letters that match by adding spaces. Something like this:
ku mb u
kuimbiu
Now in order to do that with the DataGridView, the user must enter edit mode in the top cell, add spaces, hit enter, re-enter edit mode in the bottom cell, and then add spaces. Our users would like to be able to, while in edit mode in the top cell, hit the down arrow and advance to the second cell while staying in edit mode, saving clicks or F2 hits.
Is there a good way to do this? I have tried trapping the down arrow key press, leaving edit mode, advancing a cell, and then entering edit mode with the grid's BeginEdit method, but this does not do what I want.
Any ideas?

When leaving the cell capture the edit status in a class variable. When the user presses down or enter, the next cell will begin edit mode, but only if the previous cell was in edit mode. You can add additional logic if you want it to be based upon columns.
Private blnEditMode As Boolean = False
Private Sub dgv_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv.CellEnter
If blnEditMode Then
dgv.BeginEdit(False)
End If
End Sub
Private Sub dgv_CellLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv.CellLeave
blnEditMode = dgv.IsCurrentCellInEditMode
End Sub

Related

Determine initial text selection in DataGridView Cell

A little question on use of DataGridView.
When I enter a Cell, text is all selected (in blue in the example). But I want to set the caret position at the right side of the text, for my user can delete the last character without clicking twice to place caret.
Have you some idea, because I don't find the good property...
I try to change behaviour when I enter in cell (edit with F2, in simple click), but test is always all select
Use DataGridView1.CellClick event and BeginEdit to allow casting Cell in textbox. After that set cursor position using textbox.select (from,Length) :
Private Sub cellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
DataGridView1.BeginEdit(False)
Dim tbCell As TextBox = CType(DataGridView1.EditingControl, TextBox)
tbCell.Select(tbCell.Text.Length, 0)
End Sub
The functioning of DataGridView has always been a problem for me.
tuyau2poil, your answer was perfect for my first question. I integred the code in DataGridView_CellDoubleClick and it's OK. Thousand of thanks
For my second question, I think the way is on Jimy anwser:
Now I search, when I select the cell with 1 click and press "Del", to delete the last character. That works, but when I type a new character + Enter, that delete all the cell for write only the new charactere.
This is for my users have to modify only the last number of the serial number enter in cell...
"21000_001" to become "21000_002" with "Del" + key press "2" + Enter .
Actually I've got "21000_001" then "2"
I think solution is arount of DataGridView_EditingControlShowing
Thanks

DataGridView resets to top when clicked after mouse scroll

I have a DataGridView control on a TabPage of a Windows Form application.
When the user moves the mouse over the DataGrid and uses the scroll wheel, the grid scrolls as expected. But when the user clicks in a cell on the screen, instead of the cell receiving focus, the DataGrid resets to the top and requires the user to scroll down again. This response is non-intuitive since it's not immediately obvious that the cell you thought you clicked on isn't there anymore.
I would be happy to prevent the DataGrid from responding to the scroll wheel until the user clicks in the grid, or preferably to maintain the current actions except not resetting to the top when first clicked.
From what I've researched here, it appears that the DataGrid is rebinding because I'm resetting the binding when the tabpage is entered (since the database might have been updated by one of the other tabs.
Private Sub TabPage1_Enter(sender As Object, e As EventArgs) Handles TabPage1.Enter
LoadTACTable()
End Sub
In LoadTACTable():
dbGetList("spSelectTACList", dtTACs, 0, 100000, Nothing) ' Record numbers are 0 based
bsTACs.DataSource = dtTACs
With gridTACs
' TOTAL Grid width = 1380
.DataSource() = bsTACs
.
.
.
(Showing only part of the code for brevity.
Is there a way to see if the TabPage is already displayed when entered? Or, is unnecessary to reset the gridTAC datasource every time I retrieve the data from the SQL database to the dtTACs datatable using my dbGetList() sub?
There are several possible solutions to your problem. One would be to not automatically rebind the datagrid but let the user do it by clicking some refresh button. That way the user would not see non-intuitive behavior.
You mentioned that the contents of one tab may need to be refreshed when the contents of other tabs are changed. Whenever the contents of a tab is changed and can affect other tabs, you could flag these other tabs (for example, by adding a star to their titles) to indicate that they no longer have the latest data. The user would then know that the tab needs to be refreshed.
There might be other solutions, but it is difficult to tell without knowing more about your use case.
With the guidance above, I believe I solved the issue:
I created a flag:
Dim TabDirty As Boolean
Then I set it in the TabPage.Leave handler:
Private Sub TabPage1_Leave(sender As Object, e As EventArgs) Handles TabPage1.Leave
dtTACs.Dispose()
TabDirty = True
End Sub
Then I just check it when I enter the TabPage:
Private Sub TabPage1_Enter(sender As Object, e As EventArgs) Handles TabPage1.Enter
If TabDirty = True Then
TabDirty = False
LoadTACTable()
End If
End Sub
So far, this appears to work - the grid is not getting reset when clicked, but I will do a bit more testing to confirm that the data is refreshed when necessary.

Check If User Pressed Enter to leave DataGridViewCell

I am working in VB.Net programming user functionality into a DataGridView. Currently, I am trying to allow users to update the data that is being shown in the DataGridView by editting what is in the Cells.
Right now I have a method to detect when the user begins the edit the cell:
Private Sub dataTable_CellBeginEdit(ByVal sender As Object, ByVal e As DataGridViewCellCancelEventArgs) Handles dataTable.CellBeginEdit
oldCellVal = dataTable.Rows(e.RowIndex).Cells(e.ColumnIndex).Value
End Sub
The point of the method is to store the old data in the cell in memory so that it can be restored later if the user decides not to edit the cell.
Now I am not sure how to set up the method for when the user completes the edit. I know there is a CellEndEdit event I can make a Handler for, but in that method how would I detect how they left the Cell?
I'd like to set it up so that if my User presses the Enter key, only then is their edit submitted. If they use the Arrow Keys, or the ESC key to exit the Cell, they instead get a "Would you like to stop editting?" prompt. This makes it sound like I need a keypress event, but if I do that kind of event, how do I properly detect the Cell that was modified? I need the know the row and column index of the updated Cell in order to properly submit the changes.
So how should I go about doing this? KeyPress or CellEndEdit? Or is there something else I haven't considered?

How to stop a user from pressing a button more than once

I've got a small problem of having a button and a combo box. In this situation the combobox holds values from 1 to 10. The enter button allows the user to select how many dynamic objects they want to produce however my program crashes every time I press enter after selecting another value and pressing enter again. So is there any form of validation I can have for my button to stop users from pressing enter twice.
Use a flag to supress user events.
Clear it again if need be at the appropriate time.
Private Enter_Locked As Boolean
Private Sub btn_Click(sender As Object, e As EventArgs) Handles btn.Click
If Enter_Locked Then Exit Sub
Enter_Locked = True
'do as you will here
End Sub

I have a "bug/glitch" in my vb program need assistance

I have a program that replaces every letter of the alphabet with something new inside the same textbox by clicking a button then buy clicking the button it translates it back to original text.
However when i run this program, yes it does work as needed but not 100%
Because after i click the button to translate, (yes it shows translated)
If delete the translated text and type something new, it goes back to the old text.
(this does not happen if i click the button again, it shows the orignal text. But if i type something new it will translate it.)
Any suggestions?
This is a continuation of your other question. You will need to reset your Boolean variable when you type in your TextBox i.e.
Private Sub TextBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
bConverted = False
End Sub