Monday, April 11, 2016

C# Tutorial

THREAD EXAMPLE:
        Thread t;
        string myString = "";
        private void button4_Click(object sender, EventArgs e)
        {
            t = new Thread(Write);
            object[] objA = { "Steve", 300 };
            t.Start(objA);
            while (t.IsAlive) ;
            {
                textBox1.Text = myString;
            }
        }
        void Write(object array)
        {
            object[] o = array as object[];
            for (int i = 0; i < Convert.ToInt32(o[1]); i++)
            {
                Thread.Sleep(10); // 0.5 sec
                myString += o[0].ToString() + i.ToString() + "\r\n";
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            t.Abort();
        }
WebClient Class Downloading Files:
        private void button4_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                WebClient wc = new WebClient();
                wc.DownloadFileAsync(new Uri("http://text"), sfd.FileName);
                wc.DownloadFileCompleted += new AsyncCompletedEventHandler(wc_download);
                wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wc_donloadchanged);
            }
        }

        private void wc_donloadchanged(object sender, DownloadProgressChangedEventArgs e)
        {
            // label1.Text = "Progress: " + e.ProgressPercentage.ToString();
        }

        private void wc_download(object sender, AsyncCompletedEventArgs e)
        {
            MessageBox.Show("File is downloaded");
        }
FONT DIALOG:
        private void button4_Click(object sender, EventArgs e)
        {
            FontDialog fd = new FontDialog();
            fd.MinSize = 10;
            fd.MaxSize = 20;
            fd.ShowColor = true;
            fd.ShowHelp = true;
            fd.HelpRequest += fd_HelpRequest;
            if (fd.ShowDialog() == DialogResult.OK)
            {
                textBox1.Font = fd.Font;
                textBox1.ForeColor = fd.Color;
            }
        }

        void fd_HelpRequest(object sender, EventArgs e)
        {
            MessageBox.Show("This is help button");
        }
PictureBox:
        private void button4_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                Image img = Image.FromFile(ofd.FileName);
                pictureBox1.Image = img;
            }
        }

NOTIFY ICON CONTROL:
        private void button1_Click(object sender, EventArgs e)
        {
            this.Hide();
            notifyIcon1.ShowBalloonTip(1000, "Still Running", "Your app is still running", ToolTipIcon.Info);
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Show();
        }
TreeView:
        private void button1_Click(object sender, EventArgs e)
        {
            treeView1.Nodes.Add("People");
            treeView1.Nodes.Add("Animals");
            treeView1.Nodes[0].Nodes.Add("Adam");
            treeView1.Nodes[0].Nodes.Add("Sam");
            treeView1.Nodes[1].Nodes.Add("Dog");
            treeView1.Nodes[1].Nodes.Add("Cat");
            treeView1.Nodes[1].Nodes[0].Nodes.Add("Spot");
        }
        List<TreeNode> tnlist = new List<TreeNode>();
        private void button2_Click(object sender, EventArgs e)
        {
            RemoveChecked(treeView1.Nodes);  
        }

        void RemoveChecked(TreeNodeCollection tnc)
        {
            foreach (TreeNode tn in tnc)
                if (tn.Checked) tnlist.Add(tn);
                else if (tn.Nodes.Count != 0) RemoveChecked(tn.Nodes);
            foreach (TreeNode tn in tnlist)
                treeView1.Nodes.Remove(tn);
        }
Propert Grid:
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Person p = new Person();
        private void button1_Click(object sender, EventArgs e)
        {
            p.Name = "Adam";
            p.Age = 15;
            p.Email = "google@gmail.com";
            propertyGrid1.SelectedObject = p;
            Reload();
        }
        void Reload()
        {
            textBox1.Text = p.Name;
            textBox2.Text = p.Age.ToString();
            textBox3.Text = p.Email;
        }

        private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            Reload();
        }

    }
    class Person
    {
        public string Name
        {
            get;
            set;
        }
        public int Age
        {
            get;
            set;
        }
        public string Email
        {
            get;
            set;
        }
    }

}

No comments:

Post a Comment