Monday, April 4, 2016

C# Tutorial 1-70

        private void button1_Click(object sender, EventArgs e)
        {
            object ob = "Zoloo";
            MessageBox.Show(ob.ToString());
            List<int> Num = new List<int>();
            Num.Add(5);
            Num.Add(25);
            Num.Add(125);
            foreach ( int i in Num)
                MessageBox.Show(i.ToString());
        }

        private void button2_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 10; i++)
            {
                if (i == 2) continue;
                textBox1.Text += i.ToString();
            }
        }
// Throwing Exceptions
        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                MyClass.CheckString("");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
// MyClass Code:
    class MyClass
    {
        static Exception myexp = new Exception("You cant set this                        string equal to an empty string");
        public static void CheckString(string myString)
        {
            if (myString == "") throw myexp;
        }
    }

// More on Inheritance and Interfaces
    class MyClass
    {
        private string Name = "Bob";
        public int Age = 19;
        public virtual void ShowMessage(string Message)
        {
            System.Windows.Forms.MessageBox.Show(Message);
        }
    }

    class MySecondClass : MyClass, IMyInterface
    {
        public string HairColor = "Brown";
        public void MessageBoxSpecial()
        {
            base.ShowMessage(base.Age.ToString());
            // System.Windows.Forms.MessageBox.Show(base.Age.ToString());
        }
        public override void ShowMessage(string Message)
        {
            System.Windows.Forms.MessageBox.Show(Message, "My Title");
        }
        public void myVoid()
        {
            base.ShowMessage("My Void");
        }
    }

    interface IMyInterface
    {
        void myVoid();
    }

// FORM
            MySecondClass msc = new MySecondClass();
            msc.myVoid();

// DELEGATES

    class MyClass
    {
        delegate void MyDelegate(string myString);
        public void ShowThoseMessages()
        {
            MyDelegate md = new MyDelegate(ShowMessage);
            md += ShowAnotherMessage;
            md("Zoloo");
        }
        void ShowMessage(string message)
        {
            System.Windows.Forms.MessageBox.Show(message);
        }
        void ShowAnotherMessage(string a)
        {
            System.Windows.Forms.MessageBox.Show(a, "Test");
        }
    }
FORM
        private void button1_Click(object sender, EventArgs e)
        {
            MyClass mc = new MyClass();
            mc.ShowThoseMessages();
        }
// EVENTS
    class MyClass
    {
        public event EventHandler onProperty;
        string name = "";
        public string Name
        {
            get { return name; }
            set
            {
                name = value;
                onProperty(this, new EventArgs());
            }

        }
    }
FORM
        private void button1_Click(object sender, EventArgs e)
        {
            MyClass mc = new MyClass();
            mc.onProperty += mc_onProperty;
            mc.Name = "Zoloo";
        }

        void mc_onProperty(object sender, EventArgs e)
        {
            MessageBox.Show("The property has changed");
        }

// TERNARY OPERATOR
        private void button2_Click(object sender, EventArgs e)
        {
           // string myString = (checkBox1.Checked) ? "Checked" : "Not Checked";
            MessageBox.Show((checkBox1.Checked) ? "Checked" : "Not Checked");
        }

// OPEN FILE DIALOG
    private void button3_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Excel File|*.xlsx|Word File|*.docx";
            ofd.Title = "Open Document";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
             //   MessageBox.Show(ofd.FileName);
                MessageBox.Show(ofd.SafeFileName);
            }

        }
STREAMREADER AND STREAMWRITER
        string path;
        private void button4_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                button5.Enabled = true;
                path = ofd.FileName;
                // StreamReader sr = new StreamReader(File.OpenRead(ofd.FileName));
                // textBox1.Text = sr.ReadToEnd();
                // sr.Dispose();
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            StreamWriter sw = new StreamWriter(File.Create(path));
            sw.Write(textBox1.Text);
            sw.WriteLine("This is the second line");
            sw.Dispose();
        }
BINARYREADER and BINARYWRITER
        private void button5_Click(object sender, EventArgs e)
        {
            BinaryReader br = new BinaryReader(File.OpenRead(path));
            textBox1.Text = br.ReadChar().ToString();
            br.Dispose();
        }





SAVE FILE DIALOG
        private void button4_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "Text File|*.txt";
            sfd.FileName = "My Text File";
            sfd.Title = "Save Text File";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                string path = sfd.FileName;
                BinaryWriter bw = new BinaryWriter(File.Create(path));
                bw.Write("Example text file.");
                bw.Dispose();
            }
        }
Is as and Casting
1            object myOb = "Adam";
            if(myOb is string) {
                     string myString = myOb as string;
MessageBox.Show(myString);
}
SUBSTRING:
            string Name = "John Smith Junior";
            string Firstname = Name.Substring(5, 5);
            MessageBox.Show(Firstname);

 Index of:
     string Name = "           John Smith           ";
            string Firstname = Name.TrimStart();
            MessageBox.Show(Firstname);
Remove:
                 string sentence = "Hello, my name is Adam.";
            string after = sentence.Remove(0, 7)
            // string after = sentence.Remove(0, 7);
            MessageBox.Show(after);
Replace:
            string sentence = "Hello, my name is Adam.";
            string after = sentence.Replace("a", "o");
            MessageBox.Show(after);
Split:
            string names = "Adam;Bob;Joe;Steve;Allen;Matt";
            string[] nameArray = names.Split(';');
            foreach( string name in nameArray )
                MessageBox.Show(name);
ToCharArray:
            string letters = "abcdefg";
            char[] letter = letters.ToCharArray();
            foreach(char c in letter)
                MessageBox.Show(c.ToString());
Generating bytes:
            byte[] buffer = new byte[5];
            Random r = new Random();
            r.NextBytes(buffer);
            MessageBox.Show(BitConverter.ToString(buffer));
Generating String:
        private void button4_Click(object sender, EventArgs e)
        {
            char[] letters = "qwertyuioplkjhgfdsazxcvbnm123456789".ToCharArray();
            Random r = new Random();
            string randString = "";
            for (int i = 0; i < 5; i++)
            {
                randString += letters[r.Next(0, 25)].ToString();
            }
            MessageBox.Show(randString);
        }
FolderBrowserDialog:
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.Description = "Zoloo was here";
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                MessageBox.Show(fbd.SelectedPath);
            }
NULL Coalesce Operator:
            string myStr = "Adam";
            MessageBox.Show(myStr ?? "The string is null");
Directory Class:
     private void button4_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog ofd = new FolderBrowserDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                string[] files = Directory.GetDirectories(ofd.SelectedPath);
                foreach (string s in files)
                {
                   // MessageBox.Show(s);
                    MessageBox.Show(Directory.GetCreationTime(ofd.SelectedPath).ToString());
                }
                //  Directory.CreateDirectory(ofd.SelectedPath + "\\Zoloo");
            }
        }
File Class:
// delete
        private void button4_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
                File.Delete(ofd.FileName);
        }
Path class:
       MessageBox.Show(Path.GetExtension(ofd.FileName));
Process class:
        private void button4_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
                Process.Start(ofd.FileName);
            Process.Start("cmd.exe");
            // foreach(Process p in Process.GetProcesses())
            // MessageBox.Show(p.ProcessName);
        }
Bitwise Operator:
            short mys = 3 & 5; // 011 & 101 = 00001
            mys = 3 & 4; // 011 & 100 = 000
            mys = 3 | 4; // 011 | 100 = 111
            mys = 3 | 5; // 011 | 101 = 111
            mys = 3 ^ 5; // 011 ^ 101 = 110
            mys = 3 ^ 4; // 011 ^ 100 = 111
            mys = 3 >> 1; // 011 >> 001 = 1
            mys = 5 >> 1; // 101 >> 001 = 10;
            mys = 5 << 1; // 101 << 001 = 1010
            mys = 3 << 1; // 011 << 001 = 110

            MessageBox.Show(Convert.ToString(mys, 2));

No comments:

Post a Comment