import clr
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")

from System import *
from System.Windows.Forms import *
from System.Drawing import *
from System.Threading import *

# Define a class that extends System.Windows.Forms.Form
class DemoForm( Form ):

	# The constructor for the class
	def __init__( self ):
		self.Text = "IPy Demo "

		# prevent window from being resized
		self.FormBorderStyle = FormBorderStyle.FixedSingle
		self.MaximizeBox = False

		# create a button and add it to the form
		self.btn = Button()
		self.btn.Text = "Color"
		self.btn.Click += self.OnColorButtonClick
		self.btn.Location = Point(30, 30)
		self.Controls.Add(self.btn)

		# create another button
		self.btn2 = Button()
		self.btn2.Text = "Timer"
		self.btn2.Click += self.OnTimerButtonClick
		self.btn2.Location = Point(30, 80)
		self.Controls.Add(self.btn2)

		# silly for loop example
		for i in range(0, 10):
			self.Text += i.ToString()

	# Note that to define an instance method of a class, need
	#  "self" as first parameter
	def OnColorButtonClick( self, sender, e ):
		rand = Random()
		
		# Python allows multiple variables to be assigned at once
		r, g, b = rand.Next(300), rand.Next(256), rand.Next(256)

		# Notice that rand.Next is purposely called with a value too
		#   large (300) so we can demonstrate how to handle exceptions
		try:
			self.BackColor = Color.FromArgb(r, g, b)
		except ArgumentException:
			self.BackColor = Color.White

	def OnTimerButtonClick( self, sender, e):

		# We don't want to block until self.Timer() finishes, so
		#   we start it in a separate thread
		ts = ThreadStart(self.Timer)
		t = Thread(ts)
		t.Start()

	def Timer( self ):
		Thread.Sleep(3000)
		MessageBox.Show("timer finsished!")


# Create an instance of our form and run!
Application.Run(DemoForm())
