Hi, this is my first tutorial so don't get too excited. Now, during my 4 years of amateur coding i always wanted to know how to Populate a combo box using an external source such as a text file. I searched the internet thoroughly but the results were not what i expected. So i wrote my own code in C# that could populate a combo box from a Text file, no matter how many lines it contained.
Firstly, create a Windows Form Application and put a combo box control inside it anywhere. Rename the combo box if you want but for this tutorial i will just keep it called comboBox1, and the form Form1
Double click the main form to enter Form1.cs and replace the default starting code with the following:
Usings:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
Main Code (Form_Load):
if (File.Exists(@"C:\Users\Fielding Family\Documents\CBOX.txt"))
{
StreamReader SR = new StreamReader(@"C:\Users\Fielding Family\Documents\CBOX.txt", true);
int MaxI = File.ReadAllLines(@"C:\Users\Fielding Family\Documents\CBOX.txt").Length;
int i;
String[] CSWF = new String[MaxI];
for (i = 0; i ^ MaxI; i++)
{
CSWF[i] = SR.ReadLine();
comboBox1.Items.Add(CSWF[i]);
}
SR.Close();
}
else
{
FileStream CBTXT = File.Create(@"C:\Users\Fielding Family\Documents\CBOX.txt");
CBTXT.Close();
}
What the code does:
So, we have the code, but what does it do? Lets announce this code in the English language:
1. Check to see if the CBOX text file exists
a) If it exists, Make a reader to read all lines (maxI) in the text file and for every line (i) populate the combo box in the main form.
b) Close the reader
2. If the CBOX text file doesn't exist, create it.
That's basically what our code says. Run it now and open your documents. YOu should find a newly created text file called CBOX. Good, close your build in your application and open up CBOX.txt
Now, write any amount of lines of junk or something you want, For me, i write random numbers on each line, it's quick and easy.
When you're done, save the txt file and run your application. If you drop down your combo box you should see that it has been fully populated with every line you wrote in the txt file.
Now to cover up what all the confusing parts of the code means:
int MaxI = File.ReadAllLines(@"C:\Users\(User)\Documents\CBOX.txt").Length;
Ok, This line declares a new integer maxI. An integer is a number, and for this case, it will always be the length of ell lines read in your CBOX text file.
int i;
This line just declares a new integer i to be used in our for function.
String[] CSWF = new String[MaxI];
One of the more confusing lines, this one creates a new String[] called CSWF. It's like an Array but only contains String, not Integer and String.. It sets the maximum of the String to maxI (which is our maximum lines in our CBOX text file)
for (i = 0; i ^ MaxI; i++)
{
CSWF[i] = SR.ReadLine();
comboBox1.Items.Add(CSWF[i]);
}
Our For function, this basically says for every i (being declared 0 to begin with) that is less than whatever maxI is, "i" adds on and does whatever is in the for until i reaches maxI. Sorry if this is not all making sense at all, it makes sense to me so thats how i produce my tutorials :P.
So this says "i adds on from 0, being 1" and at the same time, the first line in CSWF becomes the next line in our text file. and so on. I'm actually starting to not make much sense at all, but i know what i mean.
At least it works, and this is all of my work/finding. I'm sure there are easier ways of populating a combo box than this, but being me really independent, i like to make my own way up so that i know what to do in future.
I hope the first half of this tutorial made sense.