How To Fill A ComboBox With Items From .TXT File Using C#
In This C# Tutorial We Will See How To Import And Display Data From a Text File to a Combobox Using datatable + File.ReadAllLines() and For Loop In Csharp Programming Language And Visual Studio Editor.
Project Source Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace Csharp_Tutorials
{
public partial class TEXT_FILE_TO_COMBO : Form
{
public TEXT_FILE_TO_COMBO()
{
InitializeComponent();
}
private void TEXT_FILE_TO_COMBO_Load(object sender, EventArgs e)
{
string[] lines = File.ReadAllLines(@"C:\Users\1BestCsharp\Desktop\table2.txt");
string[] data = new string[lines.Length];
for(int i = 0; i < lines.Length; i++)
{
data[i] = i + 1 + " > " + lines[i];
}
comboBox1.DataSource = data;
}
}
}
///////////////OUTPUT:
Download Projects Source Code