c# winforms

anoi 21 Reputation points
2022-10-29T20:54:05.133+00:00

So for a good 2 weeks I've been trying to make this one function.

It works up to a function that I have to add.

I made a program which takes text from 2 textboxes and a combobox and saves it as a TXT file in the folder that has to be selected when the program starts. It has to work without a database, which is why it involves so many difficulties.

The data is entered by the user in the "Rechnungsssnummertxt.Text". databsankcombobox.Text the selected data is taken. In the case of the trackingcodetxt.Text, a number is randomly generated and the length is taken from NumericUpDown.

Now the program must save all data, both the preselected path and all data created in the program with LOGS.TXT. So not only with the selected path, but also with the LOGS.TXT file.

Before my program saves in the selected path, it has to check whether this data (here either the trackingcodetxt.Text or the invoice number txt.Text) has already been saved, if not the TXT should be saved in the preselected path. If so, the program should open a message box "The data has already been generated".

Here my code:

 private void generierenbtn_Click(object sender, EventArgs e)  
         {  
      
 //so here check the Logs.txt file before the the step, if also entery has in the Logs.txt file then do nothing give error code, if not make the step under.  
                  
             int stringLength = (int)laengeupdown.Value;  
             trackingcodetxt.Text = "B2B" + GetRandomString(stringLength);  
      
             string folderPath = Path.Combine(this.pfadtxt.Text, this.trackingcodetxt.Text + ".txt");  
             using (StreamWriter sw = new StreamWriter(folderPath))  
             {  
                 sw.WriteLine(";" + this.trackingcodetxt.Text + ";;;;;;;;;;;;;;;;;;;;;" + this.rechnungsnummertxt.Text + ";" + this.datenbankcombobox.Text);  
      
                 MessageBox.Show("Erfolgreich", "Auto-Save | V1.0.2", MessageBoxButtons.OK, MessageBoxIcon.Information);  
                 clearbtn.Enabled = true;            
                 rechnungsnummertxt.Text = "";  
                 trackingcodetxt.Text = "";  
                 infolabel.Text = "Erfolgreich: " + rechnungsnummertxt.Text;  
                 infolabel.ForeColor = Color.DarkGreen;  
      
             }  
      
         }            
         private string GetRandomString(int length)  
         {  
             string letters = "qwertzuioplkjhgfdsayxcvbnmQWERTZUIOPLKJHGFDSAYXCVBNBM1234567890";  
             Random random = new Random((int)DateTime.Now.Ticks);  
      
             string randomString = "";  
             for (int i = 0; i < length; i++)  
             {  
                 randomString += letters[random.Next(0, letters.Length - 1)];  
      
             }  
             return randomString;  
      
         }  
      
         private void pfadbtn_Click(object sender, EventArgs e)  
         {  
             if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)  
             {  
                 pfadtxt.Text = folderBrowserDialog1.SelectedPath;  
                 Properties.Settings.Default.datenbanks = datenbankcombobox.Text;  
                 Properties.Settings.Default.savefolder = pfadtxt.Text;  
                 Properties.Settings.Default.Save();  
                 MessageBox.Show("Pfad automatisch gespeichert zum ändern bitte Programm reseten oder nochmal auswählen!.", "Auto-Save | V1.0.2", MessageBoxButtons.OK, MessageBoxIcon.Information);  
                 rechnungsnummertxt.Enabled = true;  
                 generierenbtn.Enabled = true;  
                 this.Size = new Size(420, 210);  
      
             }  
             else  
             {  
                 MessageBox.Show("Ohne Pfad kann keine Datei erstellt u. gespeichert werden!", "PFAD AUSWÄHLEN!", MessageBoxButtons.OK, MessageBoxIcon.Error);  
             }  
         }  

Please could someone please help me with this?

EDIT: since so many weeks im trying to solve it. Here what i want to do to before do the steps (look->

private void generierenbtn_Click(object sender, EventArgs e) { //so here check the Logs.txt file before the the step, if also entery has in the Logs.txt file then do nothing give error code, if not make the step under. int stringLength = (int)laengeupdown.Value; trackingcodetxt.Text = "B2B" + GetRandomString(stringLength); ...)

Now the program must save all data, both the preselected path and all data created in the program with LOGS.TXT. So not only with the selected path, but also with the LOGS.TXT file.

Before my program saves in the selected path, it has to check whether this data (here either the trackingcodetxt.Text or the invoice number txt.Text) has already been saved, if not the TXT should be saved in the preselected path. If so, the program should open a message box "The data has already been generated".

Thank you guys for help!

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,511 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,635 questions
{count} votes

Accepted answer
  1. Karen Payne MVP 35,286 Reputation points
    2022-10-29T23:16:57.32+00:00

    First off, a Les indicated, do not repost a question.

    My advice is to use a .json file coupled with classes/models to use to read, add, edit, delete and save to and from.

    I put together a simple sample project found here. If you are fairly new to C# than you will need to spend time with the code and since it's not exactly tailored to your code it will need to be modified which is not possible without spending time with it. A bonus, I provide a nice method to increment alpha-numeric strings.

    Sample data

    [  
      {  
        "TrackingCode": "A001",  
        "CompanyName": "Apple"  
      },  
      {  
        "TrackingCode": "A002",  
        "CompanyName": "Microsoft"  
      },  
      {  
        "TrackingCode": "A003",  
        "CompanyName": "Amazon"  
      }  
    ]  
    

    255384-screen1.png

    0 comments No comments

0 additional answers

Sort by: Most helpful