trying to get a specific item in a list box which will then load new web page
getting error saying listbox doesnt have 'findString' method?
Am i possibly missing a using ?
protected void lstVideos_SelectedIndexChanged(object sender, EventArgs e)
{
// Get the currently selected item in the ListBox.
string curItem = lstVideos.SelectedItem.ToString();
// Find the string in lstVideos.
int index = lstVideos.FindString(curItem);
// If the item was not found in lstVideos display a message box, otherwise select it in lstVideos.
if (index == 1)
{
}
The code you are trying is for windows form ListBox and not for asp.net ListBox control. You can simply use SelectedIndex property to get the selected index:-
int index = lstVideos.SelectedIndex;
Related
I have a simple Windows Form which contains a list box called lstVersenyzok. I have a lstVersenyzok_SelectedIndexChanged function. How can I clear the selection of the list box when I click the empty field of the list box? I tried, but it does not work if I check the condition lstVersenyzok.SelectedIndex == -1.
You can use MouseClick event and get the selected index by the IndexFromPoint method.
Check if the index is -1, then call the lstVersenyzok.ClearSelected() to clear the selection.
private void lstVersenyzok_MouseClick(object sender, MouseEventArgs e)
{
int index = this.lstVersenyzok.IndexFromPoint(e.Location);
if(index == -1)
{
lstVersenyzok.ClearSelected();
}
}
Hope this help !!
I have a list view with the multiselect property set to false. When the user clicks on it, I take the NAME property of the list view item and convert it to a decimal then feed that to a method that loads the correct record.
The code below works perfectly when I select one item regardless of how many items are in the list and regardless of which item I select.
private void ListInstruments_SelectedIndexChanged(object sender, EventArgs e)
{
ListViewItem selection = listInstruments.SelectedItems[0];
if (selection != null)
{
string strSelection = selection.Name;
SelectedInstrumentID = Convert.ToDecimal(strSelection);
LoadSelectedInstrument();
}
}
When I make a second selection (not multi-select, but a different selection from the listbox) I get an error referencing listInstruments.SelectedItems[0].
System.ArgumentOutOfRangeException Message=InvalidArgument=Value of
'0' is not valid for 'index'. Parameter name: index
Source=System.Windows.Forms
Any help would be appreciated.
It's possible, that no items are selected, and thus list.SelectedItems is empty; you are tring to get 0th item from the empty collection and thus have the exception thrown. The quick patch is
// instead of original
// ListViewItem selection = listInstruments.SelectedItems[0];
ListViewItem selection = list.SelectedItems.Count > 0
? listInstruments.SelectedItems[0] // the collection has at least one item
: null; // if the collection is empty
Or we can check if we have a selection and return when there's none
private void ListInstruments_SelectedIndexChanged(object sender, EventArgs e)
{
if (list.SelectedItems.Count <= 0)
return;
listViewItem selection = listInstruments.SelectedItems[0];
string strSelection = selection.Name;
SelectedInstrumentID = Convert.ToDecimal(strSelection);
LoadSelectedInstrument();
}
I have a listview and this listview will be updated when a message is coming in.
The code below is about how I get the selected row's value.
private void CallTabLv_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string callDetailValue = "";
dynamic selectedCallDetail;
//When a row of call detail is selected, return the selected row's value only
if (LineBtn1.IsChecked == true)
{
selectedCallDetail = CallTabLv1.SelectedItem;
callDetailValue = selectedCallDetail.Value;
}
if (LineBtn2.IsChecked == true)
{
selectedCallDetail = CallTabLv2.SelectedItem;
callDetailValue = selectedCallDetail.Value;
}
if (string.IsNullOrEmpty(callDetailValue))
callDetailValue = string.Empty;
Clipboard.Clear();
Clipboard.SetText(callDetailValue);
}
It worked fine for first coming message and I could get the selected row's value. The problem is when second message came in, my application stopped and returned "Cannot perform runtime binding on a null reference".
By the way, to return single selected row' value, which one should be used: CallTabLv1.SelectedItem or CallTabLv1.SelectedItems[0]? As I tried, if the latter is used, it will return "ArgumentOutOfRange Exception" when second message is coming in.
Please help.
Update:
When the message is coming in, I update the listview by clearing the whole listview and then print it line by line. I'm not sure if it is caused by the way I update the listview.
So this is probably happening because you are clearing the collection in which you have now changed the selected item. Since the item can be null at that time the dynamic object will be unable to access the property "Value"
You are checking if a checkbox is checked which is not necessarily a condition of if the selected item will exist. I would suggest your code be updated as such.
private void CallTabLv_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string callDetailValue = "";
dynamic selectedCallDetail;
//When a row of call detail is selected, return the selected row's value only
if (LineBtn1.IsChecked == true)
{
selectedCallDetail = CallTabLv1.SelectedItem;
}
if (LineBtn2.IsChecked == true)
{
selectedCallDetail = CallTabLv2.SelectedItem;
}
// I think the 'invalid' casting is happening with the "selectedCallDetail.Value" as when you clear a listbox it is no longer selected.
callDetailValue = (selectedCallDetail != null) ? selectedCallDetail.Value : string.Empty;
Clipboard.Clear();
Clipboard.SetText(callDetailValue);
}
You should be using the SelectedItem as the SelectedItems collection may be null or empty, If you only ever want one result this is usually the best. also make sure your ListBox only allows for a single item to be selected so that the user cannot crash your code by selecting multiple items using SHIFT or CTRL click
I've got this code that populates a ListBox when a flyout is opened:
private void flyoutOpenPhotosets_Opened(object sender, object e)
{
lstbxPhotosets.ItemsSource = PhotraxSQLiteUtils.GetPhotosets();
foreach (String pset in App.CurrentlyMappedPhotosets)
{
int lstbxIndex = lstbxPhotosets.Items.IndexOf(pset);
if (lstbxIndex >= 0)
{
lstbxPhotosets.Items[lstbxIndex].? what now?
}
}
}
GetPhotosets returns a List. That part works (the list box is populated with the appropriate string values)
The problem is with the rest of the code (the foreach block).
CurrentlyMappedPhotosets is also a List. I want matching members among the strings in CurrentlyMappedPhotosets and those in the ListBox to cause the item in the ListBox to be selected when the flyout displays.
I was hoping to do be able to do something like this:
lstbxPhotosets.Items[lstbxIndex].Selected = true;
...but lstbxPhotosets is disallowing that.
So how can I programmatically select specified ListBox items?
Use
lstbxPhotosets.SelectedIndex = lstbxIndex
I am developing a windows form application using c#. I have used a listview control with its view property set to Details. It has two columns, ID and Name The data in the listview is data-bounded.
Now, i want to add search capability to the listview control using a textbox i.e. whatever string entered to the textbox will be searched over listview items in both the columns, ID and Name and when the item is found it gets selected and will become the top item of the listview.
private void textBox1_TextChanged(object sender, EventArgs e)
{
try
{
ListViewItem foundItem = listView1.FindItemWithText(textBox1.Text, false, 0, true);
if (foundItem != null)
{
listView1.TopItem = foundItem;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Have you Set the View to list to use the FindItemWithText method when initializing the ViewList? If not add the below line while initilizing your listview. without it, FindItemWithText method wont work.
listView1.View = View.List;
Edit: I found the issue is in your listView1.TopItem = foundItem;
The method actually found the data you search, but it couldn't add it to top of your list. Setting TopItem always have issues. http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.topitem.aspx
Anyway, your search works fine.
Try to add your FoundItem to a new list, It'll work..