selecteditems and selectedindex c# - c#

I'm new to C# and I'm starting to learn how to program
I'm learning to program into the Visual Studio Microsoft Edition where I use the WindowsApplication instead of the Console.
While trying to make this code, I encountered this command:
Selected Index and Selected Item
and I would like to know the difference between the two.
I'm quite confused now with my code. The code I'm trying to do is adding and deleting text in the listbox.
Thanks for your help.
Additional question: in my code I have this line:
int listBoxSelectedItem = listBox1.SelectedIndex;
listBox1.Items.RemoveAt(listBox1.SelectedIndex);
I would like to understand this part:
The first line, has a variable called " listBoxSelectedItem" with a type "int". The position of the item you selected will be store to the variable called "listBoxSelectedItem". Is that correct?
The second line is, the "listBox1.SelectedIndex" is the information that is being pass through to the method, "RemoveAt" Is my understanding here correct?
Thanks

Selected item will return the object that is selected. Selected index returns the location in the list as an int.
For example you may have a list of strings:
Cat
Dog
Hamster
Horse
If you select "Dog" from this list them the SelectedItem property is the string "Dog" while the SelectedIndex is 1 (indexes are zero based, so the first item is 0, second 1 etc.)

Related

Make a list understand which item is selected C#

I'm stuck.
I have 2 different listviews, the listview1 contains name, and the listview2 are showing the episodes to the name i selected in listview1.
The problem is that i want to have a description in a label of the specific episode i select in the listview2.
Right now i have all episodes in order in the listview2, and just for testing i have made a listview3 who shows all the descriptions in a list.
i pick out the information from and i use
To get the descriptions.
But the problem is that i dont know how to connect the right episode to the right description. Have someone done something similiar before?
Right now i just loop trough the list and make it appear inte the listview3, but its not correct.
To get the episodes from the url to the list i use
Iterate once through your items and update both lists.
foreach (SyndicationItem episode in feed.Items)
{
var episodeName = episode.Title.Text;
var episodeDesc = episode.Summary.Text;
podcastEpisodes.Add(episodeName);
episodeDescription.Add(episodeDesc);
}
To set the selected item in a listview, you can set its selected/focus state.
Can you simply do this:
lvEpisode.Where(x=> x.Name == NameListView.SelectedItem).Select(x=>x.Name).FirstOrDefault();
This assumes you have a class of item containing Name.
(Typing through mobile, this maynot compile):
Is it possible to convert your episode description from a list to a dictionary? The key would be the episode and the value would be the description. When user selects the episode then you just need to index the dictionary to get the description?
since you are getting a xml data, try parsing as a JSON and the information will likely structured. so the index you use for your episodes should also be used to index the description. so a JSON like
movies={name: "Walking Dead" ,
category: "Horror",
episode:"episode1",
description:"walkers rise again"};
so you can now get movies["episode"]; and movies["description"] easily.
They are online parsers you can put in xml and convert to JSON and also packages for c# too i believe, but put the xml HERE and see how the structure changes.

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.

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...

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

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
}
}

How to insert a Combobox value in to a database

How to insert a Combobox value in to a database. When i hit Save button in my application, it will stored stored into database like this way..
System.Windows.Controls.ComboBoxItem
What i do for getting correct value of a combo box?
if you are using a string in the combobox, then you can use the example that Akash Kava showed.
Also, if the combobox contains other elements, you can get those elements through the use of something like
((ComboBoxItem)rpcombo.SelectedItem).Content.ToString()
and casting it to what you need.
lets say your combobox has string items what you need is like this
froeach( ComboBoxItem item in ComboBox1.Items )
{
string str = (str)item;
// saving value
}
Best Regards,
Iordan
This question seems a bit too broad. We are going to need nore information about this to be able to answer this one.
What kind of database are you using?
Are you connected to the database or do you need help with tahat as well?
In that case there are alot of good reading on the internet to your aid, that I can link to.
Do you want to save the ComboBoxItem as an .net object or det values of the item?
The list can go on, you have to give me something more to chew on.
MyObject obj = myComboBox.SelectedItem as MyObject;
obj.ValueIamInterestedIn; <--- this should be inserted
or if you have string or any valuetype array as ItemsSource then,
myComboBox.SelectedItem.ToString();

Categories