===============================
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 wfa0009
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text.Trim()=="" || textBox2.Text.Trim() == "" || textBox3.Text.Trim() == "" || textBox4.Text.Trim() == "")
{
MessageBox.Show("try input data again");
return;
}
//======== check id
StreamReader sridtest = new StreamReader("mydata2.txt");
string stch = sridtest.ReadToEnd();
sridtest.Close();
if (stch.Contains(textBox1.Text + ";"))
{
MessageBox.Show("this id is taken try another");
textBox1.Focus();
textBox1.SelectAll();
}
else
{
//=== write to file
StreamWriter sw2 = new StreamWriter("mydata2.txt", true, Encoding.Unicode); //==append
string ifstr = textBox1.Text + ";"
+ textBox2.Text + ";"
+ textBox3.Text + ";"
+ textBox4.Text;
sw2.WriteLine(ifstr);
sw2.Close();
//== chech directory
if (!Directory.Exists("img"))
Directory.CreateDirectory("img");
//=== save pic to folder img with name id of person
pictureBox1.Image.Save("img/" + textBox1.Text + ".jpg");
MessageBox.Show("Person is added");
// to empty form
cleartextbox();
}
}
private void Form1_Load(object sender, EventArgs e)
{
// this.Icon = Icon.ExtractAssociatedIcon("wfa0009.exe"); //not dynamic
this.Icon = Icon.ExtractAssociatedIcon(AppDomain.CurrentDomain.FriendlyName ); // dynamic
}
void cleartextbox()
{
pictureBox1.Image = new PictureBox().Image; //clear pic
foreach (Control cont in this.Controls)
{
if (cont is TextBox)
{
cont.Text = "";
}
}
textBox1.Focus();
}
private void button4_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button2_Click(object sender, EventArgs e)
{
Form FR = new Form(); //create
FR.StartPosition = FormStartPosition.CenterParent;
FR.Font = this.Font;
FR.Icon = this.Icon;
FR.Size = this.Size;
FR.Text = "all records";
TextBox tb = new TextBox();
tb.Multiline = true;
tb.ScrollBars = ScrollBars.Both;
tb.Dock = DockStyle.Fill; // full screen
FR.Controls.Add(tb);
try
{
StreamReader sr = new StreamReader("mydata2.txt");
string rAlllines = sr.ReadToEnd();
sr.Close();
tb.Text = rAlllines;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
FR.ShowDialog();
}
private void button3_Click(object sender, EventArgs e)
{
if (textBox1.Text.Trim()!="")
{
StreamReader sr = new StreamReader("mydata2.txt");
string line = "";
bool found = false;
do
{
line = sr.ReadLine();
if (line != null)
{
string[] lwords = line.Split(';');
if (lwords[0]==textBox1.Text.Trim())
{
textBox2.Text = lwords[1];
textBox3.Text = lwords[2];
textBox4.Text = lwords[3];
string picpath = "img/" + lwords[0] + ".jpg";
if (File.Exists(picpath))
{
pictureBox1.Image = Image.FromFile(picpath);
}
if (File.Exists(picpath))
{
MessageBox.Show("found data and pic");
}
else
{
pictureBox1.Image = new PictureBox().Image; //clear pic
MessageBox.Show("found data only without pic");
}
found = true;
break;
}
}
} while (line!=null);
sr.Close();
if (!found)
{
MessageBox.Show("not found");
}
}
else
{
MessageBox.Show("plz enter Id");
textBox1.Focus();
}
}
private void button5_Click(object sender, EventArgs e)
{
// to empty form
cleartextbox();
}
private void button6_Click(object sender, EventArgs e)
{
OpenFileDialog opic = new OpenFileDialog();
opic.InitialDirectory=Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
opic.Filter = "Images|*.jpg;*.png;*.gif;*.bmp";
if (opic.ShowDialog()==DialogResult.OK)
{
pictureBox1.Image = Image.FromFile(opic.FileName);
}
}
}
}