hi everyone, how would i convert a console application (which reads records and sorts the data by date by loading from a textfile) to a windows application? and also what code would i use to load, edit and save the text file into a textbox via a 2nd form? many thanks

Recommended Answers

All 8 Replies

Depends what you would like to do. Maybe there is nothing yet to change, maybe its a lot. Show us your code, and tell us what you would like to be different in win app.

sorry for the late reply, here is my full code, what i would like different is for the records to be loaded onto the textbox, with buttons to scroll from 1 record to next, then 2 buttons to edit and save the text file which would take the user to a 2nd form, many thanks

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Data;
namespace Brennan_Neil_6114ENG_CW1
{
    class Program
    {

        private struct person //creates new person structure//
        {
            public int list; //creates an integer called list
            public string lastName; //public string is created for each part of the record
            public string firstName;
            public string middlename; 
            public DateTime dob; //datetime class is used for date of birth
            public string gender; 
            public string toString() // to string is used as an inheritance method

            {
                string file = string.Format("Person :{5}\n\tLast Name :{0}\tFirst Name :{1}\tMiddle Name :{2}\n\tBirth Date :{3}\t Gender :{4}", lastName, firstName, (middlename.Trim() == string.Empty ? "\t" : middlename), string.Format("{0:yyyy/MM/dd}", dob.Date), gender, list + 1); //this shows how the string will be formatted for each field and in what order
                return file;
            }
        }
        private person[] people; //creates a new structure for people, which is from the class person
        private string[] textfile; //new variable created
        private string[] date; //new variable created
        private int lst = 0; //creates a new integer called lst
        public Program()
        {
            try //exeption handling method
            {
                textfile = File.ReadAllLines(@".\csharp.txt"); //reads the records from the textfile
            }
            catch (FileNotFoundException ex) //exception handling method used if the file cannot be read, using a class called filenotfound exception
            {
                Console.WriteLine("File not found" + ex.Message.ToString()); //lets the user know the file could not be found
                return; //jump statement that affects control flow in the program
            }
            people = new person[textfile.Length];
            date = new string[textfile.Length];
        }
        private bool keepinfo() //a new structure is created
        {
            bool checkfile = true; //here the variables are declared for the boolean values
            char[] store = { ',' };
            string[] txt;
            bool checkrecord = true;
            try
            {
                for (int t = 0; t < people.Length; t++) 
                {
                    txt = textfile[t].Split(store); //here the arrays are created for each record and split
                    people[t].lastName = txt[0]; 
                    people[t].firstName = txt[1];
                    people[t].middlename = txt[2];
                    people[t].dob = Convert.ToDateTime(txt[3]);
                    people[t].gender = txt[4];
                    people[t].list = t;
                    date[t] = txt[3];
                    if (txt.Length > 5)
                    {
                        checkrecord = false;
                        break;
                    }
                }
            }
            catch (Exception) //execption represents any errors that occur
            {
                checkrecord = false; //the false statements are used if there is any problems with the textfile
            }
            if (!checkRecord())
            {
                Console.WriteLine("error in record");
                checkfile = false;

            }
            if (checkrecord == false)
            {
                Console.WriteLine("invalid number of fields");
                checkfile = false;
            }
            return checkfile;
        }
        private bool checkRecord()
        {
            char[] record = { '/' };
            try
            {
                if (people.Length > 10) //array for the text file, which is 10 records
                {

                    Console.WriteLine("error, more than 10 records");
                    return false;
                }
                for (int i = 0; i < people.Length; i++) //for loop for array student
                {
                    if (people[i].lastName.Trim() == string.Empty || people[i].firstName.Trim() == string.Empty || people[i].gender.Trim() == string.Empty || date[i].Trim() == string.Empty) //the trim method is used to cut out any spaces in the string
                    {
                        Console.WriteLine("one of the fields is not correct"); // return false statement
                        return false;
                    }
                    char[] chr = people[i].gender.Trim().ToUpper().ToCharArray(); //copies the characters in this array
                    if (people[i].gender.Trim().Length > 1)
                    {
                        Console.WriteLine("gender should be one character only");
                        return false;
                    }
                    else if (chr[0] != 'F' && chr[0] != 'M')
                    {
                        Console.WriteLine("Gender needs to be M or F");
                        return false;
                    }
                    string[] sortDate = date[i].Split(record);

                    if (people[i].dob.Year.ToString() != sortDate[0].Trim())
                    {
                        Console.WriteLine("Incorrect dob format 0");
                        return false;
                    }
                    else if (people[i].dob.Month != Convert.ToInt16(sortDate[1].Trim()))
                    {
                        Console.WriteLine("Incorrect dob format 1");
                        return false;
                    }
                    else if (people[i].dob.Day != Convert.ToInt16(sortDate[2].Trim()))
                    {
                        Console.WriteLine("Incorrect dob format 2");
                        return false;
                    }
                }
            }
            catch (Exception) // exception is used for any errors in the application
            {
                return false;
            }

            return true;
        }
        private void sortrecords() //sorts the arrays into relevent order, here the arrays are checked by dob
        {
            person set;
            int s;
            for (int i = 1; i < people.Length; i++)
            {

                for (s = 0; s < people.Length; s++)
                {
                    if (people[i].dob.Date > people[s].dob.Date)
                    {
                        set = people[i];
                        people[i] = people[s];
                        people[s] = set;
                    }
                }

            }
            for (int i = 0; i < people.Length; i++)
                people[i].list = i;
            people.ElementAt(0);
        }
        private void sortpeople() //the arrays are copied for sortstudent
        {
            person sort; // creates new variable from class person
            int s;
            for (int i = 1; i < people.Length; i++)
            {

                for (s = 0; s < people.Length; s++)
                {
                    if (people[i].dob.Date < people[s].dob.Date)
                    {
                        sort = people[i];
                        people[i] = people[s];
                        people[s] = sort;
                    }
                }
            }
            for (int i = 0; i < people.Length; i++)
                people[i].list = i;
            people.ElementAt(0);
        }
        private void convertString(int indx)
        {
           //the following code is used for the user input, variables such as indx,records and student represent the arrays of the records, once the relevent keys are pressed, the data is converted to show the results on the screen, breaks are used to terminate the loops
            Console.WriteLine("Press N + B to scroll though the records, S to sort, G for gender, O for date of birth, F for first name, L for last name, M for middle name and X to exit");
            Console.WriteLine(people[indx].toString());
        }
        static void Main(string[] args)
        {
            Program use = new Program();
            bool enter = use.keepinfo(); 
            int records = 0;
            ConsoleKeyInfo key;

            Console.WriteLine("Please press any key to start");
            Console.ReadKey(true);
            Console.Clear();
            use.convertString(records);
            while (enter)
            {

                key = Console.ReadKey(true);
                switch (key.Key)
                {
                    case ConsoleKey.N:
             {
            Console.Clear();
            use.convertString((records=(records++ >= use.people.Length-1 ? use.people.Length - 1 : records)));
            break;
            }
             case ConsoleKey.B:
            {
                   Console.Clear();
                            use.convertString((records=(records-- <= 0 ? 0 : records)));
                            break;
              }

              {
              }
             case ConsoleKey.S:
                        {
                            use.sortpeople();
                            Console.Clear();
                            records = 0;
                            use.convertString(records);
                            break;
                        }
           case ConsoleKey.L:
                      {
                            Console.Clear();
                            use.convertString(records);
                            Console.WriteLine();
                            Console.WriteLine("Last Name:\t" + use.people[records].lastName);
                            break;
                        }
            case ConsoleKey.F:
                        {
                            Console.Clear();
                            use.convertString(records);
                            Console.WriteLine();
                            Console.WriteLine("First Name:\t" + use.people[records].firstName);
                            break;
                        }
            case ConsoleKey.M:
                        {
                            Console.Clear();
                            use.convertString(records);
                            Console.WriteLine();
                            Console.WriteLine("Middle Name:\t"+use.people[records].middlename);
                            break;
                        }
           case ConsoleKey.O:
                        {
                            Console.Clear();
                            use.convertString(records);
                            Console.WriteLine();
                            Console.WriteLine("Date of birth:\t" + "{0:yyyy/MM/dd}", use.people[records].dob.Date);
                            break;
                        }
            case ConsoleKey.G:
                        {
                            Console.Clear();
                            use.convertString(records);
                            Console.WriteLine();
                            Console.WriteLine("Gender:\t\t" + use.people[records].gender);
                            break;
                        }
             case ConsoleKey.X:
                        {
                            enter = false;
                            break;
                        }
                    default:
                        {
                           Console.WriteLine("press X to exit"); 
                            enter = Console.ReadKey(true).Key == ConsoleKey.X ? false : true; //checks to see if the input is true or fals
                            Console.Clear();
                            use.convertString(records);
                            break;
                        }
                }

            }
        }
    }
}

i also dont need the sort records bit, but just each record loaded seperatly

Instead of usingf Console.WriteLine, you can use StringBuilder class (make is a class or form variable), and use AppendLine() method to insert text.
On the end you can show the data in MessageBox or in some other control (like label).

Instead of Console.ReadLine, you can use some textBox control so user can insert some text, and then to make it all work, use a button and its Click event - to start the code.

try to change it by your self, and lets see if you can do it (and I know you can).
bye

thanks for you help

do i declare the strings before all the event buttons? would i be better using a dialogue box to save the data, i am new to all this. I am not asking for a solution just to be pointed in the right direction

As you aware by now, there is many differences between console and windows application.
Even in the sintax its self (when declaring and instantiating variables).

Its hard to answer on your question, the best answer is, that you have to ask your self in which scope you are gonna use variables, is it gonna be in a whole form (class), or just inside some method or event. I cannot know that answer. But for the beginning, you can create then class accesaible (this is my suggestion).
If you wanna save data (like some text files for example), you can use SaveFileDialog class (if you wanna that the user decides the destination), if not, better specfiy it by your self (this is when operating with data (files) that you dont want that user knows about them, like its a database, or xml files).

If you have any other questions, please just go ahead, I`ll try to answer you as best as possible.

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.