Monday, April 11, 2016

C# Tutorial

READING XML FILE:
        private void button1_Click_1(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "XML|*.xml";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                XmlDocument xdoc = new XmlDocument();
                xdoc.Load(ofd.FileName);
                foreach(XmlNode node in xdoc.SelectNodes("People/Person"))
                    MessageBox.Show(node.SelectSingleNode("Name").InnerText);
            }
        }
XML FILE:
<People>
       <Person>
              <Name>Adam</Name>
              <Age>15</Age>
              <Email>google@gmail.com</Email>
       </Person>
       <Person>
              <Name>Zoloo</Name>
              <Age>24</Age>
              <Email>zoloo@gmail.com</Email>
       </Person>
</People>
Editing XML:
        XmlDocument xdoc;
        string path;
        private void button1_Click_1(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "XML|*.xml";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                path = ofd.FileName;
                xdoc = new XmlDocument();
                xdoc.Load(path);
                textBox1.Text = xdoc.SelectSingleNode("People/Person/Name").InnerText;
                numericUpDown1.Value = Convert.ToInt32(xdoc.SelectSingleNode("People/Person/Age").InnerText);
                textBox2.Text = xdoc.SelectSingleNode("People/Person/Email").InnerText;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            xdoc.SelectSingleNode("People/Person/Name").InnerText = textBox1.Text;
            xdoc.SelectSingleNode("People/Person/Age").InnerText = numericUpDown1.Value.ToString();
            xdoc.SelectSingleNode("People/Person/Email").InnerText = textBox2.Text;
            xdoc.Save(path);
        }
DELETING A XML NODE:
        private void button4_Click(object sender, EventArgs e)
        {
            xdoc = new XmlDocument();
            xdoc.Load("xDoc1.xml");
            foreach (XmlNode xnode in xdoc.SelectNodes("People/Person"))
                if (xnode.SelectSingleNode("Name").InnerText == textBox1.Text) xnode.ParentNode.RemoveChild(xnode);
            xdoc.Save("xDoc1.xml");
        }
MD5 AND SHA1:
        private void button1_Click(object sender, EventArgs e)
        {
            // MD5
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            UTF8Encoding utf8 = new UTF8Encoding();
            MessageBox.Show(BitConverter.ToString(md5.ComputeHash(utf8.GetBytes(textBox1.Text))));
            // SHA1
            SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
            MessageBox.Show(BitConverter.ToString(sha1.ComputeHash(utf8.GetBytes(textBox1.Text))));
        }
Drag and Drop:
        private void panel1_DragOver(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.All;
        }

        private void panel1_DragDrop(object sender, DragEventArgs e)
        {
            string[] files = e.Data.GetData(DataFormats.FileDrop) as string[];
            foreach(string s in files)
                MessageBox.Show(s);
        }
Drawing Shapes:
        private void panel1_Click(object sender, EventArgs e)
        {
            SolidBrush sb = new SolidBrush(Color.Green);
            Graphics g = panel1.CreateGraphics();
            g.FillRectangle(sb, 20, 20, 100, 50);
            g.FillEllipse(sb, 100, 100, 50, 50);
            g.FillPie(sb, 150, 150, 60, 60, 0, 270);
        }
Drawing with Pen class:
        private void panel1_Click(object sender, EventArgs e)
        {
            Pen p = new Pen(Color.Red, 3);
            Graphics g = panel1.CreateGraphics();
            g.DrawArc(p, 20, 20, 100, 100, 0, 90);
            g.DrawLine(p, new Point(0, 0), new Point(100, 100));
        }
Drawing Strings Text:
        private void panel1_Click(object sender, EventArgs e)
        {
            SolidBrush sb = new SolidBrush(Color.Blue);
            Pen p = new Pen(Color.Red, 3);
            Graphics g = panel1.CreateGraphics();
            FontFamily ff = new FontFamily("Arial");
            System.Drawing.Font font = new System.Drawing.Font(ff, 16, FontStyle.Italic);
            g.DrawString("Adam", font, sb, new Point(20, 20));
        }
Multiple Colors in a LinearGradientBrush:
        private void panel1_Click(object sender, EventArgs e)
        {
            LinearGradientBrush lgb = new LinearGradientBrush(new Point(20, 20), new Point(20, 70), Color.Red, Color.Yellow);
            SolidBrush sb = new SolidBrush(Color.Blue);
            Pen p = new Pen(Color.Red, 3);
            Graphics g = panel1.CreateGraphics();
            g.FillRectangle(lgb, 20, 20, 50, 50);
            g.FillEllipse(lgb, 100, 100, 100, 100);
// Multiple Colors:
            ColorBlend cb = new ColorBlend();
            cb.Colors = new Color[] { Color.Black, Color.Blue, Color.SkyBlue, Color.White };
            cb.Positions = new float[] { 0, .33F, .66F, 1F };
            lgb.InterpolationColors = cb;
            g.FillRectangle(lgb, 20, 20, 50, 50);
        }
PathGradientBrush :
        private void panel1_Click(object sender, EventArgs e)
        {
            GraphicsPath gp = new GraphicsPath();
            gp.AddEllipse(20, 20, 100, 100);
            PathGradientBrush pgb = new PathGradientBrush(gp);
            pgb.CenterColor = Color.White;
            pgb.SurroundColors = new Color[] { Color.Black };
            Graphics g = panel1.CreateGraphics();
            g.FillEllipse(pgb, 20, 20, 100, 100);
        }
CAPTURING SCREEN
        private void button1_Click(object sender, EventArgs e)
        {
            // screen
            /*
            Bitmap b = new Bitmap(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
            Graphics g = Graphics.FromImage(b);
            g.CopyFromScreen(Point.Empty, Point.Empty, Screen.PrimaryScreen.WorkingArea.Size);
            pictureBox1.Image = b;
             */
            Thread t = new Thread(Blah);
            t.Start();
        }
        void Blah()
        {
            for (; ; )
            {
                Bitmap b = new Bitmap(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
                Graphics g = Graphics.FromImage(b);
                g.CopyFromScreen(Point.Empty, Point.Empty, Screen.PrimaryScreen.WorkingArea.Size);
                pictureBox1.Image = b;
            }
        }
Making Keyboard Shortcuts
        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.Control && e.KeyCode.ToString() == "F")
                MessageBox.Show("You pressed ctrl and F from the textbox");
            if (e.Alt && e.KeyCode.ToString() == "F")
                MessageBox.Show("You pressed alt and F from the textbox");
            if (e.Shift && e.KeyCode.ToString() == "F")
                MessageBox.Show("You pressed Shift and F from the textbox");
        }


No comments:

Post a Comment