Noice Maker


   

/*
Code revised from chapter 1


GDI+ Custom Controls with Visual C# 2005
By Iulian Serban, Dragos Brezoi, Tiberiu Radu, Adam Ward 

Language English
Paperback 272 pages [191mm x 235mm]
Release date July 2006
ISBN 1904811604

Sample chapter
http://international.us.server12.fileserver.kutayzorlu.com/files/download/2017/01/1604_CustomControls_SampleChapter_uuid-fed6cfa1-6995-4780-8833-5b589ec60210_crc-0.pdf


For More info on GDI+ Custom Control with Microsoft Visual C# book 
visit website www.packtpub.com 


*/ 



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Media;
using System.IO;

    public partial class Form1 : Form
    {
        private TinyNoiseMaker tinyNoiseMaker1;
        private System.Windows.Forms.Button playButton;

        private System.ComponentModel.IContainer components = null;

        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing &amp;&amp; (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.playButton = new System.Windows.Forms.Button();
            this.tinyNoiseMaker1 = new TinyNoiseMaker();
            this.SuspendLayout();
            // 
            // playButton
            // 
            this.playButton.Location = new System.Drawing.Point(100, 22);
            this.playButton.Name = "playButton";
            this.playButton.Size = new System.Drawing.Size(82, 23);
            this.playButton.TabIndex = 1;
            this.playButton.Text = "Play My File";
            this.playButton.UseVisualStyleBackColor = true;
            this.playButton.Click += new System.EventHandler(this.playButton_Click);
            // 
            // tinyNoiseMaker1
            // 
            this.tinyNoiseMaker1.BackColor = System.Drawing.SystemColors.ControlLight;
            this.tinyNoiseMaker1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.tinyNoiseMaker1.FileName = null;
            this.tinyNoiseMaker1.Location = new System.Drawing.Point(21, 68);
            this.tinyNoiseMaker1.Name = "tinyNoiseMaker1";
            this.tinyNoiseMaker1.Size = new System.Drawing.Size(268, 37);
            this.tinyNoiseMaker1.TabIndex = 0;
            this.tinyNoiseMaker1.PlayStart += new System.EventHandler(this.tinyNoiseMaker1_PlayStart);
            this.tinyNoiseMaker1.PlayStop += new System.EventHandler(this.tinyNoiseMaker1_PlayStop);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(316, 163);
            this.Controls.Add(this.playButton);
            this.Controls.Add(this.tinyNoiseMaker1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }

        #endregion

        private void tinyNoiseMaker1_PlayStart(object sender, EventArgs e)
        {
            Text = "Started Play of: " + tinyNoiseMaker1.FileName;
        }

        private void tinyNoiseMaker1_PlayStop(object sender, EventArgs e)
        {
            Text = "Stopped Play of : " + tinyNoiseMaker1.FileName;
        }

        private void playButton_Click(object sender, EventArgs e)
        {
            tinyNoiseMaker1.FileName = "C:WINDOWSMedia	ada.wav";
            tinyNoiseMaker1.Play();
        }
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
        
    }
    
    public partial class TinyNoiseMaker : UserControl
    {
        public event EventHandler PlayStart;
        public event EventHandler PlayStop;

        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing &amp;&amp; (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.openButton = new System.Windows.Forms.Button();
            this.playButton = new System.Windows.Forms.Button();
            this.stopButton = new System.Windows.Forms.Button();
            this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
            this.SuspendLayout();
            // 
            // openButton
            // 
            this.openButton.Location = new System.Drawing.Point(4, 4);
            this.openButton.Name = "openButton";
            this.openButton.Size = new System.Drawing.Size(82, 25);
            this.openButton.TabIndex = 0;
            this.openButton.Text = "Open";
            this.openButton.UseVisualStyleBackColor = true;
            this.openButton.Click += new System.EventHandler(this.openButton_Click);
            // 
            // playButton
            // 
            this.playButton.Location = new System.Drawing.Point(86, 4);
            this.playButton.Name = "playButton";
            this.playButton.Size = new System.Drawing.Size(82, 25);
            this.playButton.TabIndex = 1;
            this.playButton.Text = "Play";
            this.playButton.UseVisualStyleBackColor = true;
            this.playButton.Click += new System.EventHandler(this.playButton_Click);
            // 
            // stopButton
            // 
            this.stopButton.Location = new System.Drawing.Point(168, 4);
            this.stopButton.Name = "stopButton";
            this.stopButton.Size = new System.Drawing.Size(82, 25);
            this.stopButton.TabIndex = 2;
            this.stopButton.Text = "Stop";
            this.stopButton.UseVisualStyleBackColor = true;
            this.stopButton.Click += new System.EventHandler(this.stopButton_Click);
            // 
            // openFileDialog
            // 
            this.openFileDialog.FileName = "openFileDialog";
            // 
            // TinyNoiseMaker
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackColor = System.Drawing.SystemColors.ControlLight;
            this.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.Controls.Add(this.stopButton);
            this.Controls.Add(this.playButton);
            this.Controls.Add(this.openButton);
            this.Name = "TinyNoiseMaker";
            this.Size = new System.Drawing.Size(250, 30);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Button openButton;
        private System.Windows.Forms.Button playButton;
        private System.Windows.Forms.Button stopButton;
        private System.Windows.Forms.OpenFileDialog openFileDialog;

        protected virtual void OnPlayStart(EventArgs e)
        {
            if (PlayStart != null)
            {
                PlayStart(this, e);
            }
        }

        protected virtual void OnPlayStop(EventArgs e)
        {
            if (PlayStop != null)
            {
                PlayStop(this, e);
            }
        }
        
        
        SoundPlayer soundPlayer;
        public TinyNoiseMaker()
        {
            InitializeComponent();
            soundPlayer = new SoundPlayer();
        }

        private void openButton_Click(object sender, EventArgs e)
        {
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                FileName = openFileDialog.FileName;
            }
                
        }

        private void playButton_Click(object sender, EventArgs e)
        {
            Play();
        }

        private void stopButton_Click(object sender, EventArgs e)
        {
            Stop();
        }
        public void Play()
        {
            soundPlayer.Play();
            OnPlayStart(EventArgs.Empty);
        }
        public void Stop()
        {
            soundPlayer.Stop();
            OnPlayStop(EventArgs.Empty);
        }
        private string fileName;
        public string FileName
        {
            get
            {
                return fileName;
            }
            set
            {
                if (fileName != value)
                {
                    fileName = value;
                    soundPlayer.Stream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                }
            }
        }
    }


           
          


Round down a number


   

using System;
using System.Data;
using System.Text.RegularExpressions;

class Class1{
        static void Main(string[] args){
      Console.WriteLine(RoundDown(.4));
      Console.WriteLine(RoundDown(.5));
      Console.WriteLine(RoundDown(.6));
      Console.WriteLine(RoundDown(1.4));
      Console.WriteLine(RoundDown(1.5));
      Console.WriteLine(RoundDown(1.6));
      Console.WriteLine(RoundDown(2.4));
      Console.WriteLine(RoundDown(2.5));
      Console.WriteLine(RoundDown(2.6));
      Console.WriteLine(RoundDown(3.4));
      Console.WriteLine(RoundDown(3.5));
      Console.WriteLine(RoundDown(3.6));
        }
    public static double RoundDown(double valueToRound)
    {
      double floorValue = Math.Floor(valueToRound);
      if ((valueToRound - floorValue) > .5)
      {
        return (floorValue + 1);
      }
      else
      {
        return (floorValue);
      }
    }
}


           
          


Round up a number


   


using System;
using System.Data;
using System.Text.RegularExpressions;

class Class1{
        static void Main(string[] args){
      Console.WriteLine(RoundUp(.4));
      Console.WriteLine(RoundUp(.5));
      Console.WriteLine(RoundUp(.6));
      Console.WriteLine(RoundUp(1.4));
      Console.WriteLine(RoundUp(1.5));
      Console.WriteLine(RoundUp(1.6));
      Console.WriteLine(RoundUp(2.4));
      Console.WriteLine(RoundUp(2.5));
      Console.WriteLine(RoundUp(2.6));
      Console.WriteLine(RoundUp(3.4));
      Console.WriteLine(RoundUp(3.5));
      Console.WriteLine(RoundUp(3.6));
        }
    public static double RoundUp(double valueToRound)
    {
      return (Math.Floor(valueToRound + 0.5));
    }

}

           
          


Rounding a Floating Point Value


   


using System;
using System.Data;
using System.Text.RegularExpressions;

class Class1{
        static void Main(string[] args){
      int X = (int)Math.Round(2.5555);
      Console.WriteLine(X);
      Console.WriteLine(Math.Round(2.5555, 2));
      Console.WriteLine(Math.Round(2.444444,3));
      Console.WriteLine(Math.Round(2.666666666666666666666666666660001));
      Console.WriteLine(Math.Round(2.4444444444444444444444440001));
      Console.WriteLine(Math.Round(.5));
      Console.WriteLine(Math.Round(1.5));
      Console.WriteLine(Math.Round(2.5));
      Console.WriteLine(Math.Round(3.5));
      Console.WriteLine();
      
      Console.WriteLine(Math.Floor(.5));
      Console.WriteLine(Math.Floor(1.5));
      Console.WriteLine(Math.Floor(2.5));
      Console.WriteLine(Math.Floor(3.5));
      Console.WriteLine();
      
      Console.WriteLine(Math.Ceiling(.5));
      Console.WriteLine(Math.Ceiling(1.5));
      Console.WriteLine(Math.Ceiling(2.5));
      Console.WriteLine(Math.Ceiling(3.5));  
      Console.WriteLine();
        }
}

           
          


Load Rescouce BMP image


   

  using System;
  using System.Drawing;
  using System.Collections;
  using System.ComponentModel;
  using System.Windows.Forms;
  using System.Data;

  using System.Resources;

  public class MainForm : System.Windows.Forms.Form
  {
    private System.Windows.Forms.PictureBox pictureBox1;
    private System.Windows.Forms.Button btnLoadRes;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.PictureBox pictureBox2;

    public MainForm()
    {
      InitializeComponent();
      CenterToScreen();
    }
    private void InitializeComponent()
    {
      System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(MainForm));
      this.pictureBox2 = new System.Windows.Forms.PictureBox();
      this.pictureBox1 = new System.Windows.Forms.PictureBox();
      this.btnLoadRes = new System.Windows.Forms.Button();
      this.label1 = new System.Windows.Forms.Label();
      this.SuspendLayout();
      // 
      // pictureBox2
      // 
      this.pictureBox2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
      this.pictureBox2.Location = new System.Drawing.Point(176, 72);
      this.pictureBox2.Name = "pictureBox2";
      this.pictureBox2.Size = new System.Drawing.Size(32, 32);
      this.pictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
      this.pictureBox2.TabIndex = 5;
      this.pictureBox2.TabStop = false;
      // 
      // pictureBox1
      // 
      this.pictureBox1.Image = ((System.Drawing.Bitmap)(resources.GetObject("pictureBox1.Image")));
      this.pictureBox1.Location = new System.Drawing.Point(176, 16);
      this.pictureBox1.Name = "pictureBox1";
      this.pictureBox1.Size = new System.Drawing.Size(32, 32);
      this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
      this.pictureBox1.TabIndex = 1;
      this.pictureBox1.TabStop = false;
      // 
      // btnLoadRes
      // 
      this.btnLoadRes.Location = new System.Drawing.Point(8, 80);
      this.btnLoadRes.Name = "btnLoadRes";
      this.btnLoadRes.Size = new System.Drawing.Size(144, 23);
      this.btnLoadRes.TabIndex = 4;
      this.btnLoadRes.Text = "Load Happy dude!";
      this.btnLoadRes.Click += new System.EventHandler(this.btnLoadRes_Click);
      // 
      // label1
      // 
      this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold);
      this.label1.Location = new System.Drawing.Point(8, 16);
      this.label1.Name = "label1";
      this.label1.Size = new System.Drawing.Size(152, 40);
      this.label1.TabIndex = 0;
      this.label1.Text = "Here is the image:";
      // 
      // MainForm
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(235, 128);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.pictureBox2,
                                      this.btnLoadRes,
                                      this.pictureBox1,
                                      this.label1});
      this.Name = "MainForm";
      this.Text = "Resource Loader";
      this.ResumeLayout(false);

    }
    static void Main() 
    {
      Application.Run(new MainForm());
    }

    private void btnLoadRes_Click(object sender, System.EventArgs e)
    {
      ResourceManager resources = new ResourceManager (typeof(MainForm));
      
      this.pictureBox2.Image = 
        ((System.Drawing.Bitmap)(resources.GetObject("pictureBox1.Image")));
      resources.ReleaseAllResources();  
    }
      
  }


           
          


Creating a new resource reader


   


using System;
using System.Resources;
using System.Collections;

public class MyResourceReader : IResourceReader, IEnumerable {
  private Hashtable dict;
  private string fResName;

  void IResourceReader.Close() {
  }

  public void Dispose(){
    
  }
  IDictionaryEnumerator IResourceReader.GetEnumerator()
  {
    return dict.GetEnumerator();
  }

  IEnumerator IEnumerable.GetEnumerator()
  {
    return dict.GetEnumerator();
  }

  public MyResourceReader(string resName)
  {
    fResName = resName;
    dict = new Hashtable();
    dict.Add("Greeting", "Hello");
    dict.Add("Program", "My Program");
    dict.Add("Test Resource", "www.kutayzorlu.com/java2s/com");
  }
}

class Test {
  public static void Main() {
    MyResourceReader reader = new MyResourceReader("MyResources");
    IDictionaryEnumerator dict = ((IResourceReader)reader).GetEnumerator();
    while ( dict.MoveNext() )
    {
     string s = (string)dict.Key;
     if ( s == "Greeting" )
       Console.WriteLine("{0}", dict.Value);
    }
  }
}