using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Text.RegularExpressions;
namespace ReflectionExample
{
public partial class Reflection : Form
{
private bool boolEmailValidation = false;
private bool boolPercentValidation;
public Reflection()
{
InitializeComponent();
}
Settings settings = new Settings();
private void Form1_Load(object sender, EventArgs e)
{
}
private void RetrieveButton_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
settings.Load(openFileDialog1.FileName);
UpdateSettings();
}
}
private void UpdateSettings()
{
textStudentName.Text = settings.StudentName;
textFathersName.Text = settings.FatherName;
textInstituteName.Text = settings.Institute;
textLastDegree.Text = settings.LastDegree;
textPercentage.Text = settings.Percentage;
textEmailId.Text = settings.EmailId;
SaveButton.Visible = false;
}
private void SaveButton_Click(object sender, EventArgs e)
{
if (boolPercentValidation)
{
if (boolEmailValidation)
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
settings.Save(saveFileDialog1.FileName);
FunctionEmpty();
}
}
else
{
MessageBox.Show(Constants.EmailError, Constants.Error1, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
MessageBox.Show(Constants.PercentageError, Constants.Error1, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void FunctionEmpty()
{
textStudentName.Text = string.Empty;
textFathersName.Text = string.Empty;
textInstituteName.Text = string.Empty;
textLastDegree.Text = string.Empty;
textPercentage.Text = string.Empty;
textEmailId.Text = string.Empty;
}
private void textStudentName_TextChanged(object sender, EventArgs e)
{
settings.StudentName = textStudentName.Text;
}
private void textFathersName_TextChanged(object sender, EventArgs e)
{
settings.FatherName = textFathersName.Text;
}
private void textInstituteName_TextChanged(object sender, EventArgs e)
{
settings.Institute = textInstituteName.Text;
}
private void textLastDegree_TextChanged (object sender, EventArgs e)
{
settings.LastDegree = textLastDegree.Text;
}
private void textPercentage_TextChanged (object sender, EventArgs e)
{
boolPercentValidation = true;
Regex objNotNaturalPattern = new Regex(Constants.PercentageRegularExpression);
if (!objNotNaturalPattern.IsMatch(textPercentage.Text))
{
if (textPercentage.Text != string.Empty)
{
int Marks = Convert.ToInt32(textPercentage.Text);
if (Marks > 100)
{
boolPercentValidation = false;
}
settings.Percentage = textPercentage.Text;
}
}
else
{
boolPercentValidation = false;
}
}
private void textEmailId_TextChanged (object sender, EventArgs e)
{
boolEmailValidation = true;
Regex objNotNaturalPattern = new Regex(Constants.EmailIdRegularExpression);
if (objNotNaturalPattern.IsMatch(textEmailId.Text))
{
boolEmailValidation = true;
settings.EmailId = textEmailId.Text;
}
else
{
boolEmailValidation = false;
}
}
private void CancelButton_Click(object sender, EventArgs e)
{
this.Dispose();
}
}
}
/*Reflection.Designer View*/
namespace ReflectionExample
{
partial class Reflection
{
///
/// Required designer variable.
///
private System.ComponentModel.IContainer components = null;
///
/// Clean up any resources being used.
///
/// true if managed resources should be disposed; otherwise, false.
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.Namelabel = new System.Windows.Forms.Label();
this.textStudentName = new System.Windows.Forms.TextBox();
this.FNamelabel = new System.Windows.Forms.Label();
this.textFathersName = new System.Windows.Forms.TextBox();
this.Instituelabel = new System.Windows.Forms.Label();
this.textInstituteName = new System.Windows.Forms.TextBox();
this.Institutelabel = new System.Windows.Forms.Label();
this.textLastDegree = new System.Windows.Forms.TextBox();
this.Percentagelabel = new System.Windows.Forms.Label();
this.textPercentage = new System.Windows.Forms.TextBox();
this.Emaillabel = new System.Windows.Forms.Label();
this.textEmailId = new System.Windows.Forms.TextBox();
this.SaveButton = new System.Windows.Forms.Button();
this.RetrieveButton = new System.Windows.Forms.Button();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
this.CancelButton = new System.Windows.Forms.Button();
this.numericUpDown1 = new System.Windows.Forms.NumericUpDown();
this.errorProvider1 = new System.Windows.Forms.ErrorProvider(this.components);
((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.errorProvider1)).BeginInit();
this.SuspendLayout();
//
// Namelabel
//
this.Namelabel.AutoSize = true;
this.Namelabel.Location = new System.Drawing.Point(68, 36);
this.Namelabel.Name = "Namelabel";
this.Namelabel.Size = new System.Drawing.Size(35, 13);
this.Namelabel.TabIndex = 0;
this.Namelabel.Text = "Name";
//
// textStudentName
//
this.textStudentName.Location = new System.Drawing.Point(185, 33);
this.textStudentName.MaxLength = 15;
this.textStudentName.Name = "textStudentName";
this.textStudentName.Size = new System.Drawing.Size(149, 20);
this.textStudentName.TabIndex = 1;
this.textStudentName.TextChanged += new System.EventHandler(this.textStudentName_TextChanged);
//
// FNamelabel
//
this.FNamelabel.AutoSize = true;
this.FNamelabel.Location = new System.Drawing.Point(28, 76);
this.FNamelabel.Name = "FNamelabel";
this.FNamelabel.Size = new System.Drawing.Size(75, 13);
this.FNamelabel.TabIndex = 2;
this.FNamelabel.Text = "Father\'s Name";
//
// textFathersName
//
this.textFathersName.Location = new System.Drawing.Point(185, 73);
this.textFathersName.MaxLength = 15;
this.textFathersName.Name = "textFathersName";
this.textFathersName.Size = new System.Drawing.Size(149, 20);
this.textFathersName.TabIndex = 3;
this.textFathersName.TextChanged += new System.EventHandler(this.textFathersName_TextChanged);
//
// Instituelabel
//
this.Instituelabel.AutoSize = true;
this.Instituelabel.Location = new System.Drawing.Point(59, 125);
this.Instituelabel.Name = "Instituelabel";
this.Instituelabel.Size = new System.Drawing.Size(44, 13);
this.Instituelabel.TabIndex = 4;
this.Instituelabel.Text = "Institute";
//
// textInstituteName
//
this.textInstituteName.Location = new System.Drawing.Point(185, 122);
this.textInstituteName.MaxLength = 25;
this.textInstituteName.Name = "textInstituteName";
this.textInstituteName.Size = new System.Drawing.Size(149, 20);
this.textInstituteName.TabIndex = 5;
this.textInstituteName.TextChanged += new System.EventHandler(this.textInstituteName_TextChanged);
//
// Institutelabel
//
this.Institutelabel.AutoSize = true;
this.Institutelabel.Location = new System.Drawing.Point(38, 174);
this.Institutelabel.Name = "Institutelabel";
this.Institutelabel.Size = new System.Drawing.Size(65, 13);
this.Institutelabel.TabIndex = 6;
this.Institutelabel.Text = "Last Degree";
//
// textLastDegree
//
this.textLastDegree.Location = new System.Drawing.Point(185, 171);
this.textLastDegree.MaxLength = 30;
this.textLastDegree.Name = "textLastDegree";
this.textLastDegree.Size = new System.Drawing.Size(149, 20);
this.textLastDegree.TabIndex = 7;
this.textLastDegree.TextChanged += new System.EventHandler(this.textLastDegree_TextChanged);
//
// Percentagelabel
//
this.Percentagelabel.AutoSize = true;
this.Percentagelabel.Location = new System.Drawing.Point(38, 220);
this.Percentagelabel.Name = "Percentagelabel";
this.Percentagelabel.Size = new System.Drawing.Size(62, 13);
this.Percentagelabel.TabIndex = 8;
this.Percentagelabel.Text = "Percentage";
//
// textPercentage
//
this.textPercentage.Location = new System.Drawing.Point(185, 217);
this.textPercentage.MaxLength = 3;
this.textPercentage.Name = "textPercentage";
this.textPercentage.Size = new System.Drawing.Size(149, 20);
this.textPercentage.TabIndex = 9;
this.textPercentage.TextChanged += new System.EventHandler(this.textPercentage_TextChanged);
//
// Emaillabel
//
this.Emaillabel.AutoSize = true;
this.Emaillabel.Location = new System.Drawing.Point(56, 274);
this.Emaillabel.Name = "Emaillabel";
this.Emaillabel.Size = new System.Drawing.Size(44, 13);
this.Emaillabel.TabIndex = 10;
this.Emaillabel.Text = "Email Id";
//
// textEmailId
//
this.textEmailId.Location = new System.Drawing.Point(185, 271);
this.textEmailId.MaxLength = 30;
this.textEmailId.Name = "textEmailId";
this.textEmailId.Size = new System.Drawing.Size(149, 20);
this.textEmailId.TabIndex = 11;
this.textEmailId.TextChanged += new System.EventHandler(this.textEmailId_TextChanged);
//
// SaveButton
//
this.SaveButton.Location = new System.Drawing.Point(185, 361);
this.SaveButton.Name = "SaveButton";
this.SaveButton.Size = new System.Drawing.Size(75, 23);
this.SaveButton.TabIndex = 12;
this.SaveButton.Text = "Save";
this.SaveButton.UseVisualStyleBackColor = true;
this.SaveButton.Click += new System.EventHandler(this.SaveButton_Click);
//
// RetrieveButton
//
this.RetrieveButton.Location = new System.Drawing.Point(281, 361);
this.RetrieveButton.Name = "RetrieveButton";
this.RetrieveButton.Size = new System.Drawing.Size(75, 23);
this.RetrieveButton.TabIndex = 13;
this.RetrieveButton.Text = "Retrieve";
this.RetrieveButton.UseVisualStyleBackColor = true;
this.RetrieveButton.Click += new System.EventHandler(this.RetrieveButton_Click);
//
// openFileDialog1
//
this.openFileDialog1.FileName = "openFileDialog1";
this.openFileDialog1.Filter = "XML Files|*.xml|All Files|*.*";
//
// saveFileDialog1
//
this.saveFileDialog1.DefaultExt = "xml";
this.saveFileDialog1.Filter = "XML Files|*.xml|All Files|*.*";
//
// CancelButton
//
this.CancelButton.Location = new System.Drawing.Point(89, 361);
this.CancelButton.Name = "CancelButton";
this.CancelButton.Size = new System.Drawing.Size(75, 23);
this.CancelButton.TabIndex = 14;
this.CancelButton.Text = "Cancel";
this.CancelButton.UseVisualStyleBackColor = true;
this.CancelButton.Click += new System.EventHandler(this.CancelButton_Click);
//
// numericUpDown1
//
this.numericUpDown1.Location = new System.Drawing.Point(185, 245);
this.numericUpDown1.Name = "numericUpDown1";
this.numericUpDown1.Size = new System.Drawing.Size(120, 20);
this.numericUpDown1.TabIndex = 15;
//
// errorProvider1
//
this.errorProvider1.ContainerControl = this;
//
// Reflection
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(416, 406);
this.Controls.Add(this.numericUpDown1);
this.Controls.Add(this.CancelButton);
this.Controls.Add(this.RetrieveButton);
this.Controls.Add(this.SaveButton);
this.Controls.Add(this.textEmailId);
this.Controls.Add(this.Emaillabel);
this.Controls.Add(this.textPercentage);
this.Controls.Add(this.Percentagelabel);
this.Controls.Add(this.textLastDegree);
this.Controls.Add(this.Institutelabel);
this.Controls.Add(this.textInstituteName);
this.Controls.Add(this.Instituelabel);
this.Controls.Add(this.textFathersName);
this.Controls.Add(this.FNamelabel);
this.Controls.Add(this.textStudentName);
this.Controls.Add(this.Namelabel);
this.Name = "Reflection";
this.Text = "Student-Information";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.errorProvider1)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label Namelabel;
private System.Windows.Forms.TextBox textStudentName;
private System.Windows.Forms.Label FNamelabel;
private System.Windows.Forms.TextBox textFathersName;
private System.Windows.Forms.Label Instituelabel;
private System.Windows.Forms.TextBox textInstituteName;
private System.Windows.Forms.Label Institutelabel;
private System.Windows.Forms.TextBox textLastDegree;
private System.Windows.Forms.Label Percentagelabel;
private System.Windows.Forms.TextBox textPercentage;
private System.Windows.Forms.Label Emaillabel;
private System.Windows.Forms.TextBox textEmailId;
private System.Windows.Forms.Button SaveButton;
private System.Windows.Forms.Button RetrieveButton;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.SaveFileDialog saveFileDialog1;
private System.Windows.Forms.Button CancelButton;
private System.Windows.Forms.NumericUpDown numericUpDown1;
private System.Windows.Forms.ErrorProvider errorProvider1;
}
}
/*settings.cs*/
/*This class contains how to create a xml file how data can store in xml file and how can we extract*/
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using System.Drawing;
using System.Reflection;
using System.Xml.Serialization;
namespace ReflectionExample
{
#region - public abstract class:XmlSerializable -
public class XmlSerializable
{
#region - public virtual void:Save(string path) -
public virtual void Save(string path)
{
StreamWriter objStreamWriter = new StreamWriter(path);
XmlSerializer objXmlSerializer = new XmlSerializer(this.GetType());
objXmlSerializer.Serialize(objStreamWriter, this);
objStreamWriter.Close();
}
#endregion
#region - public virtual void: Load(string path) -
public virtual void Load(string path)
{
if (File.Exists(path))
{
StreamReader objStreamReader = new StreamReader(path);
XmlTextReader objXmlTextReader = new XmlTextReader(objStreamReader);
XmlSerializer objXmlSerializer = new XmlSerializer(this.GetType());
object obj;
if (objXmlSerializer.CanDeserialize(objXmlTextReader))
{
obj = objXmlSerializer.Deserialize(objXmlTextReader);
Type objType = this.GetType();
PropertyInfo[] properties = objType.GetProperties();
foreach (PropertyInfo objProperty in properties)
{
objProperty.SetValue(this, objProperty.GetValue(obj, null), null);
}
}
objXmlTextReader.Close();
objStreamReader.Close();
}
}
#endregion
}
#endregion
public class Settings : XmlSerializable
{
[XmlIgnore()]
public string StudentName
{
get { return _StudentName; }
set { _StudentName = value; }
}
private string _StudentName = string.Empty;
[XmlElement("StudentName")]
public string XmlStudentName
{
get { return _StudentName; }
set { _StudentName = value; }
}
[XmlIgnore()]
public string FatherName
{
get { return _FatherName; }
set { _FatherName = value; }
}
private string _FatherName = string.Empty;
[XmlElement("FatherName")]
public string XmlFatherName
{
get { return _FatherName; }
set { _FatherName = value; }
}
[XmlIgnore()]
public string Institute
{
get { return _Institute; }
set { _Institute = value; }
}
private string _Institute =string.Empty;
[XmlElement("Institute")]
public string XmlInstitute
{
get { return _Institute; }
set { _Institute = value; }
}
[XmlIgnore()]
public string LastDegree
{
get { return _LastDegree; }
set { _LastDegree = value; }
}
private string _LastDegree = string.Empty;
[XmlElement("LastDegree")]
public string XmlLastDegree
{
get { return _LastDegree; }
set { _LastDegree = value; }
}
[XmlIgnore()]
public string Percentage
{
get { return _Percentage; }
set { _Percentage = value; }
}
private string _Percentage = string.Empty;
[XmlElement("Percentage")]
public string XmlPercentage
{
get { return _Percentage; }
set { _Percentage = value; }
}
[XmlIgnore()]
public string EmailId
{
get { return _EmailId; }
set { _EmailId = value; }
}
private string _EmailId = string.Empty;
[XmlElement("EmailId")]
public string XmlEmailId
{
get { return _EmailId; }
set { _EmailId = value; }
}
}
}
/*Constants.cs*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace ReflectionExample
{
class Constants
{
internal const string EmailError = "Email Id is not valid!!";
internal const string Error1 = "Error";
internal const string PercentageError = "Percentage must be less than 100!!";
internal const string PercentageRegularExpression=@"[^0-9]";
internal const string EmailIdRegularExpression = @"^[a-zA-Z0-9_\-\.]+@([\w-]+\.)+[\w-]+$";
}
}
0 comments:
Post a Comment