C
C#

How can I insert my sortedlist into a listbox?

How can I insert my sortedlist into a listbox?

Jjjsutton.11/18/2023
I am new to C# and currently using a sortedlist<string, int> in order to store values that I need to display in a listbox (according to my uni, this is how I need to do it). I have tried quite a few things however I am struggling to get both the key AND value into a listbox. Would anybody be able to give me some guidance on how I can go about doing this?
TTheRanger11/18/2023
listbox in winforms? wpf? web site?
Jjjsutton.11/18/2023
Apologies, it is in winforms
TTheRanger11/18/2023
try
yourlistBox.DataSource = yourSortedList;
yourlistBox.DataTextField = "Value";
yourlistBox.DataValueField = "Key";
yourlistBox.DataBind();
yourlistBox.DataSource = yourSortedList;
yourlistBox.DataTextField = "Value";
yourlistBox.DataValueField = "Key";
yourlistBox.DataBind();
or
yourlistBox.DataSource = new BindingSource(yourSortedList, null);
yourlistBox.ValueMember = "Key";
yourlistBox.DisplayMember = "Value";
yourlistBox.DataSource = new BindingSource(yourSortedList, null);
yourlistBox.ValueMember = "Key";
yourlistBox.DisplayMember = "Value";
Jjjsutton.11/18/2023
First one does not contain definitions for all but DataSource Second one does show my value but not the key
TTheRanger11/18/2023
ah lol just noticed its swapped, just swap it make it show the key instead
Jjjsutton.11/18/2023
lol, now the key is showing but the value is not
TTheRanger11/18/2023
that is the point you want to display both at the same time?
Jjjsutton.11/18/2023
Correct Ive been able to show one at a time, however been struggling to make it show both
TTheRanger11/18/2023
unsure, try removing these lines
yourlistBox.ValueMember = "Key";
yourlistBox.DisplayMember = "Value";
yourlistBox.ValueMember = "Key";
yourlistBox.DisplayMember = "Value";
Jjjsutton.11/18/2023
Displays the value
TTheRanger11/18/2023
hmm, i recall DataSource can accept a list directly
Jjjsutton.11/18/2023
How I originally have been doing it is through .ToList, however that only displays the value lsbScores.DataSource = scores.ToList();
TTheRanger11/18/2023
yourlistBox.DataSource = yourSortedList.Select(x => new KeyValuePair<string,int>($"{x.Key} {x.Value}", x.Value));
yourlistBox.ValueMember = "Value";
yourlistBox.DisplayMember = "Key";
yourlistBox.DataSource = yourSortedList.Select(x => new KeyValuePair<string,int>($"{x.Key} {x.Value}", x.Value));
yourlistBox.ValueMember = "Value";
yourlistBox.DisplayMember = "Key";
Jjjsutton.11/18/2023
No description
TTheRanger11/18/2023
ok put .ToList() at the end yourlistBox.DataSource = yourSortedList.Select(x => new KeyValuePair<string,int>($"{x.Key} {x.Value}", x.Value)).ToList();
Jjjsutton.11/18/2023
This works, thank you so much for that... Ill take some time to understand why it works as well. If I have any questions ill message you if you dont mind?
TTheRanger11/18/2023
well thats simple, KeyValuePair has 2 columns, Key and Value DisplayMember selects which Column to display for the user
Jjjsutton.11/18/2023
Right, but its set to key, so why does it display both the value and key?
TTheRanger11/18/2023
it does not the value is actually concatenated in the key by this line yourSortedList.Select(x => new KeyValuePair<string,int>($"{x.Key} {x.Value}", x.Value));
Jjjsutton.11/18/2023
that line is setting x to be key:value correct?
TTheRanger11/18/2023
you see, SortedList<string,int> is actually a collection of KeyValuePair<string,int>
Jjjsutton.11/18/2023
Im not suuuper familiar with lambdas yet, so trying to understand that line specifically lol
TTheRanger11/18/2023
say u have a key called Foo with a value of 1
Jjjsutton.11/18/2023
This I understand
TTheRanger11/18/2023
all i did was converting the key to Foo 1
Jjjsutton.11/18/2023
Yeah, im understanding that
TTheRanger11/18/2023
so now the Key of this element is Foo 1 instead of Foo
Jjjsutton.11/18/2023
it hasnt changed my actual key to foo 1 though right? lol
TTheRanger11/18/2023
in the sorted list itself? no
Jjjsutton.11/18/2023
ok cool Ah I see, ur initializing a new keyvaluepair and setting that to be my sortedlists key+value in the key right?
TTheRanger11/18/2023
yes
Jjjsutton.11/18/2023
coool im understanding then thanks so much I appreciate it

Looking for more? Join the community!

C
C#

How can I insert my sortedlist into a listbox?

Join Server
Want results from more Discord servers?
Add your server
Recommended Posts
How to map Entity Framework entities containing circular references to DTOs?When querying data with Entity Framework, the result data appears to contain circular references. (✅ Typeorm doesnt get the name from entityI need that typeorm get the ```name: "noInfoMILeadsToDistribute"``` inside Entity, but it wants to gProcessing files as fast as possibleHi everyone! I have a question that I’m sure you can help me with. I’m making a library that needs t✅ [SOLVED] Does the 3rd line of code hold memory as a reference or does it hold the actual value?ayooo, quick question. pretty simple. Book is a custom class just so you know.... ```csharp Book boRequestsHello, i have never done something that i will say before, how can i send a request to an API Url, gWhy is my grid on Line 28 not calling back to my other script called Grid?✅ Visual Studio 2022 Failing to Create C# ProjectWhat's happening here?✅ Help with creating shapes on a windows form appI have created a windows form app to display shapes on a bitmap, my circle function works fine but mNo inbuilt functionsHello, im a year 1 student so my knowledge is not that massive. So i have this task for university wSignalR initial connection takes way too long (logs included in comments)Hey there. So I have two versions of the same application, using essentially the same code. One of ✅ How to make DontDestroyOnLoad work for only single scene?Hello! I have a problem. In my game, when I am on my Main Menu Scene and trying to load Main Game ScUI is lagging while scrolling a listViewHi, I wanted to create an application that retrieves data from the API and displays it. I'm having tHow can i fix this error code?he's giving me an error code for private?Visual Studio 2022 ExtensionI'm working on a visual studio 2022 Extension. But as soon as i import it inside the visual studio 2✅ ValidationContext in ASP.NETWhat does ObjectInstance and ObjectType mean? afaik 1) ValidationContext is basically on which claUpdated from 2021 to 2022 version of unity, getting NavMeshSurface related error.Hey y'all, I'm an absolute newbie trying to make a basic game and figured the unity tutorials would Cache not clearing, what am I missing?Does anyone know why this cache is not clearing? I am trying to implement simple rate limiting an aiCal (webcal://) does not doing a sync in outlook365I have my .net core app, and my app generates a link using iCal.NET nuget, and initially it generateDesign Pattern for mapping generic class type to implementationHey all, I'm working on an executor service that maps an executor record of a generic type to an ex✅ Blazor Tutorial (Todo List) Doesn't WorkHello guys, I'm new in Blazor. ```@page "/todo" <PageTitle>Todo</PageTitle> <h1>Todo (@todos.Count