Select items from Listbox, one by one - c#

I am working on Microsoft Visual C# 2010 Express. Actually, when I execute my code, I get a listbox containing N items. There is a list of items with their size in my database. I am using Microsoft Access 2007 database. I want to display the total size of items present in listbox into a label below the listbox. I have my code for displaying the size. I just want to know how to select already present data in a listbox without any button click. I think loop will be used.

Do what you want to do but i don't understand why you need to select an item form listbox. but you can get the items by
listBox1.Items[i] and then can calculate the size.
Use below code to do that what you wanted.
for (int i = 0; i < listBox1.Items.Count; i++)
{
listBox1.SelectedItem = listBox1.Items[i];
//Calculate the total size of items present in listbox here
}

I just want to know how to select already present data in a listbox without any button click.
I think the MouseDoubleClick event is what you want to handle. To add the handler for that, in the designer, in the properties window, click on the little yellow lightning bolt icon. This will show a list of events. Double-Click on the one you want to handle and the system will create the stub for it and add the handler to the control. A loop in side here will allow you get what you need:
private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
foreach (string item in listBox1.Items)
{
//Pass item to your size routine here
}
}

Related

HelpProvider in checbox list specific item (C#)

I'm making an app which uses a checkbox list. Each item has to have a "description", so I decided to make it with a HelpProvider. But the problem is that when I make a loop that should fill all the checkboxes in checkbox list with a helprovider, Visual tells me that it is an object which cannot be converted into System.Windows.Forms.Control
Any ideas for a workaround?
for (int i = 0; i < CheckedListBox.Items.Count; i++)
{
this.AdditionalInfos.SetShowHelp(CheckedListBox.Items[i], true);
this.AdditionalInfos.SetHelpString(CheckedListBox.Items[i], "example description");
}
So, the reason you get that error is because and SetShowHelp and SetHelpString expect a Control object (a Button, a Label, etc...) but you're giving it a string which can't be handled by the HelpProvider.
I believe that in order to do what you want to do, you'll have to write a custom thing (since the elements of the CheckBoxList aren't Controls). You can either write a "complex" thing that can show descriptions on hover or you can just listen to the SelectedIndexChanged and show a description on a label or something.

How to disable combox dropdown when user enters text into the combox

I have a simple combobox in c# forms which is populated from an array.
I have set AutoCompleteMode to SuggestAppend and AutoCompleteSource to ListItems. This allows me to filter through the list quickly by typing a string into the combobox and matching items are displayed as I type along. This works great.
However, when the drop down list is open and I start typing, the filtered list appears on top of the dropdown list but I cannot select from the filtered list but only from the drop down.
How to disable drop down list while open as soon as user enters a character into the combobox.
Currently only have one method for the combobox
private void SelectJobDropdown_SelectedIndexChanged(object sender, EventArgs e)
{
//plenty of code here
}
I have tried adding other methods for the combobox such as KeyPress or Keydown but none seems to be working for as I'm very likely doing something wrong
Using Visual Studio 2015
If I understood you correctly you don't like the overlapping list over the old drop down. Since you type letters into the ComboBox I would suggest to use the comboBox1_TextUpdate event. This nice line of code should fix your problem:
private void comboBox1_TextUpdate(object sender, EventArgs e)
{
comboBox1.DropDownStyle = ComboBoxStyle.Simple;
Setting the ComboBox.DropDownStyle property, which
specifies whether the list is always displayed or whether the list is displayed in a drop-down[...]
to ComboBoxStyle.Simple, which
Specifies that the list is always visible and that the text portion is editable. This means that the user can enter a new value and is not limited to selecting an existing value in the list.
will remove the original dropdown (long list) and only the filtered results remain.

Selecting text from ListView in c# windowsForms

I'm coding in C#, in Windows Forms. I'm using VS 2013.
I have a ListView in detailed mode and it shows my data correctly. I want to select it's items and copy the text of them or show some information about an item when I hover the mouse over it. But I can't do anything with Data which are shown in table. Only the first column can be selected and of course I can't copy those items too. The other columns cannot be selected.
What should I do?
I've searched table in toolbox. But there is only TableLayoutPanel and working with it is harder than ListView. Does VS 2013 have anything better?
One way to control mouse hover is by making a event for mouse hover
this.panel1.MouseHover += new System.EventHandler(this.panel1_MouseHover);
private void panel1_MouseHover(object sender, System.EventArgs e)
{
// Update the mouse event label to indicate the MouseHover event occurred.
label1.Text = sender.GetType().ToString() + ": MouseHover";
// Instead of printing you can do whatever you want here, like you want to select some text or whatever
}
For more details, you can see this link https://msdn.microsoft.com/en-us/library/system.windows.forms.control.mousehover(v=vs.110).aspx
For complete row selection, you can use
listView1.FullRowSelect = true;

How to select a ListBoxItem which is already selected OR to highlight previous selected ListBoxItem after setting SelectedIndex to -1?

I'm programming a simple listbox in windows phone 7. The listbox has some items and when I click any of items the app navigates to a new page.
From beginning to here, everything is good.
But I want end-user can select the item again which causes navigate to the next page again. But listbox as it is, doesn't allow me to select an already selected item again.
I tried to do this for allowing to select an item again.
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if(ListBox.SelectedIndex != -1 )
{
NavigationService.Navigate(uri);
ListBox.SelectedIndex = -1;
}
}
I have alreadhy edited the ListBoxItemTemplate to highlight selected item, but when I use above code, I cannot highlight the selected item, because it changes SelectedIndex too fast.
So how can I allow user to select a selected item or how can I highlight previous selected item. Any suggestions or tips?
EDIT:
When using a normal ListBox , I can simply use :
listboxitem1.Background = new SolidColorBrush(Color.Blue);`
But when I edit template of listbox item this does throw exception, so I cannot still do this.
Look into implementing one of the other Event handlers for Mouse events on the ListBox. Such as the Click Event. You can detect when the user clicks the ListBox without changing the selected index by keeping track of the previous selected index value.
Well, you can edit the template of ListBoxItem and add a grid, fully front of listbox item. And bind the visibility property of this grid.
After this, you can change of the visibility according to selected index. This approach should work well if you're using a navigation with listbox.
Hope it works!

What exactly is ListView.RetrieveVirtualItem Event - C#

With respect to a Virtual ListView control in a Winforms App, what are ALL the functions of RetrieveVirtualItem event?
Scenario: There is a Dictionary object which acts as my ListView cache. And its items are displayed on the ListView. On click of a column, the cache dictionary is sorted in memory and the ListView is Refresh()ed. The effect on UI, the ListView is sorted well. :)
But I want to understand what the role of RetrieveVirtualItem Event is, which gets triggered on Refresh().
In the RetrieveVirtualItem event handler [ Someone else's code, :( ], following are done:
Based on RetrieveVirtualItemEventArgs.ItemIndex, get the message from Cache
Set RetrieveVirtualItemEventArgs.Item = retreived item above
It appears that what's done in the event handler is important because, if I take it out, ListView cries. What is the significance of this event?
EDIT
Or let me re-phrase the question... My concern is, Why, after Sorting (and RetrieveVirtualItem event handler), the selected item remains at the location where it was before sorting. I.e, If I select item #5 and sort, and if sorting makes this item as the last item, I would prefer the LAST item to be selected after Sort. But in my case 5th item is selected after sort. So what is the default behavior of RetrieveVirtualItem WRT selected item?
A virtual ListView should only call RetreiveVirtualItem for the rows currently visible on the screen.
As you navigate in the ListView, for example, you press the page down key, the ListView will calculate what should now be the index of the top row and will then call RetrieveVirtualItem so that your code can provide the item to use at each row index.
Unless you cache or otherwise store the items you are providing via RetrieveVirtualItem, they will no longer exist once they are scrolled out of the listview.
This is what the Virtual in VirtualListView means - there aren't any real rows, the rows are virtual. That is how it could display a list containing hundreds of thousands of rows - because it will ever only actually contain how ever many rows are visible on screen.
In effect, the ListView is like a window that is moving up and down your internal list of data - the RetreiveVirtualItem method is what it calls to move items into that window as it moves along. It says, hey I just moved to row 15 - give me the item for that row. It will proceed to call RetreiveVirtualItem for each row index which would be visible. If the ListView was 5 rows in height on the screen, you would receive 5 calls to RetrieveVirtualItem - even if the actual data backing the listview had 3000 items. Each time the top row of the ListView changed (because of navigation), you would receive 5 calls to RetrieveVirtualItem (this is not always the case, but it is the right idea - for example, if you scroll down one row, it will simply ask you for the new last row - it will also simply discard the data that was used for the old top row that scrolled out of view).
I guess it might be even easier to explain if we assume the ListView was only one row high on the display (meaning only a single row is ever actually visible on the screen) - as you move the ListView up and down your list of data (i.e. the user navigates the ListView), it would call RetrieveVirtualItem exactly one time every time it moves to a new row.
Hope that helps...
Good Luck
Virtual listviews only deal with indices. So, if the 5th item is selected before the sort, the 5th item will still be selected after the sort. The control itself has no way of knowing that the data that used to be on the 5th row is now on the first row.
You will have to program that for yourself in your sorting method:
remember which items were selected (remember: you can't use SelectedItems property when in virtual mode)
do the sort
find the indices of the previously selected item now live
select those indices
You can see all this in action in ObjectListView -- a wrapper around a standard .NET ListView.
The RetrieveVirtualItem event is only used when the ListView is put into virtual mode. Rather than retaining ListViewItems in the Items collection (as when not in virtual mode), it creates the ListViewItems dynamically, when needed.
If you don't handle the RetrieveVirtualItem event then no ListViewItems will be added to your ListView. I've attached some sample code for a typical method written to handle the event:
//Dynamically returns a ListViewItem with the required properties; in this case, the square of the index.
void listView1_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
{
//Check if cache has been instantiated yet, and if so, whether the item corresponding to the index requested has been added to the cache already
if (myCache != null && e.ItemIndex >= firstItem && e.ItemIndex < firstItem + myCache.Length)
{
//Return cached item for index
e.Item = myCache[e.ItemIndex - firstItem];
}
else
{
//When item not in cache (or cache not available) return a new ListViewItem
int x = e.ItemIndex * e.ItemIndex;
e.Item = new ListViewItem(x.ToString());
}
}
This example is taken from MSDN (http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.virtualmode(v=vs.90).aspx) where further details can be found.

Categories