Make a list understand which item is selected C# - 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.

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.

Article/news ordering by order id

I m having a little trouble coming up with a schema to order and changing order in a article/news management system.
Here goes:
I have News Object Model as follow:
class News {
int id;
string Title;
string Content;
string OrderId;
// trimmed
}
I have CRUD for the object model. and List as follows:
Id Title Order
1. Foo -+
2. Bar -+
3. Glah -+
What i want to do is when user clicks on - for first news, i want to replace 1 and 2 orderid and of course display as well.
well how do i do this on server side? lets say for 1. item order id is 1 , how do i find the first item that has a higher order id then this one?
Or take 2. Bar, when i click on - , how do i find/replace order ids with first one. or i click on + how do i replace order id of this news with 3. Glah ?
is there a better way of doing this?
There are also some UI where user drags and drops ? any pointers on that?
When the user changes the location of an item, the server needs to know which item was changed and what it's new position is. In the code below, I'm using a List to figure out the new positons. In this sample code, newPosition is the new zero-based position and selectedArticle is the article that was moved.
List<Article> articles = LoadSortedArticles()
articles.Remove(selectedArticle);
articles.Insert(newPosition, selectedArticle);
UpdateArticles(articles);
After running, the article's index within the list tells you its new position. That applies to all articles in the list and works the same for a drag/drop UI. I don't know entity framework, but if you can map the orderId field in the database to the index of the article in the list, then you should be good to go.
I hope this is at least able to give you some ideas. Maybe someone else can give a solution specific to entity framework, but I think putting the articles into a sorted list and letting the list do the work might be the easiest way.
I think the best approach here would be to implement a Swap method that takes two News objects and swaps their OrderId, which I am assuming is numeric.
So the idea would be that you would pass this method the object that was clicked on, as well as either the one above (if the user clicked +) or the one below (if they clicked -). If you do it this way then you avoid the task of trying to find the next or previous item.
In the case of drag and drop, the task is a little different. You would first need to retrieve all the items between (and including) the item being dragged and the drop target, ordered by OrderId. From there you swap the dragged item with the next or previous item (depending on direction), and continue doing so until you have swapped it with the drop target.

Switchin/reversing a string in a SharePoint list item

I have a question on whether it would be possible to switch/reverse the ordering of string values in a SharePoint list item in code behind to then display the result of the processed string.
So currently to illustrate this I have an "Employees" SharePoint list with the following columns:
Title
Position
Now the title column has the following values:
Ted Baker
Joe Pierce
I have a simple web part that displays the list item values, but for the "Title" field rather than displaying "Ted Baker", I want it to be displayed "Baker Ted" so essentially displaying the last string value first.
Is this possible to do and if so what would be the best way to do it?
Thanks in advance
string name = "Ted Baker";
string newname = String.Join(" ",name.Split(' ').Reverse());

Comparing Windows.Forms.ListViewItem Items for Uniqueness

I have a Windows.Forms.ListView where the user shall be able to add and remove entries. Particularly, those are files (with attributes) the user can pick through a dialog. Now, I want to check whether the file names / entries I get from the file picker are already in the list; in other words, there shall only be unique items in the ListView.
I could not find any way to compare ListViewItems to check whether the exact same entry and information is already present in my ListView. The only way I see now is to:
> Loop through the files I get from the picker (multiselect is true)
> Loop through ListView.Items
compare ListViewItem.Text
> Loop through ListViewItem.SubItems
compare .Text
If during the comparisons a complete match was found, the new entry is a duplicate and thus is not added afterwards.
This seems like an awful lot of effort to do something that I would find to be a function that is not so uncommon. Is there any other way to achieve this?
The file system itself uses only the filename to test for uniqueness, so you should do the same, no need to compare sub-items too.
Items in a ListView typically represent some object. What I usually do is to assign that object (or at least some value identifying the object) to the Tag property of the corresponding ListViewItem when they are added to the list. That way you get a quite simple setup where you can compare items by getting the values from the Tag property and perform the comparison on those objects instead of the list view representation of them.

Categories