Call List from Class

MiPakTeh 1,476 Reputation points
2022-07-23T11:12:47.087+00:00

Test, call Data1 from Class "Check_" and show in listbox1.

Class;

using System;  
using System.Collections.Generic;  
using System.IO;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
  
namespace Str3Number_1  
{  
    public class Check_  
    {  
        private List<string> Data1 = new List<string>();  
  
  
        public void FileOperations()  
        {  
            foreach (var s in File.ReadLines(@"C:\Users\family\Documents\3digit.txt"))  
            {  
                Data1.Add(s);  
            }  
              
        }  
  
  
    }  
}  

Form1;

using System;  
using System.Collections.Generic;  
using System.ComponentModel;  
using System.Data;  
using System.Drawing;  
using System.IO;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
using System.Windows.Forms;  
  
namespace Str3Number_1  
{  
    public partial class Form1 : Form  
    {  
        Check_ Result_ = new Check_();  
  
        public Form1()  
        {  
            InitializeComponent();  
        }  
  
        private void Form1_Load(object sender, EventArgs e)  
        {  
        }  
        public void viewAllData()  
        {  
            var lst = Result_.FileOperations();  
            listBox1.Items.Add(lst);  
        }  
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)  
        {  
            viewAllData();  
  
        }  
  
        private void button1_Click(object sender, EventArgs e)  
        {  
  
        }  
  
    }  
}  
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,838 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,306 questions
{count} votes

Accepted answer
  1. Karen Payne MVP 35,196 Reputation points
    2022-07-23T13:30:12.86+00:00

    Unsure what the issue is but perhaps this might help, two choices to read lines from a file.

    using System.Collections.Generic;  
    using System.IO;  
    using System.Linq;  
      
    namespace YieldReadFileFormDemo  
    {  
        public class FileOperations  
        {  
            public static IEnumerable<string> ReadYield(string fileName)  
            {  
                var lines = File.ReadAllLines(fileName);  
      
                foreach (var line in lines)  
                {  
                    yield return line;  
                }  
            }  
      
            public static List<string> Read(string fileName)   
                => File.ReadAllLines(fileName).ToList();  
        }  
    }  
    

    Usage

    using System;  
    using System.Windows.Forms;  
      
    namespace YieldReadFileFormDemo  
    {  
        public partial class Form1 : Form  
        {  
            public Form1()  
            {  
                InitializeComponent();  
            }  
      
            private void YieldButton_Click(object sender, EventArgs e)  
            {  
                foreach (var item in FileOperations.ReadYield("TextFile1.txt"))  
                {  
                    listBox1.Items.Add(item);  
                }  
            }  
      
            private void ExpressionButton_Click(object sender, EventArgs e)  
            {  
                listBox2.DataSource = FileOperations.Read("TextFile1.txt");  
            }  
        }  
    }  
      
    
    0 comments No comments

0 additional answers

Sort by: Most helpful