Forum guidelines
AuthorQuestion
adding records StreamWriter PROBLEM
Posted on: 18 Dec 2007

Well, Here is my current codes in the Windows Application program

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


// add private fields
private string[] lines;
private int currentLine = -1; // dummy value


        private void frmCustomerDetails_Load(object sender, EventArgs e)
        {

            //Reads records from a file
            lines = File.ReadAllLines(@"C:\mp2.txt");
            // display first one
            DisplayRecord(0);

        }

        private void DisplayRecord(int index)
        {
            // don't do anything if index is outside bounds of lines array
            // or if requested line is already displayed
            if (index < 0 || index >= lines.Length || index == currentLine)
               return;
            string[] records = lines[index].Split('#');
            txtCustomerID.Text = records[0];
            txtContactPerson.Text = records[1];
            txtAddress.Text = records[2];
            txtContactPerson.Text = records[3];
            txtContactNo.Text = records[4];
            currentLine = index; // update currentline 
        }


        private void btnFirst_Click( object sender, EventArgs e)
        {
             DisplayRecord(0);
        }

        private void btnNext_Click( object sender, EventArgs e)
        {
             DisplayRecord(currentLine + 1);  
        }

        private void btnPrev_Click( object sender, EventArgs e)
        {
            
             DisplayRecord(currentLine - 1);  

        }
 
        private void btnLast_Click( object sender, EventArgs e)
        {
              DisplayRecord(lines.Length - 1);
        }

 

Is there a way I could add records from the existing notepad (mp2.txt), wherein, you could still display all the previous items

plus the one you've added? Another, is there a possibilty that I could still edit my previous record from the 'mp2.txt' and save it

afterwards?

here's the thing

btn_Add - Adds a new record...then put on the last record of mp2

btn_Edit - edits the the one who is currently displayed by the textboxes...

btn_Save - saves the changes when Edit process was finished already....

btn_Cancel - if you wish to cancel the edit process..

 

(by the way, once added a new record, there should have an input recorded to the mp2.txt as well at the last part)
 

 

Sorry if I ask too much. I'm kinda confused with all these stuff. Noob me.
 
 
 
  
 


chill~

 

AuthorReply
Alan
  • 0
  • 0
Re: adding records StreamWriter PROBLEM
Posted on: 18 Dec 2007  

To do all that is going to require some changes to the existing code as well as a lot of extra code. In particular, you're going to have to store the lines in a generic List rather than an array as the latter cannot be increased in size, dynamically. The following is (as usual) off the top of my head as I haven't got time to test it, so don't panic if there are bugs:

// add enum definition within form
private enum Mode{ Display, Add, Edit};

// add private fields
private List<string> lines = new List<string>();
private int currentLine = -1; // dummy value
private Mode mode = Mode.Display; 

        private void frmCustomerDetails_Load(object sender, EventArgs e)
        {

            // change TextBoxes to readonly so they can't be edited for now
            ToggleReadOnly(true);
            // disable Save and Cancel buttons
            btnSave.Enabled = false;
            btnCancel.Enabled = false;
            //Reads records from a file
            lines.AddRange(File.ReadAllLines(@"C:\mp2.txt"));
            // display first one
            DisplayRecord(0);

        }

        private void DisplayRecord(int index)
        {
            // don't do anything if index is outside bounds of lines list
            // or if requested line is already displayed
            if (index < 0 || index >= lines.Count || index == currentLine)
               return;
            string[] records = lines[index].Split('#');
            txtCustomerID.Text = records[0];
            txtContactPerson.Text = records[1];
            txtAddress.Text = records[2];
            txtContactPerson.Text = records[3];
            txtContactNo.Text = records[4];
            currentLine = index; // update currentline 
        }

        private void ToggleReadOnly(bool value)
        {
            txtCustomerID.ReadOnly = value;
            txtContactPerson.ReadOnly = value;
            txtAddress.ReadOnly = value;
            txtContactPerson.ReadOnly = value;
            txtContactNo.ReadOnly = value;
        }

        private void btnFirst_Click( object sender, EventArgs e)
        {
             DisplayRecord(0);
        }

        private void btnNext_Click( object sender, EventArgs e)
        {
             DisplayRecord(currentLine + 1);  
        }

        private void btnPrev_Click( object sender, EventArgs e)
        {
            
             DisplayRecord(currentLine - 1);  
        }
 
        private void btnLast_Click( object sender, EventArgs e)
        {
              DisplayRecord(lines.Count - 1);
        }

        private void btnAdd_Click( object sender, EventArgs e)
        {
              // toggle ReadOnly off
              ToggleReadOnly(false);
              // enable Save and Cancel buttons
              btnSave.Enabled = true;
              btnCancel.Enabled = true;
              // disable other buttons
              btnFirst.Enabled = false;
              btnNext.Enabled = false;
              btnPrev.Enabled = false;
              btnLast.Enabled = false;
              btnAdd.Enabled = false;
              btnEdit.Enabled = false;
              // set add mode
              mode = Mode.Add;
              // clear text boxes and set focus to first one
              txtCustomerID.Text = "";
              txtContactPerson.Text = "";
              txtAddress.Text = "";
              txtContactPerson.Text = "";
              txtContactNo.Text = "";
              txtCustomerID.Focus();
        }

        private void btnEdit_Click( object sender, EventArgs e)
        {
              // toggle ReadOnly off
              ToggleReadOnly(false);
              // enable Save and Cancel buttons
              btnSave.Enabled = true;
              btnCancel.Enabled = true;
              // disable other buttons
              btnFirst.Enabled = false;
              btnNext.Enabled = false;
              btnPrev.Enabled = false;
              btnLast.Enabled = false;
              btnAdd.Enabled = false;
              btnEdit.Enabled = false;
               // set edit mode
              mode = Mode.Edit;
              // set focus to first text box
              txtCustomerID.Focus();              
        }

        private void btnSave_Click( object sender, EventArgs e)
        {

              // toggle ReadOnly on
              ToggleReadOnly(true);
              // disable Save and Cancel buttons
              btnSave.Enabled = false;
              btnCancel.Enabled = false;
              // enable other buttons
              btnFirst.Enabled = true;
              btnNext.Enabled = true;
              btnPrev.Enabled = true;
              btnLast.Enabled = true;
              btnAdd.Enabled = true;
              btnEdit.Enabled = true;
              // set display mode
              mode = Mode.Display;

              string[] records = new string[]
              {
                 txtCustomerID.Text,
                 txtContactPerson.Text,
                 txtAddress.Text,
                 txtContactPerson.Text,
                 txtContactNo.Text
              };
              string line = String.Join("#", records);
              if (mode == Mode.Add)
              {             
                 lines.Add(line);
                 currentLine = lines.Count - 1; // update currentline 
              }
              else
              {
                 lines[currentLine] = line;
              } 
              File.WriteAllLines(@"C:\mp2.txt", lines.ToArray());                                     
        }

        private void btnCancel_Click( object sender, EventArgs e)
        {
              // toggle ReadOnly on
              ToggleReadOnly(true);
              // disable Save and Cancel buttons
              btnSave.Enabled = false;
              btnCancel.Enabled = false;
              // enable other buttons
              btnFirst.Enabled = true;
              btnNext.Enabled = true;
              btnPrev.Enabled = true;
              btnLast.Enabled = true;
              btnAdd.Enabled = true;
              btnEdit.Enabled = true;
               // set display mode
              mode = Mode.Display;
              // redisplay current record
              DisplayRecord(currentLine);
        }


Re: adding records StreamWriter PROBLEM
Posted on: 19 Dec 2007  

Sir...

 

I still have troubles using the btn_Add...

It should be like... when you click the btn_Add... a new form should appear. Or should I say, this form is not included in the current records of my .txt file... once saved, this is only the time that it will add the to the existing record.

 

let's say that your current lines in .txt file is 4... once you added and saved your new input, the existing .txt file will be updated. the .txt file would have 5 lines all in all..

do i have to create a temporary variable so that i could later put it in the .txt file? what appropriate codes shall i use... if there's any...

in addition, is there any way that i could delete the current displaying record from my program (btn_Delete) ? the challenge would be that the records will update and adjust the inputs to allocate the remove entry...

by the way sir... i would like to thank you for helping with this kind of activity... i'm starting to learn new concepts in C#...


chill~

 

Alan
  • 0
  • 0
Re: adding records StreamWriter PROBLEM
Posted on: 19 Dec 2007  

Well, the way I've coded this (or the way I intended to code it if it isn't working properly) is to use the textboxes on the existing form to add a record.

When btnAdd is pressed, the textboxes should all be cleared and the input focus should be set to the first textbox. When btnSave is pressed, the contents of the textboxes will be assembled into a line (using the # separator), the new line will be added to the end of the 'lines' generic list and then the whole lot will be written to the disk file, overwriting the file that's already there. 

It's possible to append to the file rather than overwriting it but, as long as there are not too many records, I find that you have less problems with linefeeds etc. if you just overwrite it every time.

If the user presses btnCancel whilst adding the new record, then nothing is added to the 'lines' list and the record that was previously visible is redisplayed.

If you introduce a second form to add records, then that introduces all sorts of problems of communicating data between the forms and so I wouldn't recommend that when the (relatively easy) way I've just described should work fine.

It's possible to delete the current record though I've had to revise some of the existing code again to allow for the addition of  btnDelete. Here's the revised version:

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

     
// add enum definition within form
private enum Mode{ Display, Add, Edit};

// add private fields
private List<string> lines = new List<string>();
private int currentLine = -1; // dummy value
private Mode mode = Mode.Display; 

        private void frmCustomerDetails_Load(object sender, EventArgs e)
        {

            // change TextBoxes to readonly so they can't be edited for now
            ToggleReadOnly(true);
            // disable Save and Cancel buttons
            btnSave.Enabled = false;
            btnCancel.Enabled = false;
            //Reads records from a file
            lines.AddRange(File.ReadAllLines(@"C:\mp2.txt"));
            // display first one
            DisplayRecord(0);
        }

        private void DisplayRecord(int index)
        {
            // don't do anything if index is outside bounds of lines list
            // or if requested line is already displayed
            if (index < 0 || index >= lines.Count || index == currentLine)
               return;
            string[] records = lines[index].Split('#');
            txtCustomerID.Text = records[0];
            txtContactPerson.Text = records[1];
            txtAddress.Text = records[2];
            txtContactPerson.Text = records[3];
            txtContactNo.Text = records[4];
            currentLine = index; // update currentline 
        }

        private void ToggleReadOnly(bool value)
        {
            txtCustomerID.ReadOnly = value;
            txtContactPerson.ReadOnly = value;
            txtAddress.ReadOnly = value;
            txtContactPerson.ReadOnly = value;
            txtContactNo.ReadOnly = value;
        }

        private void btnFirst_Click( object sender, EventArgs e)
        {
             DisplayRecord(0);
        }

        private void btnNext_Click( object sender, EventArgs e)
        {
             DisplayRecord(currentLine + 1);  
        }

        private void btnPrev_Click( object sender, EventArgs e)
        {
            
             DisplayRecord(currentLine - 1);  
        }
 
        private void btnLast_Click( object sender, EventArgs e)
        {
              DisplayRecord(lines.Count - 1);
        }

        private void btnAdd_Click( object sender, EventArgs e)
        {
              // toggle ReadOnly off
              ToggleReadOnly(false);
              // enable Save and Cancel buttons
              btnSave.Enabled = true;
              btnCancel.Enabled = true;
              // disable other buttons
              btnFirst.Enabled = false;
              btnNext.Enabled = false;
              btnPrev.Enabled = false;
              btnLast.Enabled = false;
              btnAdd.Enabled = false;
              btnEdit.Enabled = false;
              btnDelete.Enabled = false;
              // set add mode
              mode = Mode.Add;
              // clear text boxes and set focus to first one
              txtCustomerID.Text = "";
              txtContactPerson.Text = "";
              txtAddress.Text = "";
              txtContactPerson.Text = "";
              txtContactNo.Text = "";
              txtCustomerID.Focus();
        }

        private void btnEdit_Click( object sender, EventArgs e)
        {
              // toggle ReadOnly off
              ToggleReadOnly(false);
              // enable Save and Cancel buttons
              btnSave.Enabled = true;
              btnCancel.Enabled = true;
              // disable other buttons
              btnFirst.Enabled = false;
              btnNext.Enabled = false;
              btnPrev.Enabled = false;
              btnLast.Enabled = false;
              btnAdd.Enabled = false;
              btnEdit.Enabled = false;
              btnDelete.Enabled = false;
               // set edit mode
              mode = Mode.Edit;
              // set focus to first text box
              txtCustomerID.Focus();              
        }

        private void btnSave_Click( object sender, EventArgs e)
        {

              // toggle ReadOnly on
              ToggleReadOnly(true);
              // disable Save and Cancel buttons
              btnSave.Enabled = false;
              btnCancel.Enabled = false;
              // enable other buttons
              btnFirst.Enabled = true;
              btnNext.Enabled = true;
              btnPrev.Enabled = true;
              btnLast.Enabled = true;
              btnAdd.Enabled = true;
              btnEdit.Enabled = true;
              btnDelete.Enabled = true;
              // set display mode
              mode = Mode.Display;

              string[] records = new string[]
              {
                 txtCustomerID.Text,
                 txtContactPerson.Text,
                 txtAddress.Text,
                 txtContactPerson.Text,
                 txtContactNo.Text
              };
              string line = String.Join("#", records);
              if (mode == Mode.Add)
              {             
                 lines.Add(line);
                 currentLine = lines.Count - 1; // update currentline 
              }
              else
              {
                 lines[currentLine] = line;
              } 
              File.WriteAllLines(@"C:\mp2.txt", lines.ToArray());                                     
        }

        private void btnCancel_Click( object sender, EventArgs e)
        {
              // toggle ReadOnly on
              ToggleReadOnly(true);
              // disable Save and Cancel buttons
              btnSave.Enabled = false;
              btnCancel.Enabled = false;
              // enable other buttons
              btnFirst.Enabled = true;
              btnNext.Enabled = true;
              btnPrev.Enabled = true;
              btnLast.Enabled = true;
              btnAdd.Enabled = true;
              btnEdit.Enabled = true;
              btnDelete.Enabled = true;
               // set display mode
              mode = Mode.Display;
              // redisplay current record
              DisplayRecord(currentLine);
        }

        private void btnDelete_Click( object sender, EventArgs e)
        {
              lines.RemoveAt(currentLine); // deletes current line
              if (currentLine > 0)
              {
                 currentLine--;
                 DisplayRecord(currentLine);
              }
              else if (lines.Count > 0)
              {
                 currentLine = 0;
                 DisplayRecord(0);
              }
              else // no lines left
              {
                 currentLine = -1;
                 txtCustomerID.Text = "";
                 txtContactPerson.Text = "";
                 txtAddress.Text = "";
                 txtContactPerson.Text = "";
                 txtContactNo.Text = "";
              } 
              File.WriteAllLines(@"C:\mp2.txt", lines.ToArray());
        }


Re: adding records StreamWriter PROBLEM
Posted on: 19 Dec 2007  

gee.. thanks a lot for the big help , sir allan...

by the way, you from US?


chill~

 

Alan
  • 0
  • 0
Re: adding records StreamWriter PROBLEM
Posted on: 19 Dec 2007  
No, I'm from the UK :)
Re: adding records StreamWriter PROBLEM
Posted on: 19 Dec 2007  

Sir Allan...

how come in my btnAdd is the as the function of btn_Edit? When I tried to input a new record, it edits the existing record?

is there a way where i can put my new record to the last line of records of the existing .txt file? I'm really confused at the moment... The record should also be updated once displayed by next, first, prev, last, etc. O.o

Sorry. I'm kinda in a rush....

 


chill~

 

Alan
  • 0
  • 0
Re: adding records StreamWriter PROBLEM
Posted on: 19 Dec 2007  

I can't immediately see how that's possible as the code currently stands :-/

However, as we 've got quite a lot of code now, I think I'll see if I can find time to build the whole project to try and iron out any problems.

I'll get back when I've done that though it may be tomorrow now.


Re: adding records StreamWriter PROBLEM
Posted on: 19 Dec 2007  
Okay. I understand. Hope seeing you again.. God bless

chill~

 

Alan
  • 0
  • 0
Re: adding records StreamWriter PROBLEM
Posted on: 19 Dec 2007  

OK, I've built it myself now and found a couple of bugs (including one in DisplayRecord) which were stopping it working properly. I've also tidied up the 'button enabling' code and ended up with the following which seems to be working fine:

        // add enum definition within form
        private enum Mode { Display, Add, Edit };

        // add private fields
        private List<string> lines = new List<string>();
        private int currentLine = -1; // dummy value
        private Mode mode = Mode.Display; 

        public frmCustomerDetails()
        {
            InitializeComponent();
        }

        private void frmCustomerDetails_Load(object sender, EventArgs e)
        {
            // change TextBoxes to readonly so they can't be edited for now
            ToggleReadOnly(true);
            // disable Save and Cancel buttons, enable rest
            ToggleEnabled(false);
            //Reads records from a file
            lines.AddRange(File.ReadAllLines(@"C:\mp2.txt"));
            // display first one
            DisplayRecord(0);
        }

        private void DisplayRecord(int index)
        {
            // don't do anything if index is outside bounds of lines list
            if (index < 0 || index >= lines.Count)
                return;
            string[] records = lines[index].Split('#');
            txtCustomerID.Text = records[0];
            txtCompanyName.Text = records[1];
            txtAddress.Text = records[2];
            txtContactPerson.Text = records[3];
            txtContactNo.Text = records[4];
            currentLine = index; // update currentline 
        }

        private void ToggleReadOnly(bool value)
        {
            txtCustomerID.ReadOnly = value;
            txtCompanyName.ReadOnly = value;
            txtAddress.ReadOnly = value;
            txtContactPerson.ReadOnly = value;
            txtContactNo.ReadOnly = value;
        }

        private void ToggleEnabled(bool value)
        {
            btnSave.Enabled = value;
            btnCancel.Enabled = value;
            btnFirst.Enabled = !value;
            btnNext.Enabled = !value;
            btnPrev.Enabled = !value;
            btnLast.Enabled = !value;
            btnAdd.Enabled = !value;
            btnEdit.Enabled = !value;
            btnDelete.Enabled = !value;
        }

        private void btnFirst_Click(object sender, EventArgs e)
        {
            DisplayRecord(0);
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            DisplayRecord(currentLine + 1);
        }

        private void btnPrev_Click(object sender, EventArgs e)
        {
            DisplayRecord(currentLine - 1); 
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            DisplayRecord(lines.Count - 1);
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            // toggle ReadOnly off
            ToggleReadOnly(false);
            // enable Save and Cancel buttons, disable rest
            ToggleEnabled(true);
            // set add mode
            mode = Mode.Add;
            // clear text boxes and set focus to first one
            txtCustomerID.Text = "";
            txtCompanyName.Text = "";
            txtAddress.Text = "";
            txtContactPerson.Text = "";
            txtContactNo.Text = "";
            txtCustomerID.Focus();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            // toggle ReadOnly off
            ToggleReadOnly(false);
            // enable Save and Cancel buttons, disable rest
            ToggleEnabled(true);
            // set edit mode
            mode = Mode.Edit;
            // set focus to first text box
            txtCustomerID.Focus();    
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            // toggle ReadOnly on
            ToggleReadOnly(true);
            // disable Save and Cancel buttons, enable rest
            ToggleEnabled(false);
            string[] records = new string[]
            {
                 txtCustomerID.Text,
                 txtCompanyName.Text,
                 txtAddress.Text,
                 txtContactPerson.Text,
                 txtContactNo.Text
            };
            string line = String.Join("#", records) + "#";
            if (mode == Mode.Add)
            {
                lines.Add(line);
                currentLine = lines.Count - 1; // update currentline 
            }
            else
            {
                lines[currentLine] = line;
            }
            // set display mode
            mode = Mode.Display;
            // write changed list to file
            File.WriteAllLines(@"C:\mp2.txt", lines.ToArray());
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            // toggle ReadOnly on
            ToggleReadOnly(true);
            // disable Save and Cancel buttons, enable rest
            ToggleEnabled(false);
            // set display mode
            mode = Mode.Display;
            // redisplay current record
            DisplayRecord(currentLine);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            lines.RemoveAt(currentLine); // deletes current line
            if (currentLine > 0)
            {
                currentLine--;
                DisplayRecord(currentLine);
            }
            else if (lines.Count > 0)
            {
                currentLine = 0;
                DisplayRecord(0);
            }
            else // no lines left
            {
                currentLine = -1;
                txtCustomerID.Text = "";
                txtCompanyName.Text = "";
                txtAddress.Text = "";
                txtContactPerson.Text = "";
                txtContactNo.Text = "";
            }
            File.WriteAllLines(@"C:\mp2.txt", lines.ToArray());
        }
 


SPONSORED BY

Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.