473,396 Members | 2,018 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

how to copy the contents of text file into list box

i have a text file having employee id,name anb phone number seperated by :
i want to bring all the employee ids into one listbox of windows form so that user can select one out of it. and when user selects one employee id out of it corresponding name and phone number should come in other two textboxes of the same form..how can i do this?
Sep 20 '10 #1
1 6298
Christian Binder
218 Expert 100+
First create a data-structure which contains all employee's details.
e.g.
Expand|Select|Wrap|Line Numbers
  1. public class Employee {
  2.   public string Id { get; set; }
  3.   public string Name { get; set; }
  4.   public string Phone { get; set; }
  5. }
  6.  
then I'd create a buffer containing the file's content.
I'd use a List<Employee>.

The third step would be reading the file into the buffer.
File.ReadAllLines() will return an array where each entry represents a line in the file. Iterate over this array and use string.Split to separete by ':'
Then store the results in the buffer.
Expand|Select|Wrap|Line Numbers
  1. public void Read() {
  2.   List<Employee> employees = new List<Employee>();
  3.   foreach (string line in File.ReadAllLines(@"C:\emps.txt")) {
  4.     string[] details = line.Split(':');
  5.     if (details.Length >= 3) {
  6.       Employee emp = new Employee() { 
  7.         Id = details[0],
  8.         Name = details[1],
  9.         Phone = details[2]
  10.       };
  11.       employees.Add(emp);
  12.     }
  13.   }
  14. // assign buffer to list box
  15.   listBox1.DataSource = employees;
  16.   listBox1.DisplayMember = "Id";
  17. }
  18.  
The listbox is now filled with the values of employees and the Id-property will be displayed.

If the selection in the listbox changes, we want to display the selected employee's details.

So we use the SelectedIndexChanged-event of the ListBox. As the items of the ListBox are of type Employee, we cast the selected item and display its details.
E.g.
Expand|Select|Wrap|Line Numbers
  1. if (listBox1.SelectedItem is Employee) {
  2.   Employee emp = (Employee)listBox1.SelectedItem;
  3.   textBox1.Text = emp.Name;
  4.   textBox2.Text = emp.Phone;
  5. }
  6.  
Hope this helps :-)
Sep 20 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: jannordgreen | last post by:
I use windows 2000 and Visual Studio 2003. I have a vbnet web application on our intranet that needs to read a text file that sits on a different server. The general user does not have access to...
1
by: mhodkin | last post by:
What is the best way to fill an array (or other container) with values from a text file containing numbers in a simple column, especially when one doesn't know how many values the text file list...
1
seshu
by: seshu | last post by:
yes guys this is seshu now i am able to see all the files in a folder but now wat i want is i have a button with name copy once after clicking this i want to copy all the files ahown in file list box...
6
by: ivan.perak | last post by:
Hello, im a beginner in VB.NET... The thing i would like to do is as it follows.... I have a text file (list of names, every name to the next line) which is about 350000 lines long. I would...
1
by: vjunkl | last post by:
Hello, I have a content text file in my project and I'd like to copy it to a folder at some point in the execution of the application(not during a build). If this is not possible, how about...
4
by: manoop | last post by:
Hi friends How to bulk copy the output of a query to textfile in PL/SQL Thanks Manoop
7
by: ahmed222too | last post by:
how can i copy a text file and paste it in a specific path by VB6 code another question how can i create afolder in a specific path by VB6 thank you
2
by: ahmed222too | last post by:
when i use this code to copy a text file and paste it in a specific path: Dim fso As New FileSystemObject fso.CopyFile "C:\Files\file.txt", "C:\Files1\file_copy.txt" this error appear ...
6
by: Claire | last post by:
I've noticed after copying a text file line by line and comparing, that the original had several bytes of data at the beginning denoting its encoding. How do I use that in my copy? My original...
3
MindBender77
by: MindBender77 | last post by:
Hello All, I've been fussing with this for the last few days. I'm attempting to copy a text file to a zebra printer that is wired to com 12. I've been using Window's command prompt to test this,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.