How to handle both selected index change of combobox column and keypress event of text box column in datagridviw editingcontrolshowing event? - vb.net

I have a datagridview in winform, in which four columns exists.Out of which 1 comboxcolumn and other three textboxcolumn. I have handeled combobox selected index change properly and it working fine. I handle this on Datagridview_editigControlShowing() event. Now i want to handle other three textboxes keyprees event so user can only enter numeric value. Now i searched on net there are some solution, but when i add another handler in editingControlShowing() method at run time when i click on combobox, it shows error.But when i remove the addHandler for textbox its worked fine.
My Code is look like
Private Sub dgvPurchase_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles dgvPurchase.EditingControlShowing
Dim editComboBox As ComboBox = TryCast(e.Control, ComboBox)
If Not editComboBox Is Nothing Then
AddHandler editComboBox.SelectedIndexChanged, AddressOf editComboBox_SelectedIndexChanged
End If
'RemoveHandler dgvPurchase.EditingControlShowing, AddressOf dgvPurchase_EditingControlShowing
End Sub
Now is there any way to handle both of these if so please provide me the solution

Related

Have multiple ComboBox's use the same event

I have roughly 140 ComboBox on a form. When some selects a new item within one of the box's I would like to highlight that box.
I would like to use the SelectionChangeCommitted event and I would use the following code:
Private Sub cmbDesk1_SelectionChangeCommitted(sender As System.Object, e As System.EventArgs) Handles cmbDesk1.SelectionChangeCommitted
Dim tbControl As ComboBox = DirectCast(sender, ComboBox)
tbControl.BackColor = Drawing.Color.Red
tabFloor1.Focus()
I know that I can add extra lines after the "Handles" section of the subroutine declaration but it there a neat way of doing it without adding every ComboBox to it?
Something similar to
Me.TabPage1.Controls.OfType(Of ComboBox)()
Kind regards
Matt
You can always loop all combobox and AddHandler yourself.
For Each cb As ComboBox In Me.TabPage1.Controls.OfType(Of ComboBox)()
AddHandler cb.SelectionChangeCommitted, AddressOf cmbDesk1_SelectionChangeCommitted
Next
This should be done once.

VB.Net Datagridview combobox add a handler

I've spent a day trying to figure out how to solve my problem, which is identical to
this related unanswered question
On the first combobox, my code works fine, no problem. When I try to change the combobox it throws an error Null Reference Exception
This is my code:
Private Sub dgvSurveyQuestions_EditingControlShowing(ByVal sender As Object, ByVal e As DataGridViewEditingControlShowingEventArgs) Handles dgvSurveyQuestions.EditingControlShowing
Dim editingComboBox As ComboBox = TryCast(e.Control, ComboBox)
If Not editingComboBox Is Nothing Then
'Add the handle to your IndexChanged Event
RemoveHandler editingComboBox.SelectedIndexChanged, AddressOf editingComboBox_SelectedIndexChanged
AddHandler editingComboBox.SelectedIndexChanged, AddressOf editingComboBox_SelectedIndexChanged
End If
'Prevent this event from firing twice, as is normally the case.
'RemoveHandler dgvSurveyQuestions.EditingControlShowing, AddressOf dgvSurveyQuestions_EditingControlShowing
End Sub
Private Sub editingComboBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Dim editingComboBox As ComboBox = TryCast(sender, ComboBox)
If editingComboBox Is Nothing Then Exit Sub
'Show your Message Boxes
MessageBox.Show(editingComboBox.SelectedValue.ToString) ' throws error here
End Sub
I'm still working on a different workaround, like adding a handler while the datagridview is populated or whatever.. I don't even know if this is possible but I need to do this.
I'm really stuck here, can someone shed some light and advice me on what to do? Thanks
Simply check if editingComboBox.SelectedValue is Nothing.
The DataGridView reuses only one ComboBox instance, and internally resets the DataSource of the ComboBox when the user selects another ComboBox cell.
Then the event SelectedIndexChanged raises and SelectedValue will be Nothing at that time.

vb.net remove rowsadded event on datagridviewcombobox selection

i create combobox column on dgv and enabled the .allowusertoaddrows properties.
but everytime i dropdown combobox column, it always add new line or rows on my datagridview..
i've explore stackoverflow and google, but haven't found the solution yet..
but i've come to understanding that everytime dgv combobox column were clicked, it will automatically trigger default event to add new row.
how can i remove this handler
i've got reference from this link, but it didn't worked out for me..
please give me the solution, thanks you.
here's what i've tried
Private Sub dgv_beli_EditingControlShowing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles dgv_beli.EditingControlShowing
If dgv_beli.CurrentCell.ColumnIndex = 3 AndAlso TypeOf e.Control Is ComboBox Then
Dim comboBox As ComboBox = TryCast(e.Control, ComboBox)
RemoveHandler dgv_beli.EditingControlShowing, AddressOf dgv_beli_EditingControlShowing
End If
End Sub

How to add a specific event handler to a unspecified control?

I'm looking to try and create something like the following:
If the user leaves the last textbox (for example let's say TextBox8), then a new textbox is created below textbox8 with the name textbox9. I have this part, but how would I make it so that if textbox9 is left, the same events happen, so on and so forth?
Private Sub TextBox8_LoseFocus(sender As Object, e As System.EventArgs) Handles TextBox8.LostFocus
' Textbox 9 creation code which then creates the next textbox etc.
End Sub
If anybody can offer a better way of doing this sort of thing
You can use AddHandler to add a eventhandler to a control you create at run time
AddHandler TextBox9.LostFocus, AddressOf TextBox8_LoseFocus

Selection Changed Event in DataGridComboBoxColumn

I have a Data grid with DatagridComboBoxColumn , and i want to Fire Event SelectionChanged when user Select any thing From the ComboBox , Do Some operations ,
how can i do that any advice ,
thanks
You can handle your DataGridView's EditingControlShowing event and cast the editing control to the ComboBox being displayed and then wire up its SelectionChangeCommitted event. Use the SelectionChangeCommitted handler do you what you need to do.
See the sample code in the MSDN article I linked for details.
Two important notes:
Despite the MSDN article's sample code it's best to use the
ComboBox SelectionChangeCommitted event, as discussed here and in the
comments of the linked MSDN article.
If you have more than one DatagridComboBoxColumn in your
DataGridView you might want to determine which fired either your
EditingControlShowing or the ComboBox's SelectionChangeCommitted
event. You can do this by checking your DGV
CurrentCell.ColumnIndex property value.
I reworked the MSDN sample code a bit to show what I mean:
Private Sub DataGridView1_EditingControlShowing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
' Only for a DatagridComboBoxColumn at ColumnIndex 1.
If DataGridView1.CurrentCell.ColumnIndex = 1 Then
Dim combo As ComboBox = CType(e.Control, ComboBox)
If (combo IsNot Nothing) Then
' Remove an existing event-handler, if present, to avoid
' adding multiple handlers when the editing control is reused.
RemoveHandler combo.SelectionChangeCommitted, New EventHandler(AddressOf ComboBox_SelectionChangeCommitted)
' Add the event handler.
AddHandler combo.SelectionChangeCommitted, New EventHandler(AddressOf ComboBox_SelectionChangeCommitted)
End If
End If
End Sub
Private Sub ComboBox_SelectionChangeCommitted(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim combo As ComboBox = CType(sender, ComboBox)
Console.WriteLine("Row: {0}, Value: {1}", DataGridView1.CurrentCell.RowIndex, combo.SelectedItem)
End Sub