VB.NET end cell editing mode on DataGridView lost focus - vb.net

I am building a WinForms application and I have a class called DecimalDataGridView that inherits the vb.net DataGridView. In this class I've defined an editing control class that essentially only permits the user to input numeric inputs into cells. This custom datagridview works perfectly whenever cells are edited and focus is changed to a different cell. However, I've noticed that when focus is lost outside the datagridview control (like another text box or control), the cell that was being edited (if it was in edit mode) doesn't leave edit mode and doesn't validate. This ends up messing up a bunch of other validation events in other controls, and pretty much breaks the form. The only way to resolve this issue is to re-enter the cell that was being edited, finish the edit, and then move focus to another cell in the datagridview.
To fix this issue, I'm trying to force-end the editing mode, whenever a cell is leaving (see my CellLeave event below.) I've tried to find solutions to this online, and it seems like people have had similar issues, but so far nothing has worked. The 'EndEdit' does not appear to work, nor do CommitEdit or CancelEdit. I've also tried toggling the current cell's read only property to see if that ends the edit mode, but no luck. Any solutions to this issue, or suggestions on different approaches are greatly appreciated. Thanks
Private Sub DecimalDataGridView_CellLeave(sender As DecimalDataGridView, e As EventArgs) Handles Me.CellLeave
EndEdit()
End Sub

Related

in Access, Bound Combo Boxes do not update unless the form is already Dirty

I'm working on a fairly complex Access Database, trying to build forms with custom buttons for working with records. I'm using list boxes to display and navigate through records and all fields for existing records are disabled unless the user presses an edit button. The problem I'm having is that if I press the Edit or Add New button, enabling all of the fields, and then try to change the selection in a combo box, it does not update on the first try.
If I edit a text box first then the combo boxes work fine.
I've determined that the control's beforeUpdate and afterUpdate events are not firing on the first try but the action triggers the form_Dirty event and then it works as expected. I tried setting Me.Dirty = True with the Edit button and that solved the problem but it causes problems with some of my other code and it seems like an unnecessary workaround if only I understood the actual cause of the problem. It also works as expected if I leave the Combo Box unbound, but I am trying to build a template that doesn't require too much work to build new forms off of and I would rather not go that route.
It must have something to do with some bit of code I'm using because I can start a basic form and the combo boxes work fine.
I've started a basic test form and am adding code from my template form bit by bit until the problem arises, but it's a tedious process. Any help would be appreciated.
What am I missing? Is there some way of getting the Combo Box events to fire before the form is dirty?
UPDATE:
I have templates for a basic form, a form with a single subform, and a form with multiple subs in a tab control.
After some more testing I discovered that this problem does not apply to the basic form which has no subforms. This template uses similar code to the others for new, edit, cancel, save and delete buttons and for preventing accidental changes, preventing Form_unload during an edit and so on. The main difference I can think of off the top of my head is that the templates with subforms use class modules and collections to hold various data and pass it between the main form and subforms. Not sure if or how this might relate to combo box functionality.
I built most of this last winter then got too busy over the summer to work on it. Just now picking it up again and I'm having to re-learn a lot of the details of how my code works.
you can try to use Me.CboName.Requery in your After Update event.
I figured it out. I was calling a procedure with a form refresh in it from the Form_Dirty event and for whatever reason the refresh at exactly the wrong time was causing the combo box Before_Update and After_Update events to be skipped and the combo box value to not change. It was also causing text entered into a textbox to overwrite existing text on the first keystroke.

Is there anyway to not let user lost focus from winforms in vb.net?

Issue:
My Question may be weird but I need help with this.
I have this weird problem with my client PC and it all happens sometimes.
The application loses control when I close one form and opens another form.
Then he has to use the mouse and click on the form every time.
Similar but another issue.
In an MDI child form, what I do is make the user fill data in a bunch of textboxes and make the user click (or hit Enter Key) that adds that data in a datagridview row and clears all textbox and make focus on the first textbox.
At that time too the form loses focus and the user is not able to write anything. Though the textbox is still coloured (custom coloured textbox on got focus) but form loses control.
The Temporary solution:
-I make his PC restart and everything starts working fine.
-Even Closing the whole application won't help it. I have to restart the PC only.
MY Code between two forms:
Private Sub btnOk_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnOk.Click
....
Hide()
frmUser.Show()
End Sub
I use Hide to close 1st form else I use close() to switch between forms.
I even face this issue of losing focus in MDI child forms too.
I even made a custom textbox that changes its background colour when it gets focus (in gotfocus event) and reverse to white when loses focus (lost focus event), so when the issue starts the textbox remains coloured and even the cursor blinking inside but I cannot type in it. Again I have to click on the application form (anywhere) and I am able to type again. Until that form closed or changed in the application.
Please help with this weird issue. Thanks...

Cannot edit cells after touching combobox - I think i've encountered a bug

So here i was building a form in excel for work ya, and to make it easy on the eyes and fool proof i removed 90% of the UI. On that form i put some activex combo boxes. Everything was perfect, and then i tried to edit a cell after i entered in the data in that combo box, but low to my woes i couldnt.
It seemed that touching the combo box locked every other cell in the form. Flabbergasted i did some experimenting and found a way to isolate the issue. Thus i created a test environment for the error(doc linked below). When the UI is removed clicking the combobox locks all other cells. But when the UI is there, no issue happens. Furthermore it seems the formula bar being disabled is the cause.
Is this a bug, a programming error, or something else. I'm at a total lost and would appreciate any help.
My test File, for those how want to try it
When in edit mode shift+esc to hide UI, when hidden hold shift+ctrl then type edit to bring back UI.

Access cell color value of datagridview when it has been set via the cellpainting event args

My searches regarding this have only turned up how to set the color of a datagridview cell, whether that be in the formatting, cell painting, or databound events.
The code that I'm trying to work with is currently setting the cellstyle.backcolor through the datagridview event args in the cellpainting event:
Private Sub dgvAncientCode_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles dgvResults.CellPainting
If dgvAncientCode.Columns(e.ColumnIndex).Name = "Blah" Then
e.CellStyle.BackColor = Color.Red
End If
This works--the specified cells do show up as red--but how can I access this value elsewhere in code? When I check the cell.style it's set to the default, yet it displays on the screen as red.
I know there is a lot wrong with this code, but I'm still relatively new and I'm supporting a lot of legacy stuff that's comprised of every worst practice you can think of. I did come up with a workaround for this by setting the cell.style.backcolor instead of the e.CellStyle.BackColor, which lets me access the value later, but I'm also trying to understand what was happening with the original code. I hate making things work without understanding why, mainly because I know I might be missing something important.
I apologize if this isn't clear or if it's been asked before. I've searched for a couple hours here and elsewhere, so hopefully I didn't miss it.
Edit for additional information: There is a button click event that iterates through the datagridview using a for each loop. That is being used to set the cell color of a custom excel export class. This did not work because none of the cells in the datagridview had a value on the cell.style.backcolor property, even though they show as red in the form. If the cell displays as Red in the form, I should be able to access that somehow--at least, I think I should.
Try one of this. Maybe it can help you. "yourdatagrid.ControlStyle", "yourdatagrid.ItemStyle".

Transferring data between two forms?

Okay so I've been searching for a solution for a while but can't seem to find any answers tailored to what I need to do. Also this is my first post here so I'm sorry if I'm being to vague with what I need. Basically I'm creating a program that has a few text boxes on one form and I need to display the data in those text boxes into a list box on another form. There's a button at the bottom of the first form that allows me to switch to form 2 and also display the data in the list box. I can switch to form 2 but nothing shows up in the list box. I also have a few radio buttons and check boxes on form 1 that will affect which constant values will need to show up in form 2's list box as well. One other thing is that I can't just use a form load option for getting data in the list box, it has to happen with the button press because I will be switching between forms and from my understanding the form load option only works once when the form is loaded for the first time. Anyway here is part of my code for form 1 that shows the button click:
Dim strCustomer As String
Private Sub btnPlaceOrder_Click(sender As Object, e As EventArgs) Handles btnPlaceOrder.Click
strCustomer = txtCustomer.Text
frmInvoice.ShowDialog()
frmInvoice.lstCustomerOrder.Items.Add(Me.strCustomer)
End Sub
My first form is frmMain and my second form is frmInvoice. Can anyone please help me with what I need to do differently with this code and what exactly I need to have for my code for form 2 to make this work. Again I am somewhat new to this so I'm sorry if any of this seems vague or if I'm not quite posting this is the right way. Also I'm using VB.
The code you provided should work fine but you are modifying the ListBox after you show the invoice Form. Since you are using showDialog(), the code that follows this statement will only be executed once the dialog form is dealt with (for example if you close it). This means that if you click the button a 2nd time, the ListBox should contain one item. If you switch the two lines so it looks like this, it should do what you expect it to:
frmInvoice.lstCustomerOrder.Items.Add(Me.strCustomer)
frmInvoice.ShowDialog()