Prime Numbers

Lardmeister 0 Tallied Votes 450 Views Share

Here I made a GUI template with a Form, Button and Listbox using MS VCS 2003. MS VCS 2003 still produces a single file usable as a templet, whereas MS VCS 2005 smears files all over the map. Must be some marketing persons idea of market dominance.

I took the source file, removed all the usual gibberish that detracts my students from reading the code, and saved it as a templet. Now I can use the templet with a clever program like the SnippetCompiler for all sorts of small study programs, like this one, calculating prime numbers and displaying them in a convenient scrolling listbox.

// a program to calculate primes between 1 and 1000
// this is a Windows GUI program with a button and a listbox
// compiled with C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe
// also works well with SnippetCompiler.exe 
// from: http://www.sliver.com/dotnet/SnippetCompiler/
// (this program uses csc.exe with the best compiler options)

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

namespace WindowsApplication6
{
  public class Form1 : System.Windows.Forms.Form
  {
    private System.Windows.Forms.ListBox listBox1;
    private System.Windows.Forms.Button button1;
    private System.ComponentModel.Container components = null;

    public Form1()
    {
      InitializeComponent();
    }

    protected override void Dispose( bool disposing )
    {
      if( disposing )
      {
        if (components != null) 
        {
          components.Dispose();
        }
      }
      base.Dispose( disposing );
    }

    private void InitializeComponent()
    {
      this.listBox1 = new System.Windows.Forms.ListBox();
      this.button1 = new System.Windows.Forms.Button();
      this.SuspendLayout();
      // 
      // listBox1
      // 
      this.listBox1.BackColor = System.Drawing.Color.LemonChiffon;
      this.listBox1.ItemHeight = 16;
      this.listBox1.Location = new System.Drawing.Point(8, 8);
      this.listBox1.Name = "listBox1";
      this.listBox1.ScrollAlwaysVisible = false;
      this.listBox1.Size = new System.Drawing.Size(120, 244);
      this.listBox1.TabIndex = 0;
      // 
      // button1
      // 
      this.button1.BackColor = System.Drawing.Color.Khaki;
      this.button1.Location = new System.Drawing.Point(144, 8);
      this.button1.Name = "button1";
      this.button1.Size = new System.Drawing.Size(136, 23);
      this.button1.TabIndex = 1;
      this.button1.Text = "Calculate Primes";
      this.button1.Click += new System.EventHandler(this.button1_Click);
      // 
      // Form1
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
      this.BackColor = System.Drawing.Color.Brown;
      this.ClientSize = new System.Drawing.Size(292, 260);
      this.Controls.Add(this.button1);
      this.Controls.Add(this.listBox1);
      this.Name = "Form1";
      this.Text = "Primes 1..1000";
      this.ResumeLayout(false);
    }

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

    private void button1_Click(object sender, System.EventArgs e)
    {
      int limit = 1000;
      int num = 2;
      int div;
    
      // first prime is 2 (1 is not a prime)
      listBox1.Items.Add(num.ToString());
      // only odd numbers need to be checked
      for (num = 3; num <= limit; num += 2) 
      {
        // prime test loop
        for (div = 3; num % div != 0; div += 2)
          ;
        // prime is divisible only by itself
        if (div == num)
          // add prime to listbox
          listBox1.Items.Add(num.ToString());
      }
    }
  }
}
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Lardmeister,
very nice advice. I still had VC# 2003 on one of my old machines. You are right, it makes great GUI templates. I will be converting my Python mortgage calculator programs from a boring Python console program to a nice C# GUI program and post it in the C# snippets.

BTW, the small C# Snippet Compiler is neat too, actually pretty powerful. Thanks for all the info!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.