How to stop a user from pressing a button more than once - vb.net

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

Related

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.

Select and Show new Tab on control when values in txtbox change, but keep focus on txtbox

I'm having this issue that should be pretty easy to solve but I just can't seem to figure it out or find an answer yet. I have the following code:
Public Sub NotifyThatValuesChanged(sender As Object, e As EventArgs)
APIUserForm_MAIN.OnSecurityInputValuesChanged()
APIUserForm_MAIN.MessageSender.TabControl.SelectTab(0)
End Sub
So, I'm rasing events when values within txtboxes change, and one of the things I want to do is change tab focus when these values change, which it is doing...but....I don't want the cursor (or focus) to change to the selected tab. I want the cursor/focus to stay where it is at while this event happens, and for the tab selected on this other control to change from (1) to (0).
Thanks for the help!!!!
I don't think it's 100% possible. Try putting the focus back into the textbox:
Public Sub NotifyThatValuesChanged(sender As Object, e As EventArgs)
APIUserForm_MAIN.OnSecurityInputValuesChanged()
APIUserForm_MAIN.MessageSender.TabControl.SelectTab(0)
TextBox1.Select()
End Sub
If multiple controls are calling this method, you can use the sender parameter:
DirectCast(sender, Control).Select()

error on cell value change event in vbnet datagridview

I simply added message box and when i run the form the message box keeps popping up until all the rows have finish loaded n gridview.
My initial plan was to show a message that you have edited the cell. So does cell change event happens when the gridview is being loaded and extracting the rows from the database or when you change the value of cell.
How to stop the message box from popping out countless of times and whether or not i am using the wrong event?? Below is exactly what i did. I am also using datasource to get my records from the database
Private Sub grdDataGrid_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles grdDataGrid.CellValueChanged
MsgBox("You have edited the follwing cell")
End Sub
To stop the messageBox from popping out countless times:
Private Sub grdDataGrid_DataBindingComplete(sender As Object, e As System.Windows.Forms.DataGridViewBindingCompleteEventArgs) Handles grdDataGrid.DataBindingComplete
MessageBox("your message")
End Sub
When you use the _CellValueChanged event- all of the cells that are being loaded with data are being changed when you render the gridView. So each cell is firing the _CellValueChanged event

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

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

Showing List of combobox while getting focus (vb.net)

When we click on drop down combobox control in our windows form, it shows the list automatically.
But when we press tab and navigate to that control from keyboard it doesn't shows the list automatically. So in other to show the list automatically on receiving focus what should be done?
Set the DroppedDown property of the combobox equal to true.
Private Sub myComboBox_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles myComboBox.GotFocus
myComboBox.DroppedDown= true
End Sub
I would like to mention on thing here.
I used Enter event to show Drop down list with DroppedDown=true,
But When I type something in text area of combobox and if i leave the area to next control, entered text is lost.
My combobox is databound.