Arrow keys don't work after programmatically setting the selected item in a listview - vb.net

I have a listview with some things in it, and I have a sub that will refresh the listview by deleting everything in it and re-populating it.
However, when the user selects an item and the list refreshes, I wanted to have that same item select itself once more. I accomplished that by doing:
ListView.Items(4).Selected = True
ListView.Select()
which selects the 5th item in the list view (counting 0). However, when the user presses the up or down keys, the selected item jumps back up to the top item in the list, and I can't find a way around this.
The only search that has come up with anything is here:
Arrow keys don't work after programmatically setting ListView.SelectedItem
But I do not understand the responses, I need a dumbed down version or a simpler solution if possible, thank you.

Try using the Focused property on an Item. Seems like I may have run into this before.
ListView.Items(4).Focused = True
You may also need to set that same property to False for the first item in the list.

Related

Context menu to delete items from a ComboBox

Currently, I've designed my own combobox from a ListBox/Button/TextBox to accomplish this, but I'm finding this to be way to much work to try and get a hacked together ComboBox to look like an actual ComboBox control.
This does however give me the desired effect however. Sort of ...
But my question is, is there not a simpler way to do this? Is there a way to simply right click on an item in a ComboBox, and get a context menu that says "Delete" for that specific entry?
Here is an example of the desired effect with my hacked together ComboBox:
I've tried simply linking a ContextMenu to a ComboBox, but that only allows for right click on the actual ComboBox itself, not the drop down, which renders this useless to me.
Any help or advice would be greatly appreciated.

Items not showing in listbox

I've got a simple list box on my VB form and I'm adding strings to it using the standard lstBox.items.add("string") command. However, the items aren't actually showing up in the list box. If I iterate through the items collection I can see that the strings are being added. They just aren't visible in the list box. If I add items to said collection before compilation, using the IDE, they show up fine and I can delete them in the normal way. I have tried deleting and re-adding it but that hasn't helped.
Quite frankly I am stumped, because I have used listboxes hundreds of times and I can't get this to work. So any help would be appreciated.
The exact code to add items to the list is:
MainForm.lstPendingJobs.Items.Add(MainForm.currentJob)
I have tried moving this subroutine into the mainform class and that didn't help either.
Could it have been that you had accidentally altered the DrawMode property from Normal to OwnerDrawFixed or OwnerDrawVariable?
This would account for the non display of the items even though they are in the list box
item in your case is an object. You need to get a value out of it.
Try msgbox(item.value)

Searching through DataGridView

I have a datagridview with three columns (ID, Name, Address). It's bound to a database that contains around 500 items.
I want to be able to search the gridview for data given in a text box, and then highlight it.
If possible, pressing the Next button should find the next match, and the Reset button should clear all selections (nothing highlighted).
Please advise how to do it.
Thank you very much.
Add a search box and search button in the page. OnClick of the search button, search the datatable for the results and keep the result in the session. then on the bound event of grid, highlight the first row of from the search result set (which can be done by matching the primary/unique key of the table).
You have to handle most of code the manually for this. OnClick of next button, highlight the next row from the search result. again traversing of the search result has to handled manually like keeping the track of current result, moving to next result or moving backward etc.
then on click of reset button clear the search result session and bind the grid again without any highlighted rows.
I don't have code to post for you right now. but i hope if you implement this approach surely your problems will be resolved.

Best way to select from a list - aren't the two listboxes getting a little old?

How many times have we seen this type of selector:
List Box Selector http://geekswithblogs.net/images/geekswithblogs_net/dotNETvinz/MoveItemsListBox.jpg
I was just about to start creating this in a WinForms app, when I thought that others may have some ideas for doing this better. We need it to sort - so the right hand list will need up/down buttons. But this seems so old school. I love devexpress components, and was thinking of asking them if they would consider adding a component that handles this functionality with a slick UI.
I am thinking that a graphical representation of the objects, and a graphical representation of the listboxes - that would be a more intuitive way to move items around.
Has anyone seen an open source project like this?
If a CheckListBox won't suffice (and it usually will), then the "modern" approach would be to use a ListView or similar component with a "Transfer" column. Render the button inline in that column, one for each row, so that it only takes one click to move an item from one to the other.
You see this everywhere in Vista, usually with hyperlinks as opposed to buttons. Instead of clicking on an item and then choosing an action, you click the action at the item level.
I wouldn't go overboard with slickness as it can impair functionality, but the dual-listbox screen is definitely old-school.
Also, if there's a very large amount of data to manage, it helps to provide a progressive search at the bottom of one or both lists.
I have done this type of selection using (essentially) a single CheckListBox that displays each item as an image. Part of the image looks like a LED, which is on (bright) if the item is selected or off (dark) if it is not selected.
This works well if you have a reasonable amount of data to select from, and also works well in a multi-column format if you can predict that the options will have reasonably similar lengths.
Allow users to drag items in/out of list 2, and also drag to reorder in list2.
(All items dragged out of list2, and dropped anywhere outside the list, get put back into list 1, in their correct place in the list by alphabetical or natural order.)
You can merge the two list boxes into one with the help of groups (LVGF_GROUPID flag): one group for selected and one for not selected.
You can also implement group membership changes with drag-drop between them. This way single drag-drop can move an item into the other group at the appropriate position, saving most/all of the other buttons.
Additionaly the bottom of each group can have one pseudo item with help text (i.e. "Drag items here to...") that is visible only when relevant.

limit selections in a listbox in vb.net

I am creating a web control in vb.net that contains a list box. I know that I am able to choose a selection mode of Single or Multiple, but I am interested in limiting a Multiple selection to a predetermined size. For example preventing a user from selecting more than 3 items.
I have tried adding a Validator to the list box, which might be a way forward, but was wondering if there was a way of changing the selection mode to something a bit different than just the two specified ones.
Any ideas or suggestions would be greatly appreciated
Edit: Unfortunately due to project limitations I am unable to use Javascript on this problem. very annoying I know!
You could try handling one of the SelectedIndexChange events. With multiple selection, each time this event fires you can check how many items have already been selected. If it is more than you want, have the handler deselect the one that was just selected. You could also put up a little red label under the listbox saying "Sorry, no more than [n] selections allowed."
EDIT:
Just noticed you said WEB. Same theory applies, but it would have to be done using Javascript.
On the SelectedIndexChanged event, write a short piece of code that will check the number of selected items and if it is greater than your desired amount, unselect the new selection (or unselect the oldest one, or however you wish this to operate).