Getting Value at Selected Indices in C# ASP.net ListBox - c#

I am trying to get the value at selected indicies in a ListBox using ASP.net C#
MakeListBox.SelectionMode = ListSelectionMode.Multiple;
int [] indicies= MakeListBox.GetSelectedIndices();
I am going to dynamically building a select statement for a database query to an SQLDataSource. What I had hoped to be able to do was get all the selected indexes go through a loop to for the array that it returns and add each value at the specified index to a string.
I have looked through this and this but can't find what is needed to do what I have talked about.
Basically I am looking for the opposite of the IndexOf command. Or a technique that would have the same results.

It's not clearly obvious when looking at the ListBox control properties, but this is the best way to do it:
foreach(ListItem li in MakeListBox.Items)
{
if(li.Selected)
{
// Append to your string list
}
}

Related

Duplicate Values in DropDown C#

In a dropdown I've got a list of country of this type:
Text: "Italy" Value :"IT"
I need a copy of one of the country in the top of the list referred to the current language of the portal I'm working in, so the user can both select the first or the one into the list. Is just a hint we can say.
So I've just added a copy in this way:
cmbNazione.Items.Insert(0, cmbNazione.Items.FindByText(valori.Rows[0]["M_SOAAuthorityCountry"].ToString().ToUpper()));}
This code works fine. I got my duplicate value and I can select it without problem.
I am stuck when I already have a country that I have to set into the dropDown.
I just wrote that:
cmbNazione.Items.Insert(0, cmbNazione.Items.FindByText(valori.Rows[0]["M_SOAAuthorityCountry"].ToString().ToUpper()));
cmbNazione.ClearSelection();
cmbNazione.Items.FindByText(valori.Rows[0]["M_SOAAuthorityCountry"].ToString().ToUpper()).Selected = true;
The problem is that I receive the error: Cannot have multiple items selected in a DropDownList.
In fact if I check I got both the list Items (first, and the identical in the list) with the prop : selected = true. If I try to make one false both change to false. I cannot use them separately.
I can't understand why I can use them correctly when I select manually, as a user, but not when I try to select them through code.
I also tried something like :
cmbNazione.SelectedIndex = cmbNazione.Items.IndexOf(cmbNazione.Items.FindByText(valori.Rows[0]["M_SOAAuthorityCountry"].ToString().ToUpper()));
But nothing. I'll every time have multiple items in the list of the selected
You should not have a duplicate value as it will confuse the user (if the value is on 2-3 position already) and architecture as retrieval may lead to 2 values being selected.
Your use case seem like you are offering the most commonly used value as the first one and then the remaining. If this is the case, follow the approach outlined below.
Find the ListItem required, then remove it from the list and add on the 1st position. Then mark it as selected.
var preferredItem = cmbNazione.Items.FindByText(...);
if (preferredItem != null) {
cmbNazione.Items.Remove(preferredItem);
cmbNazione.Items.Insert(0, preferredItem);
cmbNazione.SelectedItemIndex = 0;
}
This way, there will be single item being selected and preserve the sanctity of the item (underlying value etc) while still allowing user to have the preferred one on top of list. You can chose to have this as 'pre-selected' or let user select it explictly.

C# how to match textbox string linq

I'm having trouble matching textboxes using linq.
I have to populate a textbox with a collection that I converted from an array.
The conversion looks like this.
List<string> animalList = new List<string>(animals);
The list contains items such as "Dog", "Cat", "Snake", "Rat", "Mouse", "Duck"
I can populate the first textbox (there are 3 textboxes, one for the collection, one for a word to be input, and one to display whether or not the input word matches the collection).
I don't know much about Linq. Please help point me in the right direction. I've looked all over online, and I can't find an example for something that should be as simple as matching 2 textboxes using Linq.
Should be as simple as textbox1.Text == textbox2.Text
I'm not sure Linq is the way you want to go here. List<T> has a contains() method that should do what you want. Where is the Linq requirement coming from?
Also, I think you mean a Listbox for the collection (as they're designed to hold multiple items), a textbox for entering new values and then a textbox (or label) to display the result after you test the entered value against the contents of the collection, right?
In your event handler you'll want to use a statement like this:
animalList.SingleOrDefault(a => a == textbox1.Text);
That assumes that the textbox where the name of your new animal is typed into is named textbox1.

How to ArrayList Object From a Selected Index in Listbox C#

So I have an arraylist that will display data to a listbox. Then, the user can select an entry in the listbox. From there, I need to be able to grab whatever entry they select. I'm using VS 2010.
I have been trying
tempArray.Insert(0, myarray.IndexOf(mylistbox.SelectedIndex);
All this is doing is giving me the actual index number and not the contents of the index. I'm not sure how to index the arraylist to get the object that is contained at that index.
And yes, I know that I should be using List objects, but it is for class and we have yet to be taught list objects.
As the name suggests .IndexOf() will return an index from the array.
Array.IndexOf Method
This is obviously not what you want.
What you should look at is using the SelectedItem instead of the SelectedIndex.
That way, you can access the object directly and insert it into your list. One thing to remember is that SelectedItem will return an Object. This means that it will have to be cast to the type that you expect to be using.
Also, are you wanting to constantly insert items to the top of the list or does it not matter. If you can append it to the end of the list, try using .Add(yourObject).
If these are actual arrays you are working with, shouldn't you be able to to access what's in the array by using the [ ] syntax? i.e.
myArray[myListBox.SelectedIndex]
1) Dont use ArrayList... that's an old class. Use List<T> instead.
2) Have you tried ListBox.SelectedItem ? That seems a bit simpler...

deleting from a list in c#

I have a wpf c# application, that loads tasks to a treeView from a text file, the data about the tasks are loading into a list, Im trying to delete data in position I in the list, but i can not figure out how. I have this for loop checking to see if the selected treeView item is equal to the item in position I in the list and if so I wanna delete that item from the list. Here is the for loop which works, im just wondering how to do the actual delete, I've tried things such as .delete and .remove which I found on msdna.
for (int i = 0; i < name.Count; ++i)
{
string selectName = ((TreeViewItem)(treeView1.SelectedItem)).Header.ToString();
if (selectName == name[i])
{
//name.Remove(i) or name.Remove[i] or name[i].Remove
}
}
If name is a List<T> then you probably want name.RemoveAt(i) if I understand the question correctly.
Alternatively, you could just use name.RemoveAll(n => n == selectName); instead of the for loop.
How about:
string selectName = ((TreeViewItem)(treeView1.SelectedItem)).Header.ToString();
name.RemoveAll(x => x == selectedName);
You have to give List.Remove() an object, not an index.
So, assuming name is a List,
name.Remove((TreeViewItem)treeView1.SelectedItem);
Depending on your application needs, your data structure should have some time of Parent-Child relationship. All you should have to do is remove the Child that is selected from the parent, and the TreeView in WPF should just automatically update.
I suggest you read Josh Smith's article on using ViewModels with the TreeView for easier management. Trust me it will make your life a lot easier the sooner you start using a stronger data structure, and just remember that a TreeView only represents the data provided, and you should have other methods to edit the data directly and not the TreeView.

Inspecting Lucene.NET index with Luke want to replicate NHibernate.Search view

I am trying to put together an index using terms, which I specify as a comma separated list. I want to replicate the display in Luke as seen here:
http://ayende.com/Blog/archive/2009/05/03/nhibernate-search-again.aspx
But my index value just shows as a single field with the comma separate list value. For example:
Tags term,anotherterm
When I search my index, it will return results if I search with "term" but will not return anything if I search with "anotherterm"
I thought the indexing process would break the comma separate list apart into separate values but this does not seem to be the case.
Anyone got any ideas?
Thanks
The collection to index is ISet<T> and I suspect that T is a type with [Indexed] attribute and has a property Name marked with [Field] attribute.
This was answered as part of this question:
Lucene.NET search index approach

Categories