so far here are my codes... thanks for the help
however, there are still glitches in the process
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 nextLine = 0;
private void frmCustomerDetails_Load(object sender, EventArgs e)
{
//Reads records from a file
lines = File.ReadAllLines(@"C:\mp2.txt");
DisplayRecord(0);
}
private void DisplayRecord(int index)
{
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];
if (index < lines.Count - 1) nextLine = index + 1;
// lines.Count didn't worked (System.Array doesn't not contain a definition
// for 'Count'
}
private void btnFirst_Click( object sender, EventArgs e)
{
DisplayRecord(0);
}
private void btnNext_Click( object sender, EventArgs e)
{
DisplayRecord(nextLine);
}
I also added two more features, but I don't where to start:
// btnPrev - Displays records before the 2nd, 3rd, 4th......
private void btnPrev_Click( object sender, EventArgs e)
{
// What to write?
}
// btnLast - Displays the last record in the .txt file
private void btnLast_Click( object sender, EventArgs e)
{
// What to write?
}
AlanPosted Dec 13, 2007, 5:35 AM
Sorry, that should have been lines.Length rather than lines.Count.
However, if you're going to have 'previous' and 'last' buttons as well, I think it would be better to change the code so that it persists the 'current' rather than the 'next' record index to be displayed. Like the following, again untested:
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);
}