ListView ToolTip only in First Cell - VB.NET - vb.net

I'm adding a ToolTip to a ListViewItem. However, the ToolTip only shows up when the user hovers over the first cell in the row to which the ToolTip has been applied.
MyListViewItem.ToolTipText = "Important Message"
The only other code I have related to ToolTips is this:
MyListView.ShowItemToolTips = True
Any idea how I can make the ToolTip show up on a specific cell in a row, or even the entire row? Thanks.

If you want a non-wrapper answer unlike the dup mentioned, try this:
The listview FullrowSelect property must be true. Next you need to store the tips for each subitem, I do this inside the subitem tag property. What you want to do, is on the listview mousemove event, you grab the item under the mouse, get it's subitem, and use that tip.
This simple example shows you how to get that subitem tooltip, you can just hack this a bit to suit your needs.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
lvw.ShowItemToolTips = True
lvw.Columns.Add("Column A")
lvw.Columns.Add("Column B")
lvw.Columns.Add("Column C")
lvw.Items.Add(New ListViewItem(New String() {"Colors", "Green", "Blue"}))
lvw.Items(0).SubItems(0).Tag = "See the other columns"
lvw.Items(0).SubItems(1).Tag = "Like grass"
lvw.Items(0).SubItems(2).Tag = "Like the sky"
End Sub
Function GetItemTip(ByVal list As ListView, ByVal e As System.Windows.Forms.MouseEventArgs) As String
Dim item As ListViewItem = list.GetItemAt(e.X, e.Y)
If Not IsNothing(item) Then
Dim si As ListViewItem.ListViewSubItem
si = item.GetSubItemAt(e.X, e.Y)
If Not IsNothing(si) Then
Return si.Tag.ToString
Else
Return ""
End If
Else
Return ""
End If
End Function
Private Sub lvw_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lvw.MouseMove
Me.Text = GetItemTip(CType(sender, ListView), e)
End Sub

Related

Multiple selection in datagridview

I work on a new project that need a multiple row selection/deselection by the user in a datagridview with only a tap on a touch screen.
The form should look like this:
For exemple, if the user want to delete row 2 and 5, he only need to tap once on each line to select/deselect them. After the selection is done, he tap on "Delete Row" button.
I've already try to play with the CellClick event without success!!
Can someone have a clue how can I handle this problem?
After setting MultiSelect property to True and SelectionMode to FullRowSelect you can use a List to store which row of your DataGridView is selected.
On CellClick you can add/remove rows from your List, on RowPostPaint you can select a row if it's included in the List and on RowsRemoved you have to clear the List.
Private intSelectedRows As New List(Of Integer)
Private Sub DataGridView1_CellClick(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
With CType(sender, DataGridView)
Dim intRow As Integer = .CurrentRow.Index
If Not Me.intSelectedRows.Contains(intRow) Then
Me.intSelectedRows.Add(intRow)
Else
.CurrentRow.Selected = False
Me.intSelectedRows.Remove(intRow)
End If
End With
End Sub
Private Sub DataGridView1_RowPostPaint(sender As Object, e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles DataGridView1.RowPostPaint
If Me.intSelectedRows.Contains(e.RowIndex) Then
CType(sender, DataGridView).Rows(e.RowIndex).Selected = True
End If
End Sub
Private Sub DataGridView1_RowsRemoved(sender As Object, e As System.Windows.Forms.DataGridViewRowsRemovedEventArgs) Handles DataGridView1.RowsRemoved
Me.intSelectedRows.Clear()
End Sub
If you want to clear selection you can use this code:
Private Sub btnClearSelectedRows_Click(sender As System.Object, e As System.EventArgs) Handles btnClearSelectedRows.Click
For Each intSelectedRow As Integer In Me.intSelectedRows
Me.DataGridView1.Rows(intSelectedRow).Selected = False
Next intSelectedRow
Me.intSelectedRows.Clear()
End Sub

Adding context_menu items dinamically with custom functions

I create context menu dinamically and want to assign menuitems to my own functions (with arguments). Unfortunatelly that dont go as I would like.
Following example illustrates what I would like to do.
Private Sub dgv_sub_CellMouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgv_sub.CellMouseUp
If e.Button = Windows.Forms.MouseButtons.Right Then
dgv_sub.Rows(e.RowIndex).Selected = True
context_sub.Items.Clear()
context_sub.Items.Add("Delete row " + dgv_sub.CurrentRow.Index.ToString, Nothing) AddressOf delRow(dgv_sub.CurrentRow.Index))
context_sub.Items.Add("Delete all rows", Nothing) , AddressOf delRow(-1))
context_sub.Show(New Point(Cursor.Position.X, Cursor.Position.Y))
End If
End Sub
Private Sub delRow(ByVal rowtodelete As Integer)
End Sub
How to make this properly and get it to work as described?
This is how I usually do these kind of things:
Have a pre populated ContextMenu
Assign the ContextMenu to my DataGridView
Add Events on every ToolStripMenuItem
In each event, first I do a check to make sure that a row has been selected
If dgv_sub.SelectedRows.Count > 0 Then
then, I get the correct row by using the
SelectedRows(0)
To make things neater, you can also use the DataGridView.MouseDown event to make sure that when the user right click a row, it gets selected.
Private Sub dgv_sub_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgv_sub.MouseDown
If e.Button = Windows.Forms.MouseButtons.Right Then
Dim hitTest As DataGridView.HitTestInfo
hitTest = dgv_sub.HitTest(e.X, e.Y)
If hitTest IsNot Nothing AndAlso hitTest.RowIndex > -1 Then
dgv_sub.CurrentCell = dgv_sub.Item(hitTest.ColumnIndex, hitTest.RowIndex)
dgv_sub.Rows(hitTest.RowIndex).Selected = True
End If
End If
End Sub
As you need context items to be dynamic, you will have to do these in the MouseDown event aswell.
In order to add an item properly you still need a normal click event:
context_sub.Items.Add("Name of Item", Nothing, AddressOf item_Click)
Then add a Sub like this:
Private Sub item_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
'Add any logic here, you can still use the dgv_sub.SelectedRows here
End Sub
Ideally you create a different Sub for every context menu item you need to add

How to change selected item text in list box at run time?

I tried with code like this:
Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles MyBase.Leave
' This way is not working
ListBox1.SelectedItem = TextBox1.Text
' This is not working too
ListBox1.Items(ListBox1.SelectedIndex) = TextBox1.Text
End Sub
The form is looked like this:
I need to change that list text while user typing in the text box. Is it possible to do that at run time?
You are using the form's leave event MyBase.Leave, so when it fires, it is useless to you.
Try using the TextChanged event of the TextBox instead.
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) _
Handles TextBox1.TextChanged
Make sure to check if an item is actually selected in the ListBox:
If ListBox1.SelectedIndex > -1 Then
ListBox1.Items(ListBox1.SelectedIndex) = TextBox1.Text
End If
Use Double click to select line (item) inside list box and change or modify.
Instead of using text box use ListBox1_MouseDoubleClick event
Private Sub ListBox1_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDoubleClick
then add this code inside this event
Dim intIndex As Integer = ListBox1.Items.IndexOf(ListBox1.SelectedItem)
Dim objInputBox As Object = InputBox("Change Item :","Edit", ListBox1.SelectedItem)
If Not objInputBox = Nothing Then
ListBox1.Items.Remove(ListBox1.SelectedItem)
ListBox1.Items.Insert(intIndex, objInputBox)
End If
OR
Dim objInputBox As Object = InputBox("Change Item :","Edit", ListBox1.SelectedItem)
If Not objInputBox = Nothing Then
ListBox1.Items(ListBox1.SelectedIndex) = objInputBox
End If

Extract text from clicked submenu item of Menustrip

I've been dealing and looking for a way of extracting the text of any given subitem when clicked and write the text in textbox1.
this is the code that I have so far, but it does not seem to work.
Private Sub MenuStrip1_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles MenuStrip1.ItemClicked
Dim result As String
If AccionAToolStripMenuItem.Checked = True Then
result = AccionAToolStripMenuItem.Text
TextBox1.Text = result
End If
End Sub
You are using the wrong event. The ItemClicked event works for the items on the menu. You need to add a event for each of the subitems:
Sub SomeToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SomeTSMenuItem.Click
TextBox1.Text = Ctype(sender, ToolStripMenuItem).Text
End Sub
You can make a function for each subitem or handle every event on the same function:
Handles item1.Click, item2.Click, item3.CLick
Try reading e.ClickedItem.Text.

ComboBox's SelectedIndexChanged event not being called on Enter

I'm working on VS 2010 with VB using .NET Framework 4.0
I have a combobox. It has some items in it and displays just fine. Here's where it gets a little weird:
If I click the drop-down arrow on the combobox and CLICK on the item I want, SelectedIndexChanged is called - good.
If I click inside the text area of the combobox and start typing what I want selected and finish it by pressing the up (or down) key, SelectedIndexChanged is called - also good.
If I click the drop-down arrow on the combobox and start typing what I want selected and finish it by pressing ENTER, SelectedIndexChanged is not called - PROBLEM.
Is there a different event that is caused by the ENTER in the last case? I've tried using the TextChanged and TextUpdate events, but those do not seem to be working:
Private Sub cmbStatus_TextChanged(sender As System.Object, e As System.EventArgs) Handles cmbStatus.TextChanged
If e.Equals(Keys.Enter) Then
Call SomeMethod()
End If
Should I use something besides e.Equals(Keys.Enter)?
Is there another event I should be looking for?
EDIT:
An example of the items in the ComboBox are:
10 - NEW ENTRY AND COMPLETENESS CHECK ---> this is the most common type
13 - ASSIGNED TO TRB/HRB ---> there are a few with '/'
60 - EXTERNAL (HOLD UNTIL FURTHER NOTICE) ---> there are a few with '(' and ')'
Basically, the type of each listing is "## - SOME TEXT".
Disclaimer: this is written in C# - let me know if you need it translated to VB.
private void comboBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
//It's important to also check that the Combo Box is displaying its Drop Down. If
//you want this to execute even when it is not displayed, remove the check for
//comboBox1.DroppedDown.
if (e.KeyCode == Keys.Enter && comboBox1.DroppedDown &&
!string.IsNullOrEmpty(comboBox1.Text))
{
int index;
//Attempt to locate the string typed in by the user. An index of -1
//means it was not found.
if ((index = comboBox1.FindStringExact(comboBox1.Text)) != -1)
{
//Update the SelectedIndex.
comboBox1.SelectedIndex = index;
}
}
}
Interestingly, the docs say that we should be using the SelectionChangeCommitted event instead of the SelectedIndexChanged event when handling selection changes made by the user. It is necessary to do so in this case as the SelectedIndexChanged event fires twice using my approach.
Edit:
To prevent the user from having to type the entire string, use the advice from Adi's answer: go to the properties of the combo box and set set AutoCompleteMode to SuggestAppend and AutoCompleteSource to ListItems - I actully used these settings when creating my answer, so it should work for you.
Option Strict On
Public Class Form1
Friend WithEvents ComboBox1 As New ComboBox With {.Parent = Me}
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
ComboBox1.Items.AddRange({"hello", "tes1ted", "word", "item", "tes2ted"})
ComboBox1.Text = ComboBox1.Items(0).ToString
End Sub
Private Sub ComboBox1_KeyUp(ByVal sender As Object, ByVal e As KeyEventArgs) Handles ComboBox1.KeyUp
'You can put this in the keydown event, or adapt it a small bit and put it in the keypress event
'putting it in the textchanged event is problematic and not recommended.
Dim OriginalText As String = ComboBox1.Text
If e.KeyCode = Keys.Enter Then
If ComboBox1.SelectionLength > 0 Then
ComboBox1.Text = ComboBox1.Text
ComboBox1.SelectionLength = 0
ComboBox1.SelectionStart = ComboBox1.Text.Length
End If
End If
If Not IsTextKey(e.KeyCode) Then Exit Sub
Dim Filter As String = ComboBox1.Text & "*"
If Filter.Length = 1 Then Exit Sub
For I = 0 To ComboBox1.Items.Count - 1
If LCase(ComboBox1.Items(I).ToString) Like LCase(Filter) Then
ComboBox1.SelectedItem = ComboBox1.Items(I)
ComboBox1.Select(OriginalText.Length, (ComboBox1.Text.Length - OriginalText.Length))
Exit Sub
End If
Next
End Sub
Function IsTextKey(ByVal Key As Integer) As Boolean
Select Case True
Case Key = Keys.Up : Return False
Case Key = Keys.Down : Return False
Case Key = Keys.Left : Return False
Case Key = Keys.Right : Return False
Case Key = Keys.Back : Return False
Case Key = Keys.Delete : Return False
Case Key = Keys.LWin : Return False
Case Key = Keys.RWin : Return False
'add whatever I missed
'return false if the key either removes text from the textbox
'or does not produce a character
Case Else
'return true if the key produces a visible character(including space)
Return True
End Select
End Function
End Class
Private Sub ComboBox1_KeyUp(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyUp
If e.KeyCode = Keys.Enter Then
MessageBox.Show(ComboBox1.SelectedText)
Call SomeMethod()
End If
End Sub
I believe that you should set AutoCompleteMode to SuggestAppend and AutoCompleteSource to ListItems. This way, if what you type in is sustainable with the items loaded in the ComboBox, by writing it down and finding that item, when pressed Enter, the SelectedIndexChanged will be fired (even if a match wouldn't be found - the first in the list will be selected)
I've prepared something to point out this to you.
Regards,
Adi Konstantin
Subscribe to the KeyPressed event:
Private Sub yourComboBox_KeyPressed(sender As System.Object, e As System.KeyPressedEventArgs) Handles yourComboBox.KeyPressed
If e.KeyChar.Equals((char)Keys.Enter) Then
Call SomeMethod()
End If
this will helps your problems
Private Sub ComboBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyDown
If e.KeyCode = Keys.Enter Then
MsgBox("hello")'call some functions
End If
End Sub
Can you let me know if this works for you?
Private Sub ComboBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress
If e.KeyChar = Chr(13) Then
ComboBox1_SelectedIndexChanged(sender, e)
End If
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
MsgBox(ComboBox1.Text)
'Your code here
End Sub
I had a similar problem when the combobox dropdownstyle was set to simple with autocomplete set to append, sourcing from my listitems. My workaround was to have a string variable save the combobox text upon each textchanged event. That way, when ENTER is pressed, you can retrieve the deleted text and reassign it to the combobox text.
'Global declaration
'Global declaraion
dim selected_text as string = ""
Private Sub combobox_textchanged(sender As Object, e As EventArgs) Handles combobox.TextChanged
If combobox.Text IsNot Nothing And combobox.Text <> "" Then
selected_text = combobox.Text
End If
End Sub
Private Sub combobox_keydown(sender As Object, e As KeyEventArgs) Handles combobox.KeyDown
If e.KeyCode = Keys.Enter Then
combobox.Text = selected_text
'put logic here
End If
End Sub
There are some great answers above, but I liked that I didn't need to sort out all the command keys and arrows etc.
Close the list manualy on PreviewKeyDown event:
Private Sub cmbStatus_PreviewKeyDown(sender As ComboBox, e As PreviewKeyDownEventArgs) Handles cmbStatus.PreviewKeyDown
If e.KeyCode = Keys.Enter Then sender.DroppedDown = False
End Sub