27 Jun

C# String Formatting For A Number Using Scientific Notation

This example shows how to format a number using scientific notation. To do this, you can use string.Format method.

Samples:

            double number1 = 12345.67890123;
            Console.WriteLine("Default Format: " + number1);
            Console.WriteLine("Scientific Notation: " + string.Format("{0:#.##E+0}", number1));
            Console.WriteLine();

            int number2 = 123456789;
            Console.WriteLine("Default Format: " + number2);
            Console.WriteLine("Scientific Notation: " + string.Format("{0:#.##E+0}", number2));
            Console.WriteLine();

            double number3 = 12345.67890123;
            Console.WriteLine("Default Format: " + number3);
            Console.WriteLine("Scientific Notation: " + string.Format("{0:#.##E+00}", number3));
            Console.WriteLine();

Output is like below:

Default Format: 12345.67890123
Scientific Notation: 1.23E+4

Default Format: 123456789
Scientific Notation: 1.23E+8

Default Format: 12345.67890123
Scientific Notation: 1.23E+04
27 Jun

C# Chart Control Example

This example shows how to display your data in your Windows Forms program as a bar graph or spline chart. To achieve this, you use Chart class in System.Windows.Forms.DataVisualization.Charting. Chart control can be found in Toolbox(.NET Framework 4.0 or newer). Older versions will not have the Chart control available.
To add a chart control to your application,

  • In design view, open the Toolbox.
  • From the Data category, drag a Chart control to the design area.

If you cannot see the Chart control in the Toolbox, right click in the Toolbox, select Choose Items, and then select the following namespaces in the .NET Framekwork Components tab:

  • System.Web.UI.DataVisualization.Charting
  • System.Windows.Forms.DataVisualization.Charting

C# Winform Choose Toolbox Items

Usage:

        void FrmChartExample_Load(object sender, EventArgs e)
        {
            BarExample(); //Show bar chart
            //SplineChartExample();
        }


This method shows how to add a bar chart.

        public void BarExample()
        {
            this.chartControl.Series.Clear();

            // Data arrays
            string[] seriesArray = { "Cat", "Dog", "Bird", "Monkey" };
            int[] pointsArray = { 2, 1, 7, 5 };

            // Set palette
            this.chartControl.Palette = ChartColorPalette.EarthTones;

            // Set title
            this.chartControl.Titles.Add("Animals");

            // Add series.
            for (int i = 0; i < seriesArray.Length; i++)
            {
                Series series = this.chartControl.Series.Add(seriesArray[i]);
                series.Points.Add(pointsArray[i]);
            }
        }

C# Bar Chart Example

This method shows how to add a spline chart.

        private void SplineChartExample()
        {
            this.chartControl.Series.Clear();

            this.chartControl.Titles.Add("Total Income");

            Series series = this.chartControl.Series.Add("Total Income");
            series.ChartType = SeriesChartType.Spline;
            series.Points.AddXY("September", 100);
            series.Points.AddXY("Obtober", 300);
            series.Points.AddXY("November", 800);
            series.Points.AddXY("December", 200);
            series.Points.AddXY("January", 600);
            series.Points.AddXY("February", 400);
        }

C# Spline Chart Example

26 Jun

C# Indent String With Spaces

This example shows how to indent strings using method for padding in C#. “String.PadLeft” static method is used to repeat spaces.

Usage:

            Console.WriteLine(Indent(0) + "Versions");
            Console.WriteLine(Indent(2) + "Version 1");
            Console.WriteLine(Indent(4) + "Version 1.1");
            Console.WriteLine(Indent(4) + "Version 1.2");
            Console.WriteLine(Indent(6) + "Version 1.2.1");
            Console.WriteLine(Indent(2) + "Version 2");
            Console.WriteLine(Indent(4) + "Version 2.1");
            Console.WriteLine(Indent(4) + "Version 2.2");
        public static string Indent(int count)
        {
            return "".PadLeft(count);
        }

Output is like below.

Versions
  Version 1
    Version 1.1
    Version 1.2
      Version 1.2.1
  Version 2
    Version 2.1
    Version 2.2
25 Jun

C# Update A GUI Element From Another Thread

This examples shows how to update a user interface element from another thread. To do this, we must update to the user interface in user interface thread. Invoke method in Control class implements this task.

Sample Usage:

            //Usage 1
            txtSampleTextBox.Invoke(new Action(() => txtSampleTextBox.Text = "Sample Text"));

            //Usage 2
            txtSampleTextBox.Invoke((MethodInvoker)(() => txtSampleTextBox.Text = "Sample Text"));
25 Jun

Murphy’s Laws About Programming

  1. If a program is useful, it will have to be changed.
  2. If a program is useless, it will have to be documented.
  3. The value of a program is inversely proportional to the weight of its output.
  4. Any given program will expand to fill all the available memory.
  5. Program complexity grows until it exceeds the capability of the programmer who must maintain it.
  6. Any given program, when running, is obsolete.
  7. Any given program costs more and takes longer each time it is run.
  8. murphys law about programming

  9. Every non- trivial program has at least one bug
    • Corollary 1 – A sufficient condition for program triviality is that it have no bugs.
    • Corollary 2 – At least one bug will be observed after the author leaves the organization.
  10. Bugs will appear in one part of a working program when another ‘unrelated’ part is modified.
  11. The subtlest bugs cause the greatest damage and problems.
    • Corollary – A subtle bug will modify storage thereby masquerading as some other problem.
  12. A ‘debugged’ program that crashes will wipe out source files on storage devices when there is the least available backup.
  13. A hardware failure will cause system software to crash, and the customer engineer will blame the programmer.
  14. A system software crash will cause hardware to act strangely and the programmers will blame the customer engineer.
  15. Undetectable errors are infinite in variety, in contrast to detectable errors, which by definition are limited.
  16. Adding manpower to a late software project makes it later.
  17. Make it possible for programmers to write programs in English, and you will find that programmers can not write in English.