---
title: "How to populate a listbox from a ,text file at startup"  
description: "How to populate a listbox from a ,text file at startup"  
author: "Anonymous User"  
published: 2013-12-18  
updated: 2013-12-18  
canonical: https://www.mindstick.com/forum/1806/how-to-populate-a-listbox-from-a-text-file-at-startup  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# How to populate a listbox from a ,text file at startup

I need to [populate](https://www.mindstick.com/blog/60/populate-records-in-second-listbox-according-to-the-selection-in-first-list-box) a [listbox](https://www.mindstick.com/articles/626/listbox-events-in-c-dot-net) from a [text file](https://www.mindstick.com/forum/12828/converting-a-text-file-to-an-array-in-java) at startup. I also need to have the firt 2 lines eliminated from the listbox along with all blank lines, but that isn't to important [right now](https://answers.mindstick.com/qa/36863/what-would-happen-if-gravity-became-5-percent-stronger-right-now). I am currently stuck at getting the listbox populated at all. Here is my code so far:

```
struct CDCLocationEntry{    public string name;}public partial class StartupForm : Form{    private List<CDCLocationEntry> CDCList = new List<CDCLocationEntry>();    public StartupForm()    {        InitializeComponent();    }    private void ReadFile()    {        try        {            StreamReader inputFile;            string line;           CDCLocationEntry entry = new CDCLocationEntry();            inputFile = File.OpenText("P3S1 Data File For Import.txt");            while (!inputFile.EndOfStream)            {                line = inputFile.ReadLine();                CDCList.Add(entry);            }        }        catch (Exception ex)        {            MessageBox.Show(ex.Message);        }    }    private void DisplayText()    {        foreach (CDCLocationEntry entry in CDCList)        {            CDCLocationListBox.Items.Add(entry.name);        }    }    private void StartupForm_Load(object sender, EventArgs e)    {        ReadFile();        DisplayText();    }
```

[visual studio](https://www.mindstick.com/articles/12378/visual-studio-for-mac-is-out-of-beta-preview-now-officially-available) is say my [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic) is here:

struct CDCLocationEntry { public string name; }

the [message](https://www.mindstick.com/forum/12802/show-confirmation-message-yes-or-no-in-asp-dot-net) I'm getting is:

[Warning](https://answers.mindstick.com/blog/77/psychological-risks-of-ai-chatbots-what-experts-are-warning-about) 1 Field 'Project_3___Section_1.CDCLocationEntry.name' is never assigned to, and will always have its [default value](https://www.mindstick.com/forum/2035/default-value-when-using-singleordefault) null

none of my notes or online help is giving me an answer for this.

Any help that you can provide would be greatly appreciated

## Replies

### Reply by Anonymous User

Hi Pravesh,\

You need to create a CDCLocationEntry instance inside the loop and assign, at this instance property name, the line coming from your file

```
inputFile = File.OpenText("P3S1 Data File For Import.txt");while (!inputFile.EndOfStream){    CDCLocationEntry entry = new CDCLocationEntry();    entry.name = inputFile.ReadLine();   
CDCList.Add(entry);}
```

Your actual code creates just one instance of CDCLocationEntry outside the loop and, without assigning anything to the name property, adds this same instance in every loop.


---

Original Source: https://www.mindstick.com/forum/1806/how-to-populate-a-listbox-from-a-text-file-at-startup

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
