jotto

Form1.cs

Feb 13th, 2015
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.87 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.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12.  
  13. namespace Notes
  14. {
  15.     public partial class Notes : Form
  16.     {
  17.         /// <summary>
  18.         /// Store the login property.
  19.         /// </summary>
  20.         private string login    =   "Jacek";
  21.         /// <summary>
  22.         /// Store the password property.
  23.         /// </summary>
  24.         private string password =   "abc123";
  25.  
  26.         private Panel panel_l;
  27.         private Panel panel_n;
  28.  
  29.         private TextBox textBox_l;
  30.         private TextBox textBox_p;
  31.         private TextBox textBox_n;
  32.  
  33.         private Button button_l;
  34.         private Button button_n;
  35.         private Button button_e;
  36.  
  37.         /// <summary>
  38.         /// Checks for a correct login and password.
  39.         /// </summary>
  40.         /// <param name="o"></param>
  41.         /// <param name="e"></param>
  42.         private void check(Object o, EventArgs e)
  43.         {
  44.             string temp_l = textBox_l.Text;
  45.             if (temp_l == login)
  46.             {
  47.                 string temp_p = textBox_p.Text;
  48.                 if (temp_p == password) createNote();
  49.                 else MessageBox.Show("Invalid password");
  50.             }
  51.             else MessageBox.Show("Invalid username");
  52.         }
  53.  
  54.         /// <summary>
  55.         /// Key handlig for more comfortable usage of app.
  56.         /// To move from Login to Password field just click Enter.
  57.         /// To log in, press Enter while focusing Password field.
  58.         /// </summary>
  59.         /// <param name="o"></param>
  60.         /// <param name="e">Key that is pressed</param>
  61.         private void keyPressed(object o, KeyPressEventArgs e)
  62.         {
  63.             if (e.KeyChar == 13)
  64.             {
  65.                 if (textBox_l.Focused) { textBox_p.Focus(); textBox_p.Clear();}
  66.                 else if (textBox_p.Focused) check(e, EventArgs.Empty);
  67.             }
  68.         }
  69.        
  70.         /// <summary>
  71.         /// Enable Ctrl+A for Select All in Note field.
  72.         /// </summary>
  73.         /// <param name="o"></param>
  74.         /// <param name="e">Key that are down.</param>
  75.         private void keyDown(object o, KeyEventArgs e)
  76.         {
  77.             if (e.Control && e.KeyCode == Keys.A)
  78.             {
  79.                 textBox_n.SelectAll();
  80.             }
  81.         }
  82.  
  83.         /// <summary>
  84.         /// Save a Note to a new file or adds to an exisitng one.
  85.         /// </summary>
  86.         /// <param name="o"></param>
  87.         /// <param name="e"></param>
  88.         private void saveFile(object o, EventArgs e)
  89.         {            
  90.             string[] save_n = {login + " " + DateTime.Now, textBox_n.Text};
  91.             try
  92.             {
  93.                 using (StreamWriter add_n = new StreamWriter(@"Note.txt", true))
  94.                 {
  95.                     add_n.WriteLine(save_n[0]);
  96.                     add_n.WriteLine(save_n[1]);
  97.                 }
  98.             }
  99.             catch (IOException ex)
  100.             {
  101.                 File.WriteAllLines(@"Note.txt", save_n);
  102.             }
  103.         }
  104.  
  105.         /// <summary>
  106.         /// Dispose a watermark over Login field.
  107.         /// </summary>
  108.         /// <param name="o"></param>
  109.         /// <param name="e"></param>
  110.         private void enterTextBoxHandler_l(object o, EventArgs e)
  111.         {
  112.             if (textBox_l.Text == "Login")
  113.             {
  114.                 textBox_l.Text = "";
  115.                 textBox_l.ForeColor = System.Drawing.Color.Black;
  116.             }
  117.         }
  118.  
  119.         /// <summary>
  120.         /// Show watermark over Login field.
  121.         /// </summary>
  122.         /// <param name="o"></param>
  123.         /// <param name="e"></param>
  124.         private void leaveTextBoxHandler_l(object o, EventArgs e)
  125.         {
  126.             if (textBox_l.TextLength == 0)
  127.             {
  128.                 textBox_l.Text = "Login";
  129.                 textBox_l.ForeColor = System.Drawing.Color.Gray;
  130.             }
  131.         }
  132.  
  133.         /// <summary>
  134.         /// Dispose watermark over Password field hides it.
  135.         /// </summary>
  136.         /// <param name="o"></param>
  137.         /// <param name="e"></param>
  138.         private void enterTextBoxHandler_p(object o, EventArgs e)
  139.         {
  140.             if (textBox_p.Text == "Password")
  141.             {
  142.                 textBox_p.Text = "";
  143.                 textBox_p.ForeColor = System.Drawing.Color.Black;
  144.                 textBox_p.PasswordChar = '*';
  145.             }
  146.         }
  147.  
  148.         /// <summary>
  149.         /// Show watermark over Password field and reveals it.
  150.         /// </summary>
  151.         /// <param name="o"></param>
  152.         /// <param name="e"></param>
  153.         private void leaveTextBoxHandler_p(object o, EventArgs e)
  154.         {
  155.             if (textBox_p.TextLength == 0)
  156.             {
  157.                 textBox_p.Text = "Password";
  158.                 textBox_p.ForeColor = System.Drawing.Color.Gray;
  159.                 textBox_p.PasswordChar = '\0';
  160.             }
  161.         }
  162.  
  163.         private void exit(object o, EventArgs e)
  164.         {
  165.             Application.Exit();
  166.         }
  167.  
  168.         /// <summary>
  169.         /// Creates a Logging panel.
  170.         /// </summary>
  171.         private void createLogin()
  172.         {
  173.             panel_l = new Panel();
  174.             textBox_l = new TextBox();
  175.             textBox_p = new TextBox();
  176.             button_l = new Button();
  177.  
  178.             panel_l.Controls.Add(textBox_l);
  179.             panel_l.Controls.Add(textBox_p);
  180.             panel_l.Controls.Add(button_l);
  181.             panel_l.Location = new Point(50, 20);
  182.  
  183.             textBox_l.Width = 200;
  184.             textBox_l.Text = "";
  185.             textBox_l.TextAlign = HorizontalAlignment.Center;
  186.             textBox_l.KeyPress += keyPressed;
  187.             textBox_l.Enter += enterTextBoxHandler_l;
  188.             textBox_l.Leave += leaveTextBoxHandler_l;
  189.  
  190.             textBox_p.Width = 200;
  191.             textBox_p.Text = "Password";
  192.             textBox_p.ForeColor = System.Drawing.Color.Gray;
  193.             textBox_p.TextAlign = HorizontalAlignment.Center;
  194.             textBox_p.Location = new Point(0, 30);
  195.             textBox_p.KeyPress += keyPressed;
  196.             textBox_p.Enter += enterTextBoxHandler_p;
  197.             textBox_p.Leave += leaveTextBoxHandler_p;
  198.  
  199.             button_l.Width = 200;
  200.             button_l.Text = "Log in";
  201.             button_l.Location = new Point(0, 60);
  202.             button_l.Click += check;
  203.  
  204.             Controls.Add(panel_l);
  205.         }
  206.  
  207.         /// <summary>
  208.         /// Creates a Note panel.
  209.         /// </summary>
  210.         private void createNote()
  211.         {
  212.             Controls.Remove(panel_l);
  213.  
  214.             panel_n = new Panel();
  215.             textBox_n = new TextBox();
  216.             button_n = new Button();
  217.             button_e = new Button();
  218.  
  219.             panel_n.Controls.Add(textBox_n);
  220.             panel_n.Controls.Add(button_n);
  221.             panel_n.Controls.Add(button_e);
  222.             panel_n.Location = new Point(50, 20);
  223.  
  224.             textBox_n.Size = new Size(200, 50);
  225.             textBox_n.Multiline = true;
  226.             textBox_n.KeyDown += keyDown;
  227.  
  228.             button_n.Width = 95;
  229.             button_n.Text = "Save me!";
  230.             button_n.Location = new Point(0, 60);
  231.             button_n.Click += saveFile;
  232.  
  233.             button_e.Width = 95;
  234.             button_e.Text = "Exit";
  235.             button_e.Location = new Point(105, 60);
  236.             button_e.Click += exit;
  237.            
  238.             Controls.Add(panel_n);
  239.  
  240.             textBox_n.Focus();
  241.         }
  242.  
  243.         public Notes()
  244.         {
  245.             Text = "Note";
  246.             Size = new Size(300, 170);
  247.             FormBorderStyle = FormBorderStyle.FixedSingle;
  248.             StartPosition = FormStartPosition.CenterScreen;
  249.             MaximizeBox = false;
  250.             createLogin();
  251.             }
  252.     }
  253. }
Advertisement
Add Comment
Please, Sign In to add comment