I am trying to align the text in the attachment

Thats my code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Program_No2_Project
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
      
        private void detailsButton_Click(object sender, EventArgs e)
        {
            Friend[] friends = new Friend[6];
            string[] lines = File.ReadLines("namenANDumber.txt").ToArray<string>();
            char[] ch = { ' ' };
            for (int i = 0; i < friends.Length; i++)
            {
                friends[i] = new Friend();
                string[] splits = lines[i].Split(ch);
                friends[i].Name = splits[0] + splits[1];
                friends[i].Phone = splits[2];
            }
            List<Friend> li = friends.OrderBy(c => c.Name).ToList<Friend>();
             foreach (Friend f in li)
             {
                 richTextBox1.Text += f.Name.ToString() + " " + f.Phone.ToString() + "\n\n"; 
             }
        }

        }
        public class Friend
        {
            private string name;
            private string phone;

            public string Name
            {
                get
                { return name; }
                set
                { name = value; }
            }
            public string Phone
            {
                get
                { return phone; }
                set
                { phone = value; }
            }
        }
    }

Recommended Answers

All 9 Replies

Hi,
what allignment is concerned, I would strongy suggest to use some other control like listView, or dataGridView.
What do you think?
YOu can still edit data there.

Line 23: Easier to write File.ReadAllLines("namenANDumber.txt") Line 35: richTextBox1.Text += String.Format("{0,20} {1}\n\n",f.Name, f.Phone); There is no need to call ToString() on Strings.

True but then

string[] splits = lines[i].Split(ch);

gives me an error

I can't magically read the error message on your screen. Could you possibly tell me what it is?

Error 1 The name 'lines' does not exist in the current context 28 35 Program No2 Project

in this loop

for (int i = 0; i < friends.Length; i++)
            {
                friends[i] = new Friend();
                string[] splits = [B]lines[i][/B].Split(ch);
                friends[i].Name = splits[0] + splits[1];
                friends[i].Phone = splits[2];
            }

Line 23 wasn't an entire replacement for your code, thus the inline code rather than a code block.

string[] lines = File.ReadAllLines("namenANDumber.txt");

I appreciate your advice, i think i am going to stay with what i have its working.

I appreciate your advice, i think i am going to stay with what i have its working.

Yes, you should fear new things and learning. It's bad for you.

its the smell of Sarcasm in the air, its a great smell i'am quit sure i could've smelled it from your place i am in the US so wherever you are do the math

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.