I would like to insert items in a combo list from txt file. Can anyone help me? I have recently been programming and I hope to receive a simple answer if possible. Thanks in advance
Asked
Active
Viewed 78 times
-1
-
Please provide enough code so others can better understand or reproduce the problem. – Community Oct 15 '21 at 19:46
1 Answers
0
This is by integrating the method of reading text files with the addition to combo box, like this code
// Normal way: Adding elements in the combobox
comboBox1.Items.Add(1998);
// Read a text file line by line.
string[] lines = File.ReadAllLines("C:\\Users\\Mohammad Yaser Ammar\\Desktop\\ComboList.txt"); //using System.IO;
foreach (string line in lines)
{
// Adding elements in the combobox from txt file
comboBox1.Items.Add(line);
}
Good luck on your journey with C#

Mohammad Yaser Ammar
- 46
- 1
- 4
-
-
It is just an example to illustrate the difference between the traditional adding method, and how it is associated with the file with foreach, @PeterSmith – Mohammad Yaser Ammar Oct 14 '21 at 21:07
-
But in this way, I can work only on my pc? If I use code on the other pc don’t found the path. It’s all right? Then…where I put this code? In the event combo? – Mirko Tangredi Oct 14 '21 at 21:56
-
@MirkoTangredi yes, Because that can use path of project or in debug folder This will help you in ways to do that: https://stackoverflow.com/questions/816566/how-do-you-get-the-current-project-directory-from-c-sharp-code-when-creating-a-c – Mohammad Yaser Ammar Oct 14 '21 at 22:00
-
Thank so much....I Solved by only 1 code line :-) public txt_sfd() { InitializeComponent(); //In questo modo alimenta la combo tramite file txt presente nel progetto processi_combo.ItemsSource = File.ReadAllLines(AppDomain.CurrentDomain.BaseDirectory + "\\Cs_Code\\File da leggere\\processi.txt"); } – Mirko Tangredi Oct 15 '21 at 09:45
-
@MirkoTangredi Beautiful, creativity after research is important during programming – Mohammad Yaser Ammar Oct 15 '21 at 11:25