1

I'm trying to read from a document that has a list of students with their first name, last name, student id, email.

I've successfully created/designed the xmal file with a listBox (see attachment).enter image description here

But I'm having difficulties writing the code in the try section, which will read from the document into the listBox in such order that each of the specific read data must go under those relevant textblocks. for instance first name should go under first name, and so on.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;

namespace MemberList
{

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }


        string filePath = @"C:\Users\User\DEMO\MemberList.csv";

        private void Member_List_Load(object sender, EventArgs e)
        {
            FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            StreamReader sr = new StreamReader(fs, Encoding.Default);

            List<string> lines = File.ReadAllLines(filePath).ToList();

            while (!sr.EndOfStream)
            {

                try
                {



                    foreach (var line in lines)
                    {
                        string[] entries = line.Split(';');
                        textblock1 = entries[0];
                        textblock2 = entries[1];
                        textblock2 = entries[2];
                        textblock2 = entries[3];

                    }

                }
                catch (Exception)
                {

                    throw;
                }
                finally
                {

                }
            }
        }
    }
}

1 Answers1

0

You're not using the recommended MVVM pattern; I'm not going to sermon you about that, just look into it to build better applications in the future.

So, about your actual problem, you should use x:Name instead of Name for your text boxes; look at this question and its answers. This way, they will be referencable in your code behind, and your code should work.

Edit 1: You should also use the Text property of your text boxes in you code behind:

textbox1.Text = entries[0];
Kilazur
  • 3,089
  • 1
  • 22
  • 48
  • As I read through the link, they mention about the disadvantages of memory issue while using x:name! –  Aug 12 '18 at 09:42
  • 1
    There is of course some overhead to be able to reference an UI element in code behind; but this is also the only built-in solution, and most likely the most efficient one. Really, I think every WPF dev uses `x:Name` when they need to access a control in code behind; `Name` only allows to reference it in XAML. – Kilazur Aug 12 '18 at 09:45
  • but I still haven't got the answer for the code part. changing to x:name didn't change anything, though. –  Aug 12 '18 at 09:56
  • Yeah, you should use `textbox1.Text` instead of `textbox1`. – Kilazur Aug 12 '18 at 09:59