Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace Registration_Form
- {
- public partial class formregistration : Form
- {
- string filename;
- string foldername;
- bool directoryset, filenameset;
- FolderBrowserDialog folderBrowserDialog1;
- public formregistration()
- {
- InitializeComponent();
- }
- void msgBoxPerformClick(Button bt, String msg)
- {
- DialogResult result = MessageBox.Show(msg, "Alert", MessageBoxButtons.YesNo);
- if (result == DialogResult.Yes)
- {
- bt.PerformClick();
- }
- else
- {
- throw new Exception();
- }
- }
- private void btregister_Click(object sender, EventArgs e)
- {
- try
- {
- if (!directoryset)
- {
- msgBoxPerformClick(btsetdir, "Directory missng, Do you want to set it now?");
- }
- else if (!filenameset)
- {
- msgBoxPerformClick(btautoname, "File Name missing, Do you want to generate file name?");
- }
- else
- {
- foreach (Control c in this.Controls) //check if theres an empty field
- {
- if (c is TextBox)
- {
- TextBox textBox = c as TextBox;
- if (textBox.Text == string.Empty)
- {
- MessageBox.Show("There's an empty field.");
- throw new Exception();
- }
- }
- }
- foldername = folderBrowserDialog1.SelectedPath;
- foldername = Path.Combine(foldername, filename);
- if (File.Exists(foldername)) //replaces the file that has the same file name
- {
- DialogResult result = MessageBox.Show("File Name already exist, Replace the existing File?", "Alert", MessageBoxButtons.YesNo);
- if (result == DialogResult.Yes)
- {
- File.Delete(foldername);
- }
- else
- {
- throw new Exception();
- }
- }
- using (FileStream fs = File.Create(foldername))
- {
- Byte[] title = new UTF8Encoding(true).GetBytes("Student No: " + tbstudentno.Text
- + "\nFull Name: " + tbln.Text + ", " + tbfn.Text + " " + tbmi.Text +
- "\nProgram: " + cbprogram.SelectedItem + "\nSex: " + cbgender.SelectedItem + "\nAge: "
- + tbage.Text + "\nBirthday: " + dtpbday.Text + "\nContact No.: " + tbcontactno.Text);
- fs.Write(title, 0, title.Length);
- }
- MessageBox.Show(filename + " has been written in " + foldername);
- }
- }
- catch (Exception a)
- {
- }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- foldername = "";
- folderBrowserDialog1 = new FolderBrowserDialog();
- DialogResult result = folderBrowserDialog1.ShowDialog();
- if (result == DialogResult.OK)
- {
- foldername = folderBrowserDialog1.SelectedPath;
- directoryset = true;
- }
- }
- private void button2_Click(object sender, EventArgs e)
- {
- filename = "";
- var textBoxCollection = new[] { tbfn, tbln, tbstudentno };
- bool checkEmpty = textBoxCollection.Any(t => String.IsNullOrWhiteSpace(t.Text));
- if (!checkEmpty)
- {
- if (tbfilename.Text.Length == 0)
- {
- filename = tbstudentno.Text + "_" + tbln.Text + "_" + tbfn.Text;
- tbfilename.Text = filename;
- filename = filename.Insert(filename.Length, ".txt");
- }
- else
- {
- filename = tbfilename.Text;
- filename = filename.Insert(filename.Length, ".txt");
- MessageBox.Show("File Name has been set.", "Alert");
- }
- }
- else
- {
- MessageBox.Show("One of the 3 TextBox are empty, First & Last Name and Student Number.", "Error");
- }
- filenameset = true;
- }
- private void tbstudentno_TextChanged(object sender, EventArgs e)
- {
- tbfilename.Text = "";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement