Advertisement
nuggetin

afsdasdasdasd

Jan 15th, 2022
719
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.13 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11.  
  12. namespace Registration_Form
  13. {
  14.     public partial class formregistration : Form
  15.     {
  16.         string filename;
  17.         string foldername;
  18.         bool directoryset, filenameset;
  19.         FolderBrowserDialog folderBrowserDialog1;
  20.         public formregistration()
  21.         {
  22.             InitializeComponent();
  23.         }
  24.         void msgBoxPerformClick(Button bt, String msg)
  25.         {
  26.             DialogResult result = MessageBox.Show(msg, "Alert", MessageBoxButtons.YesNo);
  27.             if (result == DialogResult.Yes)
  28.             {
  29.                 bt.PerformClick();
  30.             }
  31.             else
  32.             {
  33.                 throw new Exception();
  34.             }
  35.         }
  36.  
  37.         private void btregister_Click(object sender, EventArgs e)
  38.         {
  39.             try
  40.             {
  41.                 if (!directoryset)
  42.                 {
  43.                     msgBoxPerformClick(btsetdir, "Directory missng, Do you want to set it now?");
  44.  
  45.                 }
  46.                 else if (!filenameset)
  47.                 {
  48.                     msgBoxPerformClick(btautoname, "File Name missing, Do you want to generate file name?");
  49.                 }
  50.                 else
  51.                 {
  52.  
  53.                     foreach (Control c in this.Controls) //check if theres an empty field
  54.                     {
  55.                         if (c is TextBox)
  56.                         {
  57.                             TextBox textBox = c as TextBox;
  58.                             if (textBox.Text == string.Empty)
  59.                             {
  60.                                 MessageBox.Show("There's an empty field.");
  61.                                 throw new Exception();
  62.                             }
  63.                         }
  64.                     }
  65.  
  66.                     foldername = folderBrowserDialog1.SelectedPath;
  67.                     foldername = Path.Combine(foldername, filename);
  68.                     if (File.Exists(foldername)) //replaces the file that has the same file name
  69.                     {
  70.                         DialogResult result = MessageBox.Show("File Name already exist, Replace the existing File?", "Alert", MessageBoxButtons.YesNo);
  71.                         if (result == DialogResult.Yes)
  72.                         {
  73.                             File.Delete(foldername);
  74.                         }
  75.                         else
  76.                         {
  77.                             throw new Exception();
  78.                         }
  79.                     }
  80.                     using (FileStream fs = File.Create(foldername))
  81.                     {
  82.                         Byte[] title = new UTF8Encoding(true).GetBytes("Student No: " + tbstudentno.Text
  83.                             + "\nFull Name: " + tbln.Text + ", " + tbfn.Text + " " + tbmi.Text +
  84.                             "\nProgram: " + cbprogram.SelectedItem + "\nSex: " + cbgender.SelectedItem + "\nAge: "
  85.                             + tbage.Text + "\nBirthday: " + dtpbday.Text + "\nContact No.: " + tbcontactno.Text);
  86.                         fs.Write(title, 0, title.Length);
  87.                     }
  88.                     MessageBox.Show(filename + " has been written in " + foldername);
  89.                 }
  90.             }
  91.             catch (Exception a)
  92.             {
  93.             }
  94.         }
  95.  
  96.  
  97.         private void button1_Click(object sender, EventArgs e)
  98.         {
  99.             foldername = "";
  100.                 folderBrowserDialog1 = new FolderBrowserDialog();
  101.                 DialogResult result = folderBrowserDialog1.ShowDialog();
  102.                 if (result == DialogResult.OK)
  103.                 {
  104.                     foldername = folderBrowserDialog1.SelectedPath;
  105.                     directoryset = true;
  106.                 }
  107.         }
  108.  
  109.         private void button2_Click(object sender, EventArgs e)
  110.         {
  111.             filename = "";
  112.             var textBoxCollection = new[] { tbfn, tbln, tbstudentno };
  113.             bool checkEmpty = textBoxCollection.Any(t => String.IsNullOrWhiteSpace(t.Text));
  114.  
  115.             if (!checkEmpty)
  116.             {
  117.                 if (tbfilename.Text.Length == 0)
  118.                 {
  119.                     filename = tbstudentno.Text + "_" + tbln.Text + "_" + tbfn.Text;
  120.                     tbfilename.Text = filename;
  121.                     filename = filename.Insert(filename.Length, ".txt");
  122.  
  123.                 }
  124.                 else
  125.                 {
  126.                     filename = tbfilename.Text;
  127.                     filename = filename.Insert(filename.Length, ".txt");
  128.                     MessageBox.Show("File Name has been set.", "Alert");
  129.                 }
  130.             }
  131.             else
  132.             {
  133.                 MessageBox.Show("One of the 3 TextBox are empty, First & Last Name and Student Number.", "Error");
  134.             }
  135.             filenameset = true;
  136.         }
  137.         private void tbstudentno_TextChanged(object sender, EventArgs e)
  138.         {
  139.             tbfilename.Text = "";
  140.         }
  141.     }
  142. }
  143.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement