how i can create selected_index_changed event of combobox in datagridview - vb.net

my datagridview name is "DG" and i add combobox column programatically named item as shown in code below.i want to create the event which call on itemchanged of combobox.i use DG_CellLeave event but it not call after the item selection immediatly but call when we leave the cell.i want to create event which immediatly call on selection change event of combobox.
Dim item As New DataGridViewComboBoxColumn
item.DataSource = dset.Tables("tab")
item.HeaderText = "item"
item.Name = "item"
item.DisplayMember = "p_name"
item.DataPropertyName = "item"
DG.Columns.Add(item)
which event should i choose for this purpose...

You should take a look at: DataGridView.EditingControlShowing Event. This event is raised whenever an edit control is shown in the DataGridView. It can be handled/used like below:
Dim gridComboBox As ComboBox
Private Sub DG_EditControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs)
' Check to see if the ColumnIndex is where we expect to have the DropDown
If (DG.CurrentCell.ColumnIndex = 1) Then
' Get the ComboBox that is being shown
gridComboBox = TryCast(e.Control, ComboBox)
If Not (gridComboBox Is Nothing) Then
' Always remove the Event Handler before Adding, when setting them at runtime
RemoveHandler gridComboBox.SelectedIndexChanged, AddressOf gridComboBox_SelectedIndexChanged
AddHandler gridComboBox.SelectedIndexChanged, AddressOf gridComboBox_SelectedIndexChanged
End If
End If
End Sub
Private Sub gridComboBox_SelectedIndexChanged(sender As Object, e As EventArgs)
Dim dropDown As ComboBox = TryCast(sender, ComboBox)
if not(dropDown is nothing) then
Dim drv as DataRowView = dropDown.SelectedItem
if Not (drv is nothing) then
Debug.Print(drv("item"))
end if
end if
End Sub
The SelectedIndexChanged event is raised as per a ComboBox used outside of a DataGridView.

Related

vb.net show tooltiptext for every item in datagridviewcombobox

I have searched for displaying tooltiptext for datagridview but I'm only getting tooltiptext for datagridviewcell. What I want is the tooltiptext to display when an item is highlighted (mouse hover) from the dropdown list of a datagridviewcomboboxcolumn.
I have set the tooltiptext in databinding of the comboboxcolumn but it doesn't display anything in runtime.
'assuming dtExpense is the datatable used as datasource
With CType(DataGridView3.Columns(2), DataGridViewComboBoxColumn)
.AutoComplete = True
.DataSource = dtExpense
.DisplayMember = "acct_title"
.ValueMember = "acct_id"
.DataPropertyName = "acct_id"
.ToolTipText = "description"
End With
Can anyone tell me how to do this. In datagriviewcell.tooltiptext it has to drawn at some point. I was thinking how to do this with datagridviewcomboboxcolumn and it has to display for each item in the combobox.
Assuming you have an object class with string properties named acct_title (to display as the dropdown items) and description (to display as tooltips on those dropdown items), you'll want to:
Add a ToolTip control to your Form.
Handle the EditingControlShowing event for the DataGridView to add event handlers to the underlying ComboBox.
Me.DataGridView3.EditingControlShowing += DataGridView3_EditingControlShowing
Private Sub DataGridView3_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs)
If TypeOf e.Control Is ComboBox Then
Dim cb As ComboBox = TryCast(e.Control, ComboBox)
cb.DrawMode = DrawMode.OwnerDrawFixed
cb.DrawItem -= Cb_DrawItem
cb.DrawItem += Cb_DrawItem
cb.DropDownClosed -= Cb_DropDownClosed
cb.DropDownClosed += Cb_DropDownClosed
End If
End Sub
Handle the ComboBox DrawItem event to set the ToolTip value and show it. We will use reflection to grab the description property off the dropdown items and set it's string value to the ToolTip.
Private Sub Cb_DrawItem(sender As Object, e As DrawItemEventArgs)
Dim cb As ComboBox = TryCast(sender, ComboBox)
Dim item = cb.Items(e.Index)
Dim display As String = cb.GetItemText(item)
Dim toolText As String = item.[GetType]().GetProperty("description").GetValue(item, Nothing).ToString()
e.DrawBackground()
Using br As New SolidBrush(e.ForeColor)
e.Graphics.DrawString(display, e.Font, br, e.Bounds)
End Using
If (e.State And DrawItemState.Focus) = DrawItemState.Focus AndAlso cb.DroppedDown Then
Me.toolTip1.Show(toolText, cb, e.Bounds.Right, e.Bounds.Bottom, 2000)
End If
e.DrawFocusRectangle()
End Sub
Handle the ComboBox DropDownClosed event to hide the ToolTip.
Private Sub Cb_DropDownClosed(sender As Object, e As EventArgs)
Dim cb As ComboBox = TryCast(sender, ComboBox)
Me.toolTip1.Hide(cb)
End Sub

SpreadSheetGear Right Click Menu Edit in Vb.Net

Can I: Edit the right click menu of selected cells of Sheet in SpreatSheetGear to add an option like Merge, and then handle the event of selection of that menu item click?
Thanks in Advance for sharing some idea.
You ought to just be able add a new ToolStripItem to WorkbookView.ContextMenuStrip (the ContextMenuStrip property is inherited from the Control class):
' Create and add new item to WorkbookView's context menu
Dim newItem As ToolStripItem = workbookView.ContextMenuStrip.Items.Add("Merge Cells")
' Add event handler
AddHandler newItem.Click, AddressOf MenuItemMergeCells_Click
...
Private Sub MenuItemMergeCells_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim item As ToolStripItem = CType(sender, ToolStripItem)
If item.Text = "Merge Cells" Then
workbookView.GetLock()
Try
' Merging is only valid for multi-cell ranges
If workbookView.RangeSelection.CellCount >= 2 Then
workbookView.RangeSelection.Merge()
End If
Finally
workbookView.ReleaseLock()
End Try
End If
End Sub

How to notice any change on a form

I have a form with many different radiobuttons, checkboxes and textboxes. Depending on their values I start my calculations. The results are shown on the same form and panel. If any of my controls (checkboxes, ...) changes, I want to immediatly update the results without a need to press any update-button.
I could define a statsChanged-sub for every single control on the form but there are so many. Isn't there a way/event of the form starting whenever a control is changed? It should be something like controlOnFormChanged. How can I get a sub that starts whenever a any control on the form changed?
Thank you in advance!
You could wire up the events that correspond to the desired change manually to a specific event handler:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Iterate through all controls and handle them according to their type
For Each c As Control In Me.Controls
If TypeOf (c) Is CheckBox Then
AddHandler CType(c, CheckBox).CheckedChanged, AddressOf SomethingChanged
ElseIf TypeOf (c) Is RadioButton Then
AddHandler CType(c, RadioButton).CheckedChanged, AddressOf SomethingChanged
ElseIf TypeOf (c) Is TextBox Then
AddHandler CType(c, TextBox).TextChanged, AddressOf SomethingChanged
ElseIf ......
......
End If
Next
End Sub
Private Sub SomethingChanged(sender As Object, e As EventArgs)
'Whatever it is you do
End Sub
End Class
Whenever one of the events on a control fires the sub SomethingChanged is called, allowing you to update your results.
Please be aware: If you have controls in subcontainers like Panels you need to modify this method and iteratively get all controls in all containers.
Here is, for example, a solution to this:
http://kon-phum.com/tutors/pascal/programming_cs_getcontrolsonform.html
Public Shared Function GetAllControls(ctrls As IList) As List(Of Control)
Dim RetCtrls As New List(Of Control)()
For Each ctl As Control In ctrls
RetCtrls.Add(ctl)
Dim SubCtrls As List(Of Control) = GetAllControls(ctl.Controls)
RetCtrls.AddRange(SubCtrls)
Next
Return RetCtrls
End Function

VB.NET ComboBox results selected item

Hi I have a vb windows form application that has a ComboBox from the form1 I have some code that reads some registry and adds item results to combobox. I would like to select one of the results and run a start process. My problem is where do I put the code when item is selected then do something and how to I determine what has been selected?
My Code to query registry keys
Dim Key, Reader As RegistryKey, Y As String
Key = Registry.LocalMachine.OpenSubKey("SOFTWARE\AppStream\AppMgr\Shortcuts", False)
For Each X In Key.GetSubKeyNames
Reader = Registry.LocalMachine.OpenSubKey("SOFTWARE\AppStream\AppMgr\Shortcuts\" & X, False)
If Reader.GetValueNames().Contains("AppTitle") Then
Y = Reader.GetValue("AppTitle")
If Not ComboBox1.Items.Contains(Y) Then ComboBox1.Items.Add(Y)
End If
If i do somehting like this, it just shows a blank messagebox and I have not selected that text from combobox yet.
If ComboBox1.SelectedText Then
MessageBox.Show(ComboBox1.SelectedText())
End If
You subscribe to the SelectedIndexChanged event writing a method like this
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim comboBox As comboBox = CType(sender, comboBox)
' Caution, the event could be called also when there is nothing selected
if combBox.SelectedItem IsNot Nothing Then
Dim curValue = CType(combBox.SelectedItem, String)
'do your stuff with the selected key'
End If
End Sub
if combBox.SelectedItem IsNot Nothing Then
Dim cmbselected As String = DirectCast(DirectCast(DirectCast(DirectCast(combBox, System.Windows.Controls.ComboBox).SelectedValue, System.Object), System.Data.DataRowView).Row, System.Data.DataRow).ItemArray(0)
End If

vb.net programatically add link button to datagrid -- click event not firing

I have a datagrid where I am programatically adding a linkbutton on ItemDataBound.
Protected Sub dgCounts_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgCounts.ItemDataBound
For i As Integer = 1 To (e.Item.Cells.Count - 1)
Dim lb As New LinkButton
lb.CommandArgument = aryDealers(i)
lb.Text = e.Item.Cells(i).Text
lb.CausesValidation = False
AddHandler lb.Click, AddressOf lb_Click
If e.Item.Cells(i).Text.Trim.Length > 0 Then
e.Item.Cells(i).Controls.Add(lb)
End If
Next
End Sub
Protected Sub lb_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim lb As LinkButton = CType(sender, LinkButton)
Dim s As String = lb.CommandArgument
End Sub
The linkbutton is adding to the grid cells correctly, but when you the click event is not firing.
Thoughts?
Thanks!
You should not add controls dynamically in the DataGrid's ItemDataBound Event (or GridView's RowDataBound-Event). ItemDataBound is only triggered when you databind the DataGrid to it's DataSource. If you've enabled ViewState and databind it only if Not Page.IsPostback, the control would not be recreated on postbacks. Therefore no events are triggered.
You should use ItemCreated instead(RowCreated in GridView) to create controls dynamically, because ItemCreated is called on every postback.