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); } } }
Resource file generator application for difference languages
using System; using System.Resources; class LanguageResourceWriter { public static void WriteEnglishResources() { ResourceWriter rw = new ResourceWriter("Eng.resources"); rw.AddResource("Greeting", "Hello"); rw.Generate(); rw.Close(); } public static void WriteSpanishResources() { ResourceWriter rw = new ResourceWriter("Span.resources"); rw.AddResource("Greeting", "Hola"); rw.Generate(); rw.Close(); } public static void WriteFrenchResources() { ResourceWriter rw = new ResourceWriter("French.resources"); rw.AddResource("Greeting", "Bonjour"); rw.Generate(); rw.Close(); } public static void Main() { WriteEnglishResources(); WriteSpanishResources(); WriteFrenchResources(); } }
Writing a resource file programmatically
using System; using System.Resources; class Test { public static void Main() { ResourceWriter rw = new ResourceWriter("English.resources"); rw.AddResource("Name", "Test"); rw.AddResource("Ver", 1.0 ); rw.AddResource("Author", "www.kutayzorlu.com/java2s/com"); rw.Generate(); rw.Close(); } }
Save and read value from resx resource file
using System; using System.Resources; using System.Drawing; using System.Collections; using System.Windows.Forms; using System.Resources; class ResourceGenerator { static void Main(string[] args) { ResXResourceWriter w = new ResXResourceWriter("ResXForm.resx"); Image i = new Bitmap("winter.jpg"); w.AddResource("myImage", i); w.AddResource("welcomeString", "www.kutayzorlu.com/java2s/com"); w.Generate(); w.Close(); // Make a resx reader. ResXResourceReader r = new ResXResourceReader("ResXForm.resx"); IDictionaryEnumerator en = r.GetEnumerator(); while (en.MoveNext()) { Console.WriteLine("Value:" + en.Value.ToString(), "Key: " + en.Key.ToString()); } r.Close(); } }