Deselect an Item in a Listbox when clicked in empty space - vb.net

So I am working on a software at the moment and when you click the load button to show files in a folder I want to make it where when you click in the empty white space to deselect the item. Below is the code I am using and I can't seem to get anywhere it still keeps the item selected.
Private Sub Listbox1_MouseUp(sender As Object, e As MouseEventArgs) Handles ListBox1.MouseUp
If ListBox1.IndexFromPoint(e.Location) < 0 Then
' clear the data
End If
End Sub
Any help is appreciated!

Related

ListBox past edge of Panel

Here's photo of the form showing the listbox stopping at the edge of the panel when it should extend up past it. I have a project with several panels and a datagridview. I also have some textboxes and buttons. when I click on one of the textboxes, I have a listbox set to be shown and and rezise and locate to just above the textbox but the size I want it to expand to goes beyond the panel and datagrid and that part is not visible.
I've set it to "bring to front" but that doesn't solve the problem
Private Sub txtPayee_Click(sender As Object, e As EventArgs) Handles txtPayee.Click
ListPayee.Show()
ListPayee.BringToFront()
ListPayee.Left = txtPayee.Left
ListPayee.Height = 200
ListPayee.Width = txtPayee.Width
ListPayee.Top = txtPayee.Top - ListPayee.Height
End Sub

Show ContextMenuStripItem without clicking off the current cell

I'm using an UltraGrid which has a ContextMenuStrip with 2 items. These are shown when right-clicking an UltraGridCell.
However, in order to show them, the user has to first click off the cell to take it out of edit mode, then right click on it to show the ContextMenuStripItems.
This has become confusing and irritating to the user, so I was wondering if there is any way that it can be changed to show them when right clicking whilst still in edit mode?
I've tried this to take it out of edit mode after a key is pressed, but it doesn't work.
Private Sub ugComm_keyup(sender As Object, e As KeyEventArgs) Handles ugComm.KeyUp
ugComm.UpdateData()
If ugComm.ActiveCell.IsInEditMode = True Then
ugComm.ActiveCell.Row.Update()
End If
End Sub
I also tried something in the MouseClick that was suggested on the Infragistics forums, but again it didn't work.
Is there any way that a user right-clicking a cell that is in edit mode can bring up the ContextMenuStripItems rather than this menu?
The above image shows what is currently show when right-clicking a cell in edit mode (The cell is the bottom right white cell). I don't want this to appear, but the CMS instead.
EDIT
I've tried the suggestions in the current answers, but neither of those worked for me. Possibly because the grids are a slightly older version?
My most recent effort was done with the following code:
Private Sub ugComm_MouseDown(sender As Object, e As MouseEventArgs) Handles ugComm.MouseDown
If e.Button = Windows.Forms.MouseButtons.Right Then
Me.cmCommRate.Show(mouseX, mouseY)
End If
End Sub
But this wasn't triggered until the cell was no longer in edit mode.
NEITHER OF THE ANSWERS BELOW RESOLVE THE ISSUE. STILL NEEDS AN ANSWER
When any cell of the grid enters in edit mode a TextBox is drawn over the cell. The nice part here is this text box is reused for all the cells in the grid. When you right click on the cell in edit mode the default context menu, that comes from MS, shows. What you need to do is get this text box and assign it your context menu strip. You can do this by handling ControlAdded event of the grid like this:
' create a field to store the TextBox
Private cellTextBox As TextBox
Private Sub grid_ControlAdded(sender As Object, e As ControlEventArgs) Handles grid.ControlAdded
' Check if added control is TextBox
If TypeOf e.Control Is TextBox Then
' If added control is TextBox store it in your private field and set its ContextMenuStrip
If Me.cellTextBox Is Nothing Then
Me.cellTextBox = DirectCast(e.Control, TextBox)
Me.cellTextBox.ContextMenuStrip = Me.ctx
End If
End If
End Sub
I have tried to write an event handler for the MouseUp event with this code
Private Sub grid_MouseUp(sender As Object, e as MouseEventArgs) Handles grid.KeyUp
grid.PerformAction(UltraGridAction.ExitEditMode)
grid.ContextMenuStrip.Show()
End Sub
and it works.
The ContextMenuStrip was added in code with this text (as example)
ContextMenuStrip ctx = new ContextMenuStrip()
ctx.Items.Add("Test1")
ctx.Items.Add("Test2")
ctx.Items.Add("Test3")
grid.ContextMenuStrip = ctx

Are ListviewItems ever truely deselected?

I have a listview with several items in it. If I have an item selected and click in an empty white space or anywhere else on the form the highlight is removed but .FocusedItem & .SelectedItems still report an item is selected.
I have events that I want to trigger when no listviewitems are selected, but that never seems to occur. How do I detect if no items are selected or does that ever actually happen after that first item gets clicked?
Private Sub lstCats_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstCats.SelectedIndexChanged
If IsNothing(lstCats.FocusedItem) Then
DisableGUI()
Else
EnableGUI()
DisplayQuestions()
End If
End Sub
Basically DisableGUI() will never execute.
Thanks!
You can deselect all items in the ListView by clicking on an empty part of the ListView control (that is not on one of the items). For example:
Private Sub lstCats_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstCats.SelectedIndexChanged
If lstCats.SelectedItems.Count = 0 Then
DisableGUI()
Else
EnableGUI()
DisplayQuestions()
End If
End Sub
Note if you click on an item in the ListView, SelectedItems will be > 0. If you click on the background of the ListView (not on an item), SelectedItems will be = 0. This assumes that there is part of the ListView control that does not contain an item.

how to get Listview Selected Item

I have a lisview that I have created that loads information into the program from selected files. I am now trying to add in a search button to the information typed in so that it highlights (selects) the line of code that information is found in (Entire line for both columns) Multiselect is turned on.
The part I am stuck with is if it was a normal listbox I would use selectedindex but with Listview this method does not seem to work and only has selectedIndexCollection or selected items.
If I try something like these they dont seem to work and want more information added
List.SelectedIndexCollection = Lines.text
List.SelectedItems = Lines.text
Edit from Comments:
Private Sub Search_Click(sender As System.Object, e As System.EventArgs)
Handles Search.Click
Do Until List.FocusedItem.text = lines.text
List.FocusedItem = List.Items(0) +1
Loop
End Sub

VB.net modify a textfile by using listbox and textbox

I want to be able to edit/delete/insert lines of text from a textfile by using listbox and textbox.
I want to display all the contents of the textfile per line in a listbox and when I click a line of text it will then display it in the textbox giving me the option to either edit or delete that line of text.
My insertion of text would be inserted after the last line of text being displayed in the listbox. Is this possible? I just want a starting point and I will continue it from there. Thanks in advance.
Here is an answer with a listview , button 9 is to fill Listview, listview click sends the text to textbox and button 10 saves it back to the listview
This is probably the simplest way to do what you want to achieve.
Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
ListView1.HeaderStyle = ColumnHeaderStyle.None
ListView1.HideSelection = False
For i As Integer = 0 To 50
ListView1.Items.Add("Line number" & i.ToString)
Next
End Sub
Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.Click
TextBox8.Text = ListView1.SelectedItems(0).Text
End Sub
Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
ListView1.SelectedItems(0).Text = TextBox8.Text
End Sub
Probably a good starting point for you anyway and expand this code from here on.
Yes. It is possible. I would suggest to use already existing text editors rather than re-inventing the wheel. If you still want to go ahead and create a new one from start, then you can try the following.
Create a window form application in vb.net with ListBox control for showing the lines, a textbox control for entering the file name, a button to browse to the given file, a button on clicking on which the file content should get loaded. Refer file objects for this.
Utilise ContextMenu class in vb.net to allow right click to read selected listbox line and accordingly perform add/delete/edit operation by modifying the selected listitem value.