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.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- namespace Notes
- {
- public partial class Notes : Form
- {
- /// <summary>
- /// Store the login property.
- /// </summary>
- private string login = "Jacek";
- /// <summary>
- /// Store the password property.
- /// </summary>
- private string password = "abc123";
- private Panel panel_l;
- private Panel panel_n;
- private TextBox textBox_l;
- private TextBox textBox_p;
- private TextBox textBox_n;
- private Button button_l;
- private Button button_n;
- private Button button_e;
- /// <summary>
- /// Checks for a correct login and password.
- /// </summary>
- /// <param name="o"></param>
- /// <param name="e"></param>
- private void check(Object o, EventArgs e)
- {
- string temp_l = textBox_l.Text;
- if (temp_l == login)
- {
- string temp_p = textBox_p.Text;
- if (temp_p == password) createNote();
- else MessageBox.Show("Invalid password");
- }
- else MessageBox.Show("Invalid username");
- }
- /// <summary>
- /// Key handlig for more comfortable usage of app.
- /// To move from Login to Password field just click Enter.
- /// To log in, press Enter while focusing Password field.
- /// </summary>
- /// <param name="o"></param>
- /// <param name="e">Key that is pressed</param>
- private void keyPressed(object o, KeyPressEventArgs e)
- {
- if (e.KeyChar == 13)
- {
- if (textBox_l.Focused) { textBox_p.Focus(); textBox_p.Clear();}
- else if (textBox_p.Focused) check(e, EventArgs.Empty);
- }
- }
- /// <summary>
- /// Enable Ctrl+A for Select All in Note field.
- /// </summary>
- /// <param name="o"></param>
- /// <param name="e">Key that are down.</param>
- private void keyDown(object o, KeyEventArgs e)
- {
- if (e.Control && e.KeyCode == Keys.A)
- {
- textBox_n.SelectAll();
- }
- }
- /// <summary>
- /// Save a Note to a new file or adds to an exisitng one.
- /// </summary>
- /// <param name="o"></param>
- /// <param name="e"></param>
- private void saveFile(object o, EventArgs e)
- {
- string[] save_n = {login + " " + DateTime.Now, textBox_n.Text};
- try
- {
- using (StreamWriter add_n = new StreamWriter(@"Note.txt", true))
- {
- add_n.WriteLine(save_n[0]);
- add_n.WriteLine(save_n[1]);
- }
- }
- catch (IOException ex)
- {
- File.WriteAllLines(@"Note.txt", save_n);
- }
- }
- /// <summary>
- /// Dispose a watermark over Login field.
- /// </summary>
- /// <param name="o"></param>
- /// <param name="e"></param>
- private void enterTextBoxHandler_l(object o, EventArgs e)
- {
- if (textBox_l.Text == "Login")
- {
- textBox_l.Text = "";
- textBox_l.ForeColor = System.Drawing.Color.Black;
- }
- }
- /// <summary>
- /// Show watermark over Login field.
- /// </summary>
- /// <param name="o"></param>
- /// <param name="e"></param>
- private void leaveTextBoxHandler_l(object o, EventArgs e)
- {
- if (textBox_l.TextLength == 0)
- {
- textBox_l.Text = "Login";
- textBox_l.ForeColor = System.Drawing.Color.Gray;
- }
- }
- /// <summary>
- /// Dispose watermark over Password field hides it.
- /// </summary>
- /// <param name="o"></param>
- /// <param name="e"></param>
- private void enterTextBoxHandler_p(object o, EventArgs e)
- {
- if (textBox_p.Text == "Password")
- {
- textBox_p.Text = "";
- textBox_p.ForeColor = System.Drawing.Color.Black;
- textBox_p.PasswordChar = '*';
- }
- }
- /// <summary>
- /// Show watermark over Password field and reveals it.
- /// </summary>
- /// <param name="o"></param>
- /// <param name="e"></param>
- private void leaveTextBoxHandler_p(object o, EventArgs e)
- {
- if (textBox_p.TextLength == 0)
- {
- textBox_p.Text = "Password";
- textBox_p.ForeColor = System.Drawing.Color.Gray;
- textBox_p.PasswordChar = '\0';
- }
- }
- /// <summary>
- /// Exit the program.
- /// </summary>
- /// <param name="o"></param>
- /// <param name="e"></param>
- private void exit(object o, EventArgs e)
- {
- Application.Exit();
- }
- /// <summary>
- /// Creates a Logging panel.
- /// </summary>
- private void createLogin()
- {
- panel_l = new Panel();
- textBox_l = new TextBox();
- textBox_p = new TextBox();
- button_l = new Button();
- button_e = new Button();
- panel_l.Controls.Add(textBox_l);
- panel_l.Controls.Add(textBox_p);
- panel_l.Controls.Add(button_l);
- panel_l.Controls.Add(button_e);
- panel_l.Location = new Point(50, 20);
- textBox_l.Width = 200;
- textBox_l.Text = "";
- textBox_l.TextAlign = HorizontalAlignment.Center;
- textBox_l.KeyPress += keyPressed;
- textBox_l.Enter += enterTextBoxHandler_l;
- textBox_l.Leave += leaveTextBoxHandler_l;
- textBox_p.Width = 200;
- textBox_p.Text = "Password";
- textBox_p.ForeColor = System.Drawing.Color.Gray;
- textBox_p.TextAlign = HorizontalAlignment.Center;
- textBox_p.Location = new Point(0, 30);
- textBox_p.KeyPress += keyPressed;
- textBox_p.Enter += enterTextBoxHandler_p;
- textBox_p.Leave += leaveTextBoxHandler_p;
- button_l.Width = 95;
- button_l.Text = "Log in";
- button_l.Location = new Point(0, 60);
- button_l.Click += check;
- button_e.Width = 95;
- button_e.Text = "Exit";
- button_e.Location = new Point(105, 60);
- button_e.Click += exit;
- Controls.Add(panel_l);
- }
- /// <summary>
- /// Creates a Note panel.
- /// </summary>
- private void createNote()
- {
- Controls.Remove(panel_l);
- panel_n = new Panel();
- textBox_n = new TextBox();
- button_n = new Button();
- button_e = new Button();
- panel_n.Controls.Add(textBox_n);
- panel_n.Controls.Add(button_n);
- panel_n.Controls.Add(button_e);
- panel_n.Location = new Point(50, 20);
- textBox_n.Size = new Size(200, 50);
- textBox_n.Multiline = true;
- textBox_n.KeyDown += keyDown;
- button_n.Width = 95;
- button_n.Text = "Save me!";
- button_n.Location = new Point(0, 60);
- button_n.Click += saveFile;
- button_e.Width = 95;
- button_e.Text = "Exit";
- button_e.Location = new Point(105, 60);
- button_e.Click += exit;
- Controls.Add(panel_n);
- textBox_n.Focus();
- }
- public Notes()
- {
- Text = "Note";
- Size = new Size(300, 170);
- FormBorderStyle = FormBorderStyle.FixedSingle;
- StartPosition = FormStartPosition.CenterScreen;
- MaximizeBox = false;
- createLogin();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment