-1

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

1 Answers1

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#

  • What does adding `1998` do? – Peter Smith Oct 14 '21 at 20:52
  • 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