IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
C#
Search C# Group:

Advanced
-Collapse +Expand C# To/From
To/FromCODEGuides
-Collapse +Expand C# Study Test
PRESTWOODCERTIFIED
-Collapse +Expand C# Store
PRESTWOODSTORE

Prestwood eMagazine

December Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingC#   Print This     
 
Creating a Windows Form with Add, Edit and Delete Record Capability in C#
 
Posted 13 months ago on 11/13/2020
Take Away:

This article describes how I designed a C# Windows form with add, edit and delete capability.

KB104923

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# software 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 programming 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. I think this is the best method for managing this function.

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. privatevoid CustomerListBox_SelectedIndexChanged(object sender, System.EventArgs e)  
  2. {  
  3. // declare local variables.  
  4. int[] convert_to_number = new int[5];  
  5. string CustomerListBox_String;  
  6. // if there is a selection made in the list box of customer records,  
  7. // then proceed.  
  8. if(CustomerListBox.SelectedIndex != -1)  
  9. {  
  10. // grab the selected customer row from the list box.
  11. CustomerListBox_String = CustomerListBox.Text;  
  12. // calculate the file offset within the binary data file that
  13. // marks the customer record to be retrieved. the file offset
  14. // will be called "choose_record_datafile_offset".  
  15. for(a = 0; a < 5; a++)  
  16. {  
  17. convert_to_number[a] = 0;  
  18. if(CustomerListBox_String.Substring((CustomerListBox_String.Length - 5) + a, 1) == "0") convert_to_number[a] = 0;  
  19. if(CustomerListBox_String.Substring((CustomerListBox_String.Length - 5) + a, 1) == "1") convert_to_number[a] = 1;  
  20. if(CustomerListBox_String.Substring((CustomerListBox_String.Length - 5) + a, 1) == "2") convert_to_number[a] = 2;  
  21. if(CustomerListBox_String.Substring((CustomerListBox_String.Length - 5) + a, 1) == "3") convert_to_number[a] = 3;  
  22. if(CustomerListBox_String.Substring((CustomerListBox_String.Length - 5) + a, 1) == "4") convert_to_number[a] = 4;  
  23. if(CustomerListBox_String.Substring((CustomerListBox_String.Length - 5) + a, 1) == "5") convert_to_number[a] = 5;  
  24. if(CustomerListBox_String.Substring((CustomerListBox_String.Length - 5) + a, 1) == "6") convert_to_number[a] = 6;  
  25. if(CustomerListBox_String.Substring((CustomerListBox_String.Length - 5) + a, 1) == "7") convert_to_number[a] = 7;  
  26. if(CustomerListBox_String.Substring((CustomerListBox_String.Length - 5) + a, 1) == "8") convert_to_number[a] = 8;  
  27. if(CustomerListBox_String.Substring((CustomerListBox_String.Length - 5) + a, 1) == "9") convert_to_number[a] = 9;  
  28. }  
  29. 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;  
  30. // if the header of the list box has been selected, then throw
  31. // up the error message and abort the procedure.
  32. if(CustomerListBox_String.Substring(0, 4) == "Cust") MessageBox.Show("Invalid selection...", "Error Mode");  
  33. // if a customer record has been selected, then proceed.
  34. if(CustomerListBox_String.Substring(0, 4) != "Cust")  
  35. {  
  36. // a customer record from the list box has been selected.
  37. SelectVar = 1;  
  38. // open a file stream to the calculated file stream position
  39. // marker, "choose_record_datafile_offset", and read the line
  40. // from the data file into the string variable,
  41. // “CustomerListBox_String". then close the file stream.
  42. StreamReader streamobj = newStreamReader(currdir + "customer.txt");  
  43. BaseStream.Seek(choose_record_datafile_offset, SeekOrigin.Begin);  
  44. CustomerListBox_String = streamobj.ReadLine();  
  45. Close();  
  46. // now use the Substring function on the string variable, 
  47. // "CustomerListBox_String", to assign the retrieved information
  48. // from the binary data file to their counterpart text boxes in
  49. // the Winform.
  50. this.textBox1.Text = CustomerListBox_String.Substring(0, 35);  
  51. this.textBox2.Text = CustomerListBox_String.Substring(35, 12);  
  52. this.textBox3.Text = CustomerListBox_String.Substring(47, 8);  
  53. this.textBox4.Text = CustomerListBox_String.Substring(55, 35);  
  54. Current = Cursors.Default;  
  55. }  
  56. }  

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 application 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 software 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, because I can repair them easily and quickly in the event of a problem. And it does not require that much in the way of skills either.


Comments

0 Comments.
Share a thought or comment...
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P195A1
Enter key:
Article Contributed By NE Ohio Computer Guy:

Please visit my software developer website for more information about my services. I offer application development as well as Android app coding services. My developer skills are best suited to dealing with custom software projects. I can perform programming for Corel Paradox as well as C# and PHP.

In my local area of northeast Ohio, I can cater to computer repair and "fix my computer" issues. And don‘t forget to check out my YouTube Channel. It is full of cool videos and has something for everyone!

Use my contact web page today to reach me about any software design ideas you have.

Visit Profile

 KB Article #104923 Counter
311
Since 11/13/2020
Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
www.prestwood.com


©1995-2021 Prestwood IT Solutions.   [Security & Privacy]