Click here to Skip to main content
15,890,186 members
Everything / ListBox

ListBox

ListBox

Great Reads

by Nilay M Joshi
Multiselect Combobox - Custom control for WPF
by Magnus_
public static void RemoveSelected(this ListControl source){ foreach (var item in source.Items.Cast().Where(li => li.Selected).ToList()) source.Items.Remove(item);}protected void btnRemove_Click(object sender, EventArgs e){ lstCity.RemoveSelected();}
by Evren Yortuçboylu
A checkbox list jQuery UI widget with real time filtering functionality explained
by Paul_Williams
The difference between ListBox and CheckedListBox DataBinding.

Latest Articles

by Martin Henke (1971)
Edit Metadata for folders by using desktop.ini features
by Nilay M Joshi
Multiselect Combobox - Custom control for WPF
by Michael Shpilt
A WPF tutorial on how to create FilteredListView: A ListView custom control with search filter that uses Throttling.
by davekahn
An easy way to control the lists of options in multiple related SELECT controls

All Articles

Sort by Updated

ListBox 

23 Feb 2024 by Martin Henke (1971)
Edit Metadata for folders by using desktop.ini features
13 Dec 2023 by Inczu
Hello guys and ladies I want to create a code that will add all my local users from Items from listbox public void ExistUserList_SelectedIndexChanged(object sender, EventArgs e) while added local users are in private void LoadLocalUsers() to...
1 Dec 2023 by Inczu
Hello, I want to create a research for my listbox. I have created listbox called: public void ExistUserList_SelectedIndexChanged(object sender, EventArgs e) and a code that will write in a textbox private void AllText2_TextChanged(object...
20 Nov 2023 by Graeme_Grant
If you use data binding with the ListBox to a List, you don't need to access the ListBox to access the collection. The ListBox becomes a 'view' to the data. This means that you can access the collection of data directly. You can read more on how...
20 Nov 2023 by Ralf Meier
you haven't answered my question - but independant here is an answer for you : Dim l As Integer = ListBox1.Items.Count Dim myEntry as string = ListBox1.Items(l-1) this gives you the last Entry from your Listbox. to get any else you...
20 Nov 2023 by Member 12602897
I have a listbox on my form and I add entries to it throughout the program. Normally the listbox Visible property is set to FALSE. When it is FALSE the SELECTITEDITEM only returns the first entry. When I change it to TRUE I am able in my program...
4 Nov 2023 by Inczu
I want to create a code without admin license when I select a username from a Items listbox: private void ExistUserList_SelectedIndexChanged(object sender, EventArgs e) Then the code must search the same username in lusrmgr.msc and put the...
4 Nov 2023 by Inczu
I want to create a code using netuser function that will search by clicking on the button: private void NextForm3_Click(object sender, EventArgs e) if the user by selecting an Item from listbox. private void...
3 Nov 2023 by Dave Kreskowiak
Like Richard already said, I told you how to validate credentials against the machines local account database that does not use shelling out to "NET USE". It is not called "in Windows services". That phrase means something completely different...
30 Oct 2023 by Dave Kreskowiak
An "admin license," whatever that is, has nothing to do with this. You're completely misunderstanding the Windows security system and directory. You cannot check an entered password against what you perceive to be a plain text password in the...
30 Oct 2023 by Richard Deeming
You can't. Windows does not store user passwords using a reversible encryption. Passwords technical overview | Microsoft Learn[^]: For use in Windows networking, including Active Directory domains, the password is stored two different ways by...
12 Sep 2023 by vblover Programmer
Public Class Form1 Dim Link_ As New LinkLabel Dim Proc As Process Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click If Me.TextBox1.TextLength = 0 Then Exit Sub If...
17 May 2023 by Member 12926744
Hi All , I have listbox to select multiple items . I need to save the listbox value in selected order rather than its unique id order . Anyone please help . What I have tried:
17 May 2023 by Richard Deeming
A ListBox is rendered as a element[^]. That element submits the selected items in the order they were declared in the source, not the order in which the user selects them. If you need to know the order in which the user selected the...
3 Apr 2023 by Member 15758581
I am Working on a Backuptool but now i ran into a problem, all my Items in my ListBox where they were read from an OpenFolderDialog all have checkboxes. Now here is my Problem, I want to get the checked Items into another ListBox called...
3 Apr 2023 by Graeme_Grant
You are using MVVM. You can use DataBinding to track selection. 1. Wrap the Model: public class CheckItemModel : ObservableObject { private bool isChecked; public bool IsChecked { get => isChecked; set => Set(ref...
4 Dec 2022 by ernteSKY
I load all of data from the database into a List then display all in a ListBox. games_listBox.DataSource = _games; All games have a data: year of publication, year of manufacture, name, manufacturer, category. The question: How to display...
4 Dec 2022 by Richard MacCutchan
Take a look at A Detailed Data Binding Tutorial[^] which shows a number of ways to do it.
21 Nov 2022 by ernteSKY
I have a ListBox display all data from the database. Can do a multiple selections as well but only one selected item possible to edit in a same time. I tried the following code but working only with the else branch but I want put my full code...
21 Nov 2022 by RickZeeland
Your code is almost right, just change the first line: if (listBox.SelectedItems.Count == 1)
1 Nov 2022 by Tshumore
I have a form whereby i need to do select multiple employees on a HtmlDropDownFor. I have used an SelectListItem and in my controller i am getting an exception 'Cannot implicitly convert type 'string' to string[] ' on the property pertaining to the employee i.e on line request.EmployeeNumber...
1 Nov 2022 by Rocky New
EmployeeNumber = new string[] { Convert.ToString(formcollection["EmployeeNumber"]) }; this solution only work in single index , can you please tell this statment with loop
22 Oct 2022 by ernteSKY
The following code working well with basics but I would like to update with speacial request. My_listBox.SelectedItems.Clear(); for (int i = My_listBox.Items.Count - 1; i >= 0; i--) { ...
7 Jul 2022 by AmirSina Mashayekh
Hello. I'm creating a windows program (Visual Studio - C#) to communicate with AVR MCU using serial port. I want to show available serial ports in a ListBox (or combobox) and update the list immediately when a new device is available or a device is disconnected. Currently I use a "Refresh"...
7 Jul 2022 by joseph wind
public List getCommPorts() { List devices = new List(); string[] ports = SerialPort.GetPortNames(); foreach (string portName in ports) { SerialPort...
6 Jun 2022 by Ivan Patrick Mandi
I have saved a list of JSON files stored in a local folder. What I've been trying to do is to view all those JSON files to a listbox then auto populate the JSON file selected in a series of textfields for each attribute. Here are the contents of...
20 May 2022 by susad
I have a WPF ListBox with the ItemsSource bound to a ViewModels list of MyObjects (products). This works fine, the lisbox will be filled with products from Database. Unfortunatly there are over 4.000 entries in the DB. When the user navigates to the listbox, a specific product entry should be...
3 Apr 2022 by Ashkan X
Does anyone know how to split items in listbox / 5? Automatically, this items / 5 and split to 5 other listboxes. For example, we have 1000 items in Listbox 1. I want to split these 1000 items to the 5 different listboxes. What code should I...
3 Apr 2022 by Maciej Los
Think of it. Imagine, you need only one loop. Based on your example - listbox1 has got 1000 items. Then you want to split it into 5 listboxes. int j = 0; for(int i=0; i
2 Apr 2022 by Tshumore
I have a ListBoxFor where i need to retrieve all the selected values. The selected values are in a string array of the form : string commaDelimited = "mihr20, emp1004, fbc567,mihr01";. Now when i attempt to separate the individual items between the commas i get type-casting exception 'Cannot...
2 Apr 2022 by Member 15588374
Seem to be some other reason for this error because I got the same error with the following code: foreach (string filePath in DirectoryBrowser.SelectedPath) { //.....do something with the path } filePath is declared as a string...
1 Feb 2022 by Richard-Dev
My problem is I need to sort the items in ListBox. These are my sample items in ListBox. 2021 - January 2021 - February 2022 - January 2022 - February 2021 - April This is my desired output. I want to sort using Year and then Month. 2021 -...
1 Feb 2022 by George Swan
It is not as difficult as it seems if you are familiar with LINQ. //read the lines into an array var lines = File.ReadAllLines(@"C:\temp\TestDates.txt"); List dates = new List(); ...
1 Feb 2022 by M Imran Ansari
In this scenario, You need perform some string operations with custom implementation. You can get you listbox items into a collection and perform split operation to seperate year and month values. You can get the listbox items like string[]...
31 Jan 2022 by OriginalGriff
Repost: Deleted. Please do not repost your question; use the Improve question widget to add information or reformat it, but posting it repeatedly just duplicates work, wastes time, and annoys people. I'll delete this one.
31 Jan 2022 by Richard MacCutchan
You already have the answer in your original question at Sort month and year in listbox C#[^]. Please do not repost.
31 Jan 2022 by OriginalGriff
The problem is that you can't do it like that: you are providing string based data, so the comparison is always going to be string based - and that means that the result of the entire comparison is based on the first different character in the...
22 Dec 2021 by asaad kittaneh
0 I'm building my first Tkinter GUI, and I need to create a multi-column Listbox using TKtreectrl, but when importing the module I get this error, import error: No module named "TkTreectrl". How to solve this problem, thanks in advance. What I...
22 Dec 2021 by Richard MacCutchan
You first need to install the library from its online repository. See TkTreeCtrl[^] for details.
11 Oct 2021 by candijen
The user enters a link in textBox1 and hits the button and the code below prints it in a listbox but only once. If the user enters another URL or the same URL it does not get saved into the listbox how can I take the user input multiple times...
11 Oct 2021 by Richard Deeming
Quote: ...WebForms... List hll = new List(); It looks like you're storing your list as a field within your page. That won't work in WebForms - every time the user causes a post-back, a new instance of your page class...
11 Oct 2021 by BillWoodruff
You just discovered the no-datasource-refresh when new Item added to DataSource behavior that most of us (ouch) discover. The best way to fix this is just to use a 'BindingList [^] // create and initialize at class level scope: private...
26 May 2021 by Nilay M Joshi
Multiselect Combobox - Custom control for WPF
29 Mar 2021 by Member 15076984
Hello. I'm trying to get a list of integers from Excel to my VB application by pulling them from the clipboard. The error is Conversion from string "908313 264357 914466 280352 " to type 'Integer' is not valid so it still a string and not a...
29 Mar 2021 by Jo_vb.net
I would paste the clipboard data to a multiline textbox. Then you see how the data really look. Then remove all blank characters and html formatting code from the string(s) 264357 Format/parse/split the string(s) and convert it to...
29 Mar 2021 by Maciej Los
Try this: Dim clipNumbers = clipboarddata.Split(New String(){Microsoft.VisualBasic.vbNewLine}, StringSplitOptions.RemoveEmptyEntries) _ .Select(Function(x) New SortedNumbers With {.RandomNumber =Math.Abs(Convert.ToInt32(x))}) _...
22 Nov 2020 by George Swan
My choice would be to use a value Tuple. Like this: public List namesList = new List(); //add an item namesList.Add((txtSurname.Text, txtFname.Text));...
22 Nov 2020 by Morke97
Hi guys, I'm creating a simple application that that takes firstName and surName input from text boxes, adds them to a List (currently using List) and a ListBox by clicking on a "Add" button. And I also have a button that has to sort the names...
22 Nov 2020 by PIEBALDconsult
Define a class, with the two values, a ToString for displaying them together, and a compare method which sorts by last then first.
22 Oct 2020 by izz moe
I am using drag and drop functionality by devexpress to fill a listbox by dragging items from a data grid the problem is not related to devexpress or dragging and dropping, it works just fine but the listbox.Items.Count is always 0 even if the...
8 Sep 2020 by Member 10190163
Does anyone know why the Listbox1.Refresh() command may not trigger the ListBox1_DrawItem sub every time?In Microsoft Visual Basic 2010, a listbox has a forcolor and backcolor property. These properties change the forcolour and backcolor for all the items in the listbox. By default there is...
8 Aug 2020 by Sandeep Mewara
Code in combobox selected change would be roughly: String[] files = System.IO.Directory.GetFiles("combobox selected path of folder"); for (i = 0; i
7 Aug 2020 by TheRealSteveJudge
If your filenames are like this e.g. 11111111111_a.txt or 11111111111_b.txt LINQ will help you. listBox1.DataSource = shortFilenames.Where(x => Path.GetFileNameWithoutExtension(x).EndsWith("a")).ToList(); listBox2.DataSource =...
7 Aug 2020 by Member 11396175
Hello friends !!!!! I have one folder with many txt files with filename 11111111111_a,11111111111_b,22222222_a,22222222_b How can separate to 2 listboxes by last digit? What I have tried: private void Form1_Load(object sender, EventArgs e) {...
7 Aug 2020 by Richard Deeming
Something like this should work: var fileNames = directoryInfo.EnumerateFiles() .Select(f => f.Name) .ToLookup(f => Path.GetFileNameWithoutExtension(f).LastOrDefault()); listBox1.DataSource = fileNames['a']; listBox2.DataSource =...
4 Aug 2020 by TheRealSteveJudge
First of all you must cast "sender" which is just an object to "ComboBox". Then you must get "SelectedValue". Finally you must get the files that are in the selected directory and use it as data source for the ListBox. private void...
4 Aug 2020 by Member 11396175
Good morning I think my question is very simple for your experience one combobox with specific path give me the folders that have in it I want when i select one folder from combobox the files in the folder placed in the list box. What I have...
4 Aug 2020 by Richard MacCutchan
See Path.GetFileName Method (System.IO) | Microsoft Docs[^].
28 Jun 2020 by Rodrigolo
i got my listbox (listBox1) i want to save the items inside and load them when form1 is loaded i've got no idea so please explain everything you can Thanks What I have tried: i treid looking for this in the internet , i found nothing some...
28 Jun 2020 by Maciej Los
Every .net application has got a configuration file: app.config already. If you don't see it, you can add it! See: How to add an app.config file to a project - Visual Studio | Microsoft Docs[^] Please, read this article on CP KnowledgeBase:...
28 Jun 2020 by OriginalGriff
Top be honest, they "use a file" faction is probably right - settings files aren't really there for data and they aren't really good at it. Using a file isn't difficult, and particularly with something like a list box it is the logical way to do...
28 Jun 2020 by Rodrigolo
I want to : When i click a button (xuiButton2) it opens a new form 2 it worked and in form 2 there is a listbox the items in that listbox are the same than the items in the other listbox What I have tried: I tried making that when...
28 Jun 2020 by OriginalGriff
Exactly how depends on the "relationship" between the two forms. Have a look at these, one of them will fit your circumstances. The form that creates an instance of another: MyForm mf = new MyForm(); mf.Show(); Is the "parent", the other form is...
24 May 2020 by Maciej Los
To display data in a tabular form, use DataGridView[^] control. You'll need to make some changes in your code: 1. Add new class: public class Föremål { public string Kurs { get; set; } public string Lärare { get; set; } public...
24 May 2020 by Severus Josefsen
I'm trying to align strings using string.format but it doesn't work as I want. What it looks like: Programmering P Matematik U Engelska V This is the code: public partial class Form1 : Form { List kurs = new...
24 May 2020 by OriginalGriff
It's because the font that the ListBox is using to display the strings is a proportional font: not all the characters are the same width. For example, "i" is much narrower than "W", and "." is really narrow while "#" is a lot wider. Change the...
12 Mar 2020 by Member 14771221
Hello, I'm working on a project. And I have a listbox in Form1 and a listbox in Form2. What I have already done is that I have a button that adds values from numericUpDown to listbox in Form1 (values from 1 to 999999). What I need help with is...
12 Mar 2020 by Maciej Los
In addition to @phil.o solution, i'd suggest to read these very interesting tips/articles: Transferring information between two forms, Part 1: Parent to Child[^] Transferring information between two forms, Part 2: Child to Parent[^] Transferring...
12 Mar 2020 by phil.o
You could give Form2 a constructor accepting an enumeration of integers as parameter, which you would use to populate the second listbox: public Form2() { InitializeComponent(); } public Form2(IEnumerable values) : this() { ...
9 Mar 2020 by Member 7713421
anyone find a solution to this? Experiencing the same thing with clickshare and not showing a WPF application. All other apps are showing fine.
9 Mar 2020 by JamesHadleyChase
Hello All,I use an Intel powered Dell laptop to run my application built using WPF 4.0 on Windows 7 64 bit OS.Problem:When I connect a secondary monitor to the laptop or plug the laptop to a projector, the WPF application behaves very strangely. Few of the controls like the Listbox,...
16 Jan 2020 by TheGoffy
Hello,How can I add a hyperlink as listbox item?Example:ListBox1.Items.Add(item, "http://www.google.com/")
26 Dec 2019 by Richard MacCutchan
This looks like the same issue as in your previous question at How do I resolve error 'cannot implicitly convert type 'string' to string[][^]. You need to understand the difference between a string, which is a single string of characters, and a string[], which is an array of string objects. As...
24 Dec 2019 by MadMyche
The reason that you are getting this error is that EmployeeNumber is declared as an array of strings (string[]) and you are attempting to populate it from a single string directly. I believe that you want this just to be a string, and not an array of them.OverTimeRequest.cs[Required]...
18 Dec 2019 by TheRealSteveJudge
A so-called behavior will help you. It is assumed that your application is called "MyApp". Then you could implement the wished behavior in a subfolder called Behaviors. using System.Windows.Controls; using System.Windows.Interactivity; namespace MyApp.Behaviors { public class...
18 Dec 2019 by Mark Millman
I have a ListBox which displays an ObservableCollection that grows in response to arbitrary events. I would like the oldest entry to be at the top and as I add new entries I would like the ListBox to scroll down so that the last row is always displayed. The default behavior is for the...
18 Dec 2019 by Gerry Schmitz
Scroll to the "last object" that was added. How you determine "last" is up to you. https://docs.microsoft.com/en-us/dotnet/api/system.windows.controls.listbox.scrollintoview?view=netframework-4.8
19 Oct 2019 by Richard MacCutchan
I have looked into this and it appears that because of data binding, the values returned in the SelectedItems list are DataRowView objects. Each of which contains the DataRow of the data source as a member. So the following code will list your items. foreach (DataRowView rowview in...
18 Oct 2019 by Member 14608841
i have ItemID, ItemName, Quantity, Date and ItemStatus from inventory table in sql database. i only want the ItemID and itemName the quantity will be reduced if i want 5 qty from selected item if item has 21 it should be reduce to 16 Capture5 — imgbb.com[^] when i add selected item from...
2 Oct 2019 by Maciej Los
Wel... Start here: Ready-to-use serial port enumeration list box[^] For further details, please see: SerialPort.GetPortNames Method (System.IO.Ports) | Microsoft Docs[^] [EDIT] There's no guarantee that SerialPort.GetPortNames method will return the names of ports. But, there's work-around....
5 Jul 2019 by Ahmad_kelany
hi everyone,i searched for changing ListBoxItem back color and persisting this selection color even if the control lost focusi found the solution like this :
5 Jul 2019 by Frank,Lin
MyListBox.Resources[SystemColo...
30 Mar 2019 by Member 14017552
C#, Windows Forms - I did almost 90% of the problem but Search Button is not working! Given Access file with the data (below). I need to build a program (GUI application) that uses LINQ to get data from the database file called (students.accdb). I need radio buttons to select and search by last...
30 Mar 2019 by OriginalGriff
Instead of searching, use filtering of your original data. It's easy enough to do: private BindingSource displaySource = new BindingSource(); private void frmMain_Shown(object sender, EventArgs e) { DataTable dt = new DataTable(); using...
14 Feb 2019 by Sonhospa
Hello, In my code I'm using the SelectedIndexChanged event of listboxes. I had to find out that this event seems to be automatically raised while the listbox gets filled with it's elements. First of all, after the listbox has been filled I want to have no item selected. I'm currently doing...
14 Feb 2019 by OriginalGriff
You could remove the event from the handlers list and add it back at the end: Add and remove event handlers dynamically in VB.NET[^] That effectively "disables" it while it's being filled.
31 Jan 2019 by ZurdoDev
As Richard mentioned in the comments, it should be pretty obvious, right? All of your code is inside of an if statement that will only run when i is less than 0. However, I is set to the count of the listbox items. How can I ever be less than 0? Simple debugging would have found this much...
31 Jan 2019 by Member 13998948
Good day guys, I'm a semi-intermediate here in c#. Actually, the said topic name it won't work with my codes implemented, i need some help on how to improve my timer tick procedure code as shown what I've tried below. What I have tried: here is my code as follows. private void...
18 Dec 2018 by Member 13751744
I have a dynamic listBox: ...
17 Dec 2018 by Vincent Maverick Durano
Not a WPF guy, but I found this discussion that may help you get started: Get selected items from checkbox in a list - C# WPF - Stack Overflow[^]
13 Dec 2018 by idkwhatimdoing
Hello I'm working on a code for an assignment and I can't figure out how to use my list boxes in equation like how to use the variables in the list boxes already. I need to add both variables together and present them on a label which I can not figure out how to do. To clarify I'm working in...
13 Dec 2018 by OriginalGriff
We can't really help you specifically based on that little - we can't see what you have in your list boxes! But at a guess, the list boxes contains strings, and you can't add them together directly. Instead, convert the strings to numbers first: Dim valueFromListBox1 As String = "666" ...
11 Dec 2018 by Afzaal Ahmad Zeeshan
Quote: I need to search one directory (c:\K_txt) of almost 3,000 .txt files Searching this on the fly is a really very bad idea, and would have to make your users wait each time they make a change in the query. A good approach would be to read your files once, and create tokens (words, in...
11 Dec 2018 by Member 13698711
hi, I am not good with coding - in vb.net, I need to search one directory (c:\K_txt) of almost 3,000 .txt files for either a word or exact phrase that the user enters, and then load up a listbox with the names of those txt files that contain the word or phrase. Then when the file in the listbox...
11 Dec 2018 by RickZeeland
See answer here: How to search specific string into text file from VB.NET[^] And here: VB.Net 2005: Search specific word in text file within a directory-VBForums[^]
11 Dec 2018 by CHill60
You could adapt the technique used in this CodeProject article File Searcher in C#[^] or this one WinSearchFile: how to search files on your PC[^] Or the techniques discussed here .net - c# Fastest string search in all files - Stack Overflow[^]
9 Dec 2018 by TABiSH777
Hi, please try changing the scroll viewer in the control template of Listbox. please check this Link. In the question itself user has tried implementing that.
9 Dec 2018 by Member 14007122
1. I was made List box in form 2. but I wanna enlarge width of Scroll Bar. So, Customize a new Scroll Bar using VScroll bar in 'Tool Box' 3. but I don't know Combine VScroll Bar with List box and How do I scroll down List box I was made... 4. I think.. Using Event " .Scroll += new...
30 Nov 2018 by Michael Shpilt
A WPF tutorial on how to create FilteredListView: A ListView custom control with search filter that uses Throttling.
11 Nov 2018 by M.Kamran Asim
OK First Create a public ListItem property and Bind it to TextBox public ListItem SelectedItem { get=>selectedItem; set { selectedItem=value; RaisePropertyChange(); } } Second...