Creating A Windows Form With Add, Edit and Delete Record Capability in C#

INTRODUCTION

Adding information to a data file of records is one of the most basic and needed functions in any organization. Not only does it keep everything in one central location, but it should also make it easy for someone to retrieve and change needed information quickly and easily. Here, I will describe C# coding I have created that demonstrates different features I use to manage a simple customer data file in binary text format.

HOW IT WORKS

The programming code I use works with a binary data file I call “customer.txt". The “currdir” variable I use contains the path of the current directory. It is concatenated to the beginning of the customer file to form the string expression, "currdir + customer.txt" . The defined constant, “CUSTOMERLEN”, is the length in characters of each customer record in the binary data file. To view the complete code readout, please click here.

After clicking the “Save” button in the screen, the fields (4 in this example) will first be retrieved under the function, AddCustomerButton_Click(object sender, System.EventArgs e). The data will be written to the customer data file in one of two ways. When the selection flag variable “SelectVar” equals 0, this means no customer data record has yet been retrieved and the information will be appended to the customer data file. If “SelectVar” equals 1, then a customer record has been retrieved and the currently displayed record will be updated with the changes made by the user to the customer data file. Then the list box that displays all of the customer records in the data file will be refreshed.

Next, I will discuss customer record retrieval into the Winform. Each customer record in the binary file has been automatically assigned a unique number denoting its physical sequence in the data file. This is done during adding and deleting customer records. Whenever the customer data file is restructured, this sequential record numbering system will also be updated.

Upon retrieving the desired customer record into the string variable I call CustomerListBox_String”, the program will next calculate its file position offset by using the record length constant of “CUSTOMERLEN”. This constant is then multiplied by the selected customer record’s physical sequence number to yield its file position offset, choose_record_datafile_offset”. Now, this will be used to read the selected customer record from a file stream to "currdir + customer.txt". The record from the file is read into the same string variable from above, “CustomerListBox_String”. This is then selectively parsed and read into its counterpart text fields on the Winform. Below you see the code I used for this operation via the function, CustomerListBox_SelectedIndexChanged(object sender, System.EventArgs e).


  1. private void CustomerListBox_SelectedIndexChanged(object sender, System.EventArgs e)  
  2.   
  3. {  
  4.   
  5. // declare local variables.  
  6.   
  7. int[] convert_to_number = new int[5];  
  8.   
  9. string CustomerListBox_String;  
  10.   
  11. // if there is a selection made in the list box of customer records,  
  12.   
  13. // then proceed.  
  14.   
  15. if (CustomerListBox.SelectedIndex != -1)  
  16.   
  17. {  
  18.   
  19. // grab the selected customer row from the list box.  
  20.   
  21. CustomerListBox_String = CustomerListBox.Text;  
  22.   
  23. // calculate the file offset within the binary data file that marks the  
  24.   
  25. // customer record to be retrieved. the file offset will be called  
  26.   
  27. // "choose_record_datafile_offset".  
  28.   
  29. for (a = 0; a < 5; a++)  
  30.   
  31. {  
  32.   
  33. convert_to_number[a] = 0;  
  34.   
  35. if (CustomerListBox_String.Substring((CustomerListBox_String.Length - 5) + a, 1) == "0") convert_to_number[a] = 0;  
  36.   
  37. if (CustomerListBox_String.Substring((CustomerListBox_String.Length - 5) + a, 1) == "1") convert_to_number[a] = 1;  
  38.   
  39. if (CustomerListBox_String.Substring((CustomerListBox_String.Length - 5) + a, 1) == "2") convert_to_number[a] = 2;  
  40.   
  41. if (CustomerListBox_String.Substring((CustomerListBox_String.Length - 5) + a, 1) == "3") convert_to_number[a] = 3;  
  42.   
  43. if (CustomerListBox_String.Substring((CustomerListBox_String.Length - 5) + a, 1) == "4") convert_to_number[a] = 4;  
  44.   
  45. if (CustomerListBox_String.Substring((CustomerListBox_String.Length - 5) + a, 1) == "5") convert_to_number[a] = 5;  
  46.   
  47. if (CustomerListBox_String.Substring((CustomerListBox_String.Length - 5) + a, 1) == "6") convert_to_number[a] = 6;  
  48.   
  49. if (CustomerListBox_String.Substring((CustomerListBox_String.Length - 5) + a, 1) == "7") convert_to_number[a] = 7;  
  50.   
  51. if (CustomerListBox_String.Substring((CustomerListBox_String.Length - 5) + a, 1) == "8") convert_to_number[a] = 8;  
  52.   
  53. if (CustomerListBox_String.Substring((CustomerListBox_String.Length - 5) + a, 1) == "9") convert_to_number[a] = 9;  
  54.   
  55. }  
  56.   
  57. choose_record_datafile_offset = ((convert_to_number[0] * 10000) + (convert_to_number[1] * 1000) + (convert_to_number[2] * 100) + (convert_to_number[3] * 10) + (convert_to_number[4] * 1)) * CUSTOMERLEN;  
  58.   
  59. // if the header of the list box has been selected, then throw  
  60.   
  61. // up the error message and abort the procedure.  
  62.   
  63. if (CustomerListBox_String.Substring(0, 4) == "Cust") MessageBox.Show("Invalid selection...""Error Mode");  
  64.   
  65. // if a customer record has been selected, then proceed.  
  66.   
  67. if (CustomerListBox_String.Substring(0, 4) != "Cust")  
  68.   
  69. {  
  70.   
  71. // a customer record from the list box has been selected.  
  72.   
  73. SelectVar = 1;  
  74.   
  75. // open a file stream to the calculated file stream position  
  76.   
  77. // marker, "choose_record_datafile_offset", and read the line  
  78.   
  79. // from the data file into the string variable,  
  80.   
  81. // “CustomerListBox_String". then close the file stream.  
  82.   
  83. StreamReader streamobj = new StreamReader(currdir + "customer.txt");  
  84.   
  85. streamobj.BaseStream.Seek(choose_record_datafile_offset, SeekOrigin.Begin);  
  86.   
  87. CustomerListBox_String = streamobj.ReadLine();  
  88.   
  89. streamobj.Close();  
  90.   
  91. // now use the Substring function on the string variable,  
  92.   
  93. // "CustomerListBox_String", to assign the retrieved information  
  94.   
  95. // from the binary data file to their counterpart text boxes in  
  96.   
  97. // the Winform.  
  98.   
  99. this.textBox1.Text = CustomerListBox_String.Substring(0, 35);  
  100.   
  101. this.textBox2.Text = CustomerListBox_String.Substring(35, 12);  
  102.   
  103. this.textBox3.Text = CustomerListBox_String.Substring(47, 8);  
  104.   
  105. this.textBox4.Text = CustomerListBox_String.Substring(55, 35);  
  106.   
  107. Cursor.Current = Cursors.Default;  
  108.   
  109. }  
  110.   

Removing unwanted records is a necessary function of any Winform. When the user clicks the “Delete” button, the Winform function DeleteCustomerButton_Click(object sender, System.EventArgs e) is fired. This actually works in much the same way as the CustomerListBox_SelectedIndexChanged(object sender, System.EventArgs e) function discussed above. After the remove_record_datafile_offset file offset has been calculated, the program code simply rewrites the "currdir + customer.txt" file without the customer record that had been selected from the list box for removal. Then the list box holding all the customer records will be refreshed using the same coding as with the add/update feature mentioned above.

CONCLUSION

Differing variations of the programming code I have discussed here have been used in a number of my application development projects and have proven themselves to be very reliable. The last thing you want is to have your time wasted when you are filling customer orders and trying to get things shipped out. I prefer to work with binary text files as a software development professional, because I can repair them easily and quickly in the event of a problem.

Up Next
    Ebook Download
    View all
    Learn
    View all