Click here to Skip to main content
15,898,134 members
Home / Discussions / Windows Forms
   

Windows Forms

 
QuestionUnwanted Event firing Pin
francoisdotnet23-May-07 2:06
francoisdotnet23-May-07 2:06 
AnswerRe: Unwanted Event firing Pin
Dave Kreskowiak28-May-07 6:06
mveDave Kreskowiak28-May-07 6:06 
GeneralRe: Unwanted Event firing Pin
francoisdotnet28-May-07 11:12
francoisdotnet28-May-07 11:12 
QuestionScroll a listbox Pin
ScottM122-May-07 20:28
ScottM122-May-07 20:28 
AnswerRe: Scroll a listbox Pin
PandemoniumPasha23-May-07 1:51
PandemoniumPasha23-May-07 1:51 
GeneralRe: Scroll a listbox Pin
ScottM123-May-07 2:24
ScottM123-May-07 2:24 
QuestionWorkerthread in .Net 1.1 Pin
matthias s.22-May-07 8:27
matthias s.22-May-07 8:27 
AnswerRe: Workerthread in .Net 1.1 Pin
ScottM122-May-07 20:20
ScottM122-May-07 20:20 
Here is an application that I made for an interview that calculates prime numbers.

//Purpose:to find prime numbers

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections;
using System.Threading;
using System.IO;

namespace Primes
{
	
	public class MainForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button cmdDoit;
		private System.Windows.Forms.ListBox lbPrimes;
		private System.Windows.Forms.ProgressBar pbPrimes;
		private System.Windows.Forms.Label lblValue;
		Thread t;
		public MainForm()
		{
			
			InitializeComponent();
			
			
		}
		
		[STAThread]
		public static void Main(string[] args)
		{
			Application.Run(new MainForm());
		}
		
		#region Windows Forms Designer generated code
		/// <summary>
		/// This method is required for Windows Forms designer support.
		/// Do not change the method contents inside the source code editor. The Forms designer might
		/// not be able to load this method if it was changed manually.
		/// </summary>
		private void InitializeComponent() {
			this.lblValue = new System.Windows.Forms.Label();
			this.pbPrimes = new System.Windows.Forms.ProgressBar();
			this.lbPrimes = new System.Windows.Forms.ListBox();
			this.cmdDoit = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// lblValue
			// 
			this.lblValue.Location = new System.Drawing.Point(8, 64);
			this.lblValue.Name = "lblValue";
			this.lblValue.TabIndex = 3;
			// 
			// pbPrimes
			// 
			this.pbPrimes.Location = new System.Drawing.Point(8, 40);
			this.pbPrimes.Name = "pbPrimes";
			this.pbPrimes.TabIndex = 2;
			// 
			// lbPrimes
			// 
			this.lbPrimes.Location = new System.Drawing.Point(8, 96);
			this.lbPrimes.Name = "lbPrimes";
			this.lbPrimes.Size = new System.Drawing.Size(136, 290);
			this.lbPrimes.TabIndex = 0;
			// 
			// cmdDoit
			// 
			this.cmdDoit.Location = new System.Drawing.Point(8, 8);
			this.cmdDoit.Name = "cmdDoit";
			this.cmdDoit.TabIndex = 1;
			this.cmdDoit.Text = "Calculate";
			this.cmdDoit.Click += new System.EventHandler(this.CmdDoitClick);
			// 
			// MainForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(152, 398);
			this.Controls.Add(this.lblValue);
			this.Controls.Add(this.pbPrimes);
			this.Controls.Add(this.cmdDoit);
			this.Controls.Add(this.lbPrimes);
			this.Name = "MainForm";
			this.Text = "Primes";
			this.Closing += new System.ComponentModel.CancelEventHandler(this.MainFormClosing);
			this.Load += new System.EventHandler(this.MainFormLoad);
			this.ResumeLayout(false);
		}
		#endregion
		void MainFormLoad(object sender, System.EventArgs e)
		{
			
		}
		
		void CmdDoitClick(object sender, System.EventArgs e)
		{
            Control.CheckForIllegalCrossThreadCalls = false; //so that I can access controls from the thread(Not necessary in .net 1.1)
			t = new Thread(new ThreadStart(Calc));
			t.Start();
            //you can use t.Suspend() and t.Resume() to pause and continue
		}
		
		void Calc() {
			int count = 0;
			int max = 1000000; //will find primes up to this number
            this.pbPrimes.Maximum = max;
            this.pbPrimes.Value = 0;

			ArrayList al = new ArrayList();
			for (int x = 1; x <= max; x++) {
				bool prime = true;
				for (int y = 2; y < (x/2)+1; y++) { //to check if a number is prime
					if ((x % y) == 0) { //will find any other multiples of x
						prime = false;
					}
				}
				if (prime) {
					al.Add(x);
					count++;
				}
                pbPrimes.Value++;
				lblValue.Text = pbPrimes.Value.ToString();
                
                
			}
            
			lbPrimes.DataSource = al;
            
            //writing to file
			FileStream fs = new FileStream("Primes.txt",FileMode.Create);
			BufferedStream bs = new BufferedStream(fs);
			StreamWriter sw = new StreamWriter(bs);
			foreach (int num in al) {
				sw.WriteLine(num.ToString());
			}
			sw.Close();
			bs.Close();
			fs.Close();

			MessageBox.Show(count.ToString() + " Primes were found and written to Primes.txt");
		}
		
		void MainFormClosing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			try {
				t.Abort();
			} catch (Exception ex) {
				MessageBox.Show(ex.ToString() + "  Thread could not be stopped");
			}
		}
		
	}
}


There are 10 types of people in the world, those who understand binary and those who dont.

QuestionMDI application help - multiple views Pin
earlgraham22-May-07 7:37
earlgraham22-May-07 7:37 
QuestionButton click event Pin
dodoxor22-May-07 5:49
dodoxor22-May-07 5:49 
AnswerRe: Button click event Pin
Paul Conrad13-Jul-07 13:47
professionalPaul Conrad13-Jul-07 13:47 
AnswerRe: Button click event Pin
dodoxor13-Jul-07 21:33
dodoxor13-Jul-07 21:33 
GeneralRe: Button click event Pin
Paul Conrad14-Jul-07 4:25
professionalPaul Conrad14-Jul-07 4:25 
QuestionClick Once with multiple sites Pin
Garry Pilkington22-May-07 1:01
Garry Pilkington22-May-07 1:01 
QuestionImport a class form Pin
dodoxor21-May-07 22:35
dodoxor21-May-07 22:35 
AnswerRe: Import a class form Pin
sarah_malik22-May-07 1:44
sarah_malik22-May-07 1:44 
QuestionRe: Import a class form Pin
dodoxor22-May-07 2:15
dodoxor22-May-07 2:15 
QuestionASP.NET button transparent in Safari [modified] Pin
hifiger200421-May-07 3:24
hifiger200421-May-07 3:24 
QuestionUsing report viewer control in Windows forms with CLR suppport Pin
Shraddha Gautam19-May-07 19:57
Shraddha Gautam19-May-07 19:57 
QuestionHow to kill exe? Pin
NanaAM18-May-07 22:28
NanaAM18-May-07 22:28 
AnswerRe: How to kill exe? Pin
Giorgi Dalakishvili18-May-07 23:11
mentorGiorgi Dalakishvili18-May-07 23:11 
QuestionHow to Insert row from Binded Combo Box Pin
P.T.R.K18-May-07 20:31
P.T.R.K18-May-07 20:31 
AnswerRe: How to Insert row from Binded Combo Box Pin
PandemoniumPasha19-May-07 19:16
PandemoniumPasha19-May-07 19:16 
AnswerRe: How to Insert row from Binded Combo Box Pin
Ch_Shahzad iqbal21-May-07 23:19
Ch_Shahzad iqbal21-May-07 23:19 
QuestionHow to scroll two listbox together in c# ? Pin
nav_smec18-May-07 1:07
nav_smec18-May-07 1:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.