Disable few menus upon opening when item is selected in listbox - vb.net

I want to disable few item in a contextmenu or menustrip when an item in listbox is selected, I manage to do it in a listview control but I can't convert it to listbox, there is no ListItem in listbox control, can i do it using this code, or perhaps an entirely different code, thanks for helping..
Private Sub cIconList_Opening(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles cIconList.Opening
Dim item As ListViewItem = Nothing
If lvIcon.SelectedIndices.Count = 1 Then
item = TryCast(lvIcon.SelectedItems(0), ListViewItem)
End If
mnuExtractIcon.Enabled = (item IsNot Nothing)
mnuIconProperties.Enabled = (item IsNot Nothing)
End Sub

If you are trying to disable another form object when any ListBox item is selected, just add this to the ListBox's SelectedIndexChanged event.
AnyFormObject.Enabled = (ListBoxObject.SelectedIndex = -1)

Related

how i can create selected_index_changed event of combobox in datagridview

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.

Change the tooltip text when selecting item in listview

I have a listview containing the source addresses of files (from user system) and the destination addresses (two columns, multilpe selection = false).
Since the source addresses might be quite long like:
d:\root\branch1\branch2\branch3\branch4\myfile.dat
the first column shows just:
d:\ ... \myfile.dat
The real path is stored in the ListViewItem.Tag
I want to have a tooltip showing the whole path every time the user clicks (or changes) the selected item. I came out with this:
Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged
For Each locItem As ListViewItem In ListView1.SelectedItems
With ToolTip1
.RemoveAll()
.SetToolTip(ListView1, locItem.Tag)
End With
Next
End Sub
Now, the tooltip does change, but it always skip one selection. That is:
selecting item 1: tooltip shows correctly
selecting item 2: tooltip don't show
selecting item 3: tooltip shows correctly
selecting item 5: tooltip don't show
selecting item 2: tooltip shows correctly (selection went back to item 2 which didn't show the 1st time)
Any idea?
PS: I am using Visual Studio Community 2015
PPS: I also need to have the View property set to View.Details (to show both columns and headers), so setting ShowItemToolTip = True does not work
CURRENT SOLUTION
I found a workaround, destroying and recreating the tooltip control. Now the tooltip shows correctly on every item:
Private myTooltip As ToolTip
Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged
If myTooltip IsNot Nothing Then myTooltip.Dispose()
myTooltip = New ToolTip
For Each locItem As ListViewItem In ListView1.SelectedItems
myTooltip.SetToolTip(ListView1, locItem.Tag)
Next
End Sub
I'm still baffled about the skipping in the first approach.
ListView has a property called ShowItemToolTips (https://msdn.microsoft.com/en-us/library/system.windows.forms.listview.showitemtooltips(v=vs.110).aspx). You can set that to true and then set the ToolTipText property of the ListViewItems to the long path.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
ListView1.ShowItemToolTips = True
Dim lvi1 As New ListViewItem With {.Text = "d:\..\myFile.dat", .Tag = "d:\myLongPath\myFile.dat", .ToolTipText = .Tag}
Dim lvi2 As New ListViewItem With {.Text = "d:\..\myFile2.dat", .Tag = "d:\myLongPath\myFile2.dat", .ToolTipText = .Tag}
ListView1.Items.Add(lvi1)
ListView1.Items.Add(lvi2)
End Sub

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

winforms menu as array

I know it most likely possible to access a winforms menu like an array but I am not seeing it in the menu designer of VS2008? What I mean is, my app has a typical menu bar across the top, with multiple items on each drop down. As it is written (I inherited this code), each menu item is a separate name, i.e.
myMenuOption1MenuItem
myMenuOption2MenuItem
myMenuOption3MenuItem
myMenuOption4MenuItem
etc...
I need the ability to enable or disable (or even hide/unhide) menu options depending on user privileges, for example:
For I = 0 to maxIndex
myMenuOption(I).Enabled = myUser.IsAdministrator
Next
Obviously I could set/unset each menu item by name, but for a lot of reasons I'd prefer to use loops.
ToolStripMenuItems are exposed through ContextMenuStrip.Items
For Each myItem As ToolStripMenuItem In myContextMenuStrip.Items
myItem.Enabled = myUser.IsAdministrator
Next
Each child menu item is in the DropDownItems collection - So you can loop through that (If you are using ToolStripMenuItems and not the older style menus).
For Each Menu As ToolStripMenuItem In MenuPrincipal.Items
For Each Item As ToolStripItem In Menu.DropDownItems
If TypeOf (Item) Is ToolStripMenuItem Then
AddHandler Item.Click, AddressOf Menu_OnClick
End If
Next
Next
This way you can access each of the events as follows:
Private Sub Menu_OnClick(ByVal sender As Object, ByVal e As System.EventArgs)
Dim Frm As Form = GetFormByName(sender.name)
If sender.Tag = "Modal" Then
Frm.ShowDialog(Me)
ElseIf sender.Tag = "Modeless" Then
Frm.Show(Me)
Else
OpenForm(Frm, True)
End If
End Sub