SpreadSheetGear Right Click Menu Edit in Vb.Net - 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

Related

Add click events to ToolStripTextBox in VB.NET when created in code

I am creating tabs to keep track of open files. When I create them I can set their properties and add them to the list in a MenuStrip.
Before I do so I want to add a click event function. Since this is in a function and will be run multiple times, I need to have a dynamic way to do add these event handlers. I cannot write this:
Private Sub ToolStripTextBox1_Click(sender As Object, e As EventArgs) Handles ToolStripTextBox1.Click
' ...
End Sub
If I do this I can only name them all one name. I want to be able to add a click event that applies to them separately as individual items.
UPDATE:
Dim textFile As ToolStripTextBox = New ToolStripTextBox("textFile")
FileList.Items.Add(textFile)
textFile.Text = filename
textFile.ReadOnly = True
textFile.BackColor = Color.FromArgb(61, 61, 61)
textFile.ForeColor = Color.White
This is the creation and personalization code. Though when you guys suggested AddButton_Click() Handles AddButton.Click It doesn't work because AddButton isn't an actual button
You can keep track of the items, add add and remove click handlers with the following code
Private FileList As New List(Of ToolStripItem)()
' put this code where you add an item
Dim filename = "whatever"
Dim textFile As ToolStripTextBox = New ToolStripTextBox(filename)
textFile.Text = filename
textFile.ReadOnly = True
textFile.BackColor = Color.FromArgb(61, 61, 61)
textFile.ForeColor = Color.White
add(textFile)
Private Sub clickHandler(sender As Object, e As EventArgs)
Dim item = DirectCast(sender, ToolStripItem)
MessageBox.Show(item.Text)
End Sub
Private Sub add(textFile As ToolStripItem)
FileList.Add(textFile) ' Since I don't know what FileList is, I ued List
AddHandler textFile.Click, AddressOf clickHandler
OpenProjectToolStripMenuItem.DropDownItems.Add(textFile) ' add to whatever menu item you normally do
End Sub
Private Sub remove(textFile As ToolStripItem)
FileList.Remove(textFile)
RemoveHandler textFile.Click, AddressOf clickHandler
OpenProjectToolStripMenuItem.DropDownItems.Remove(textFile)
End Sub
The sample click event in your code includes this:
sender As Object
You can re-use this one event handler, because whichever menu item is clicked will be the sender. So you do something like this:
Private Sub ToolStripTextBox1_Click(sender As Object, e As EventArgs) Handles ToolStripTextBox1.Click
Dim menuItem = DirectCast(sender, ToolStripMenuItem)
If menuItem Is Nothing Then Return
'If you need to choose different code for each item:
Select Case menuItem.name
Case "some item"
Case "some other item"
End Select
End Sub
And you can connect your dynamic toolstrip items to this method using AddHandler:
AddHandler myMenuStrip.Click, AddressOf ToolSTripTextBox1_Click

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.

Disable few menus upon opening when item is selected in listbox

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)

How to manage dynamically created controls in VB.NET?

I just understand how to make controls dynamically in VB.NET (I mean, only part of adding a new one)
But, unlike VB6, it seems hard to handle those dynamic things.
When I click the DONE button, I want to make an array filled with the text of textboxes.
At the same time, I want to make a Delete button that removes the button itself and the textbox in the same line.
Is there any simple method or an sample code for this?
Thank you!
Drop a TableLayoutPanel on your form, called pnlLayout, and also the Add button called btnAdd. Configure TableLayoutPanel to have two columns, adjust column width as needed.
Paste below code into your form:
Public Class Form1
Dim deleteButtons As List(Of Button)
Dim textBoxes As List(Of TextBox)
Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
deleteButtons = New List(Of Button)
textBoxes = New List(Of TextBox)
End Sub
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim elementCount As Integer = deleteButtons.Count
Dim txt As New TextBox
txt.Width = 100
txt.Height = 20
textBoxes.Add(txt)
Dim btn As New Button
btn.Width = 100
btn.Height = 20
btn.Text = "Delete " & elementCount.ToString
AddHandler btn.Click, AddressOf btnDelete
deleteButtons.Add(btn)
pnlLayout.SetCellPosition(txt, New TableLayoutPanelCellPosition(0, elementCount))
pnlLayout.SetCellPosition(btn, New TableLayoutPanelCellPosition(1, elementCount))
pnlLayout.Controls.Add(btn)
pnlLayout.Controls.Add(txt)
End Sub
Private Sub btnDelete(sender As Object, e As EventArgs)
Dim senderButton As Button = DirectCast(sender, Button)
Dim txt As TextBox = textBoxes(deleteButtons.IndexOf(senderButton))
pnlLayout.Controls.Remove(senderButton)
pnlLayout.Controls.Remove(txt)
End Sub
End Class
By default, it will have no textboxes and no Delete buttons, you can add as many rows of "Textbox + Delete button" as you want. When you press Delete, the row will be removed (and everything shifted to accommodate the empty space).
For the textbox'ex part:
Dim strcol() As String = {TextBox2.Text, TextBox3.Text}
For Each strtxt In strcol
MessageBox.Show(strtxt)
Next
It really depends on your code, but, if you have their name use this to delete the buttons &/ textbox'es:
For i As Integer = Me.Controls.Count - 1 To 0 Step -1
If TypeOf Me.Controls(i) Is TextBox Then
If Me.Controls(i).Name = "TextBox2" Then
Me.Controls.RemoveAt(i)
End If
End If
If TypeOf Me.Controls(i) Is Button Then
If Me.Controls(i).Name = "Button3" Then
Me.Controls.RemoveAt(i)
End If
End If
Next
But it depends on your code...

How to use dynamically added toolstripmenuitem

I'm trying to make a tool strip item that contains bookmarks and each bookmark should go to the page. How do make each button work?.
For Each b In New System.IO.DirectoryInfo("Bookmarks").GetFiles
BookmarksToolStripMenuItem.DropDownItems.Add(b.Name)
Next
You should first create a too ToolStripMenuItem, then add handlers and put it to your toolstripmenu object instead of adding to toolstripmenu a string object.
For Each b In New System.IO.DirectoryInfo("Bookmarks").GetFiles
Dim menuItem As New ToolStripMenuItem(b.Name)
'Add any handlers here
'Click handler to your menuItem.
AddHandler menuItem.Click, AddressOf menuItem_Click 'CLICK EVENT HANDLER ALSO UNIQUE
'Add menuItem to ToolStripMenu
BookmarksToolStripMenuItem.DropDownItems.Add(menuItem)
Next
Private Sub menuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
'CODE TO HANDLE CLICK EVENT
End Sub
If you don't know how to dynamicaly add handlers then take a look at examples.