// Learn more about F# at http://fsharp.net
//specifies the memory location of the class files
//that will be needed in our application
open System.Collections.Generic
open System
open System.Windows.Forms
open System.ComponentModel
open System.Drawing
open System.Drawing.Printing
open System.Drawing.Imaging
let imageform = new Form(Text="Print Form",AutoScaleDimensions=new System.Drawing.SizeF(60.0F, 13.0F),ClientSize=new System.Drawing.Size(300, 250),StartPosition=FormStartPosition.CenterScreen)
//creates our control
let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 200))
let loadbutton=new Button(Text="Load", Location=new System.Drawing.Point(120, 200))
let prnbutton=new Button(Text="Print", Location=new System.Drawing.Point(40, 200))
let pic=new PictureBox(SizeMode=PictureBoxSizeMode.StretchImage,Location=new System.Drawing.Point(20, 20),BorderStyle=BorderStyle.FixedSingle,Size=new System.Drawing.Size(100, 100))
let label=new Label(AutoSize=true,Location=new System.Drawing.Point(0, 120))
let dlg=new OpenFileDialog()
let gr=imageform.CreateGraphics()
let prn=new System.Drawing.Printing.PrintDocument()
imageform .Controls.Add(pic)
imageform.Controls.Add(loadbutton)
imageform.Controls.Add(label)
imageform.Controls.Add(prnbutton)
imageform.Controls.Add(exitbutton)
//sends the data to the printer
prnbutton.Click.Add(fun startprint->prn.Print())
loadbutton.Click.Add(fun load->
//filter dialog result
dlg.Filter <- "JPEG Images (*.jpg,*.jpeg)|*.jpg;*.jpeg|Gif Images (*.gif)|*.gif"
//adds title to your dialog box
dlg.Title<-"Select an Image File"
if dlg.ShowDialog()=DialogResult.OK then
//creates a bitmap object
//assigns the image selected by the user as its value
let bmp=new System.Drawing.Bitmap(dlg.FileName)
bmp.RotateFlip(RotateFlipType.RotateNoneFlipNone)
//assigns the loaded image as a picturebox value
pic.Image<-bmp
//displays the image url in our label
label.Text<-"\t\tFilename:" + Convert.ToString(Convert.ToChar(32))+ (dlg.FileName))
//specifies the data to be printed
//in this case our image
prn.PrintPage.Add(fun printdata->gr.DrawImage(pic.Image,10,10))
//close the form
exitbutton.Click.Add(fun quit->imageform.Close())
[]
//executes our application
Application.Run(imageform)
Printing an Image Data
To print an image or text, use a PrintDocument control. To following example shows how to print an image data, using a PrintDocument control.
Drawing Triangles and Circles
To draw a triangle, use the DrawLine or DrawPolygon method. Here’s an example that uses DrawPolygon method:
// Learn more about F# at http://fsharp.net // Learn more about F# at http://fsharp.net //specifies the memory location of the class files //that will be needed in our application open System.Collections.Generic open System open System.Windows.Forms open System.ComponentModel open System.Drawing let graphicform = new Form(Text="Draw Triangle",AutoScaleDimensions=new System.Drawing.SizeF(60.0F, 13.0F),ClientSize=new System.Drawing.Size(300, 250),StartPosition=FormStartPosition.CenterScreen) //creates our control let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(190, 200)) graphicform.Paint.Add(fun draw-> let array=[|new Point(0,150);new Point(150,10);new Point(300,150)|] let pen=new Pen(Color.Blue,Width=12.0f) draw.Graphics.DrawPolygon(pen,array)) graphicform.Controls.Add(exitbutton) //executes our application Application.Run(graphicform)To draw a filled triangle, use the FillPolygon method:
// Learn more about F# at http://fsharp.net // Learn more about F# at http://fsharp.net //specifies the memory location of the class files //that will be needed in our application open System.Collections.Generic open System open System.Windows.Forms open System.ComponentModel open System.Drawing let graphicform = new Form(Text="Draw Triangle",AutoScaleDimensions=new System.Drawing.SizeF(60.0F, 13.0F),ClientSize=new System.Drawing.Size(300, 250),StartPosition=FormStartPosition.CenterScreen) //creates our control let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(190, 200)) graphicform.Paint.Add(fun draw-> let array=[|new Point(0,150);new Point(150,10);new Point(300,150)|] let brush=new SolidBrush(Color.Blue) draw.Graphics.FillPolygon(brush,array)) graphicform.Controls.Add(exitbutton) //executes our application Application.Run(graphicform)To draw a circle, use the DrawEllipse method:
// Learn more about F# at http://fsharp.net // Learn more about F# at http://fsharp.net //specifies the memory location of the class files //that will be needed in our application open System.Collections.Generic open System open System.Windows.Forms open System.ComponentModel open System.Drawing let graphicform = new Form(Text="Draw Circle",AutoScaleDimensions=new System.Drawing.SizeF(60.0F, 13.0F),ClientSize=new System.Drawing.Size(300, 250),StartPosition=FormStartPosition.CenterScreen) //creates our control let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(190, 200)) graphicform.Paint.Add(fun draw-> let pen=new Pen(Color.Blue,Width=12.0f) draw.Graphics.DrawEllipse(pen,0.0f,0.0f,100.0f,100.0f)) graphicform.Controls.Add(exitbutton) //executes our application Application.Run(graphicform)To draw a solid circle, use the FillEllipse method:
// Learn more about F# at http://fsharp.net // Learn more about F# at http://fsharp.net //specifies the memory location of the class files //that will be needed in our application open System.Collections.Generic open System open System.Windows.Forms open System.ComponentModel open System.Drawing let graphicform = new Form(Text="Draw Circle",AutoScaleDimensions=new System.Drawing.SizeF(60.0F, 13.0F),ClientSize=new System.Drawing.Size(300, 250),StartPosition=FormStartPosition.CenterScreen) //creates our control let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(190, 200)) graphicform.Paint.Add(fun draw-> let brush=new SolidBrush(Color.Blue) draw.Graphics.FillEllipse(brush,0.0f,0.0f,100.0f,100.0f)) graphicform.Controls.Add(exitbutton) //executes our application Application.Run(graphicform)
Display the Date or Time from a DateTime Object
There are several ways on how to retrieve the date or time part from a datetime object, here are the few methods that I discover through research and experimentation:
1. Using Substring
Display the time part using Substring:
Display the time part using the substring function:
2. Using Remove
Display the time part using Remove:
Display the date part using the Remove function:
1. Using Substring
Display the time part using Substring:
// Learn more about F# at http://fsharp.net
//use the F# library
open System
//use this to enable the intellisense. Very helpful in coding your application
open System.Drawing
//specify the location of the Form classes
open System.Windows.Forms
let ffont=new Font("Verdana", 14.5F,FontStyle.Regular, GraphicsUnit.Point)
//creates a form
let timerform=new Form(Text="Display Time Part",StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)
let timelabel=new Label(Location=new System.Drawing.Point(20,40),BorderStyle=BorderStyle.FixedSingle,AutoSize=true)
let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 220),AutoSize=true)
//create a timer object and set its interval to 1 second
//by default timer are disabled so you'll need to enable it
let timer1=new Timer(Interval=1000,Enabled=true)
timelabel.Font<-ffont
//change it every 1 min second
timer1.Tick.Add(fun time->
//assigns the current date and time to a variable
let datetime=Convert.ToString(System.DateTime.Now)
//retrieves the value of the datetime variable
//starting from the 11th character
let timepart=datetime.Substring(10)
//display the time
timelabel.Text<-timepart)
//adds the exit button to our form
timerform.Controls.Add(timelabel)
timerform.Controls.Add(exitbutton)
//when the exit button is clicked
exitbutton.Click.Add(fun quit->
//stops the time
timer1.Stop()
//close the form
timerform.Close())
//show our form
timerform.Show()
//execute our application
Application.Run(timerform)
Display the time part using the substring function:
// Learn more about F# at http://fsharp.net
//use the F# library
open System
//use this to enable the intellisense. Very helpful in coding your application
open System.Drawing
//specify the location of the Form classes
open System.Windows.Forms
let ffont=new Font("Verdana", 14.5F,FontStyle.Regular, GraphicsUnit.Point)
//creates a form
let dateform=new Form(Text="Display Date Part",StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)
//use the random function to generate random numbers
let datelabel=new Label(Location=new System.Drawing.Point(20,40),BorderStyle=BorderStyle.FixedSingle,AutoSize=true)
let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 220),AutoSize=true)
datelabel.Font<-ffont
dateform.Load.Add(fun time->
//assigns the current date and time to the datetime variable
let datetime=Convert.ToString(System.DateTime.Now)
//retrieves the text from the datetime variable staring from the first
//character to the 11th character
let datepart=datetime.Substring(0,10)
//display the current time
datelabel.Text<-datepart)
//adds the exit button to our form
dateform.Controls.Add(datelabel)
dateform.Controls.Add(exitbutton)
//when the exit button is clicked
exitbutton.Click.Add(fun quit->dateform.Close())
//execute our application
Application.Run(dateform)
2. Using Remove
Display the time part using Remove:
// Learn more about F# at http://fsharp.net
//use the F# library
open System
//use this to enable the intellisense. Very helpful in coding your application
open System.Drawing
//specify the location of the Form classes
open System.Windows.Forms
let ffont=new Font("Verdana", 14.5F,FontStyle.Regular, GraphicsUnit.Point)
//creates a form
let timerform=new Form(Text="Display Time Part",StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)
let timelabel=new Label(Location=new System.Drawing.Point(20,40),BorderStyle=BorderStyle.FixedSingle,AutoSize=true)
let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 220),AutoSize=true)
//create a timer object and set its interval to 1 second
//by default timer are disabled so you'll need to enable it
let timer1=new Timer(Interval=1000,Enabled=true)
timelabel.Font<-ffont
//change it every 1 min second
timer1.Tick.Add(fun time->
//assigns the current date and time to the datetime variable
let datetime=Convert.ToString(System.DateTime.Now)
//removes the first 11 characters from the datetime variable
let timepart=datetime.Remove(0,10)
//display the current time
timelabel.Text<-timepart)
//adds the exit button to our form
timerform.Controls.Add(timelabel)
timerform.Controls.Add(exitbutton)
//when the exit button is clicked
exitbutton.Click.Add(fun quit->
//stops the time
timer1.Stop()
//close the form
timerform.Close())
//show our form
timerform.Show()
//execute our application
Application.Run(timerform)
Display the date part using the Remove function:
// Learn more about F# at http://fsharp.net
//use the F# library
open System
//use this to enable the intellisense. Very helpful in coding your application
open System.Drawing
//specify the location of the Form classes
open System.Windows.Forms
let ffont=new Font("Verdana", 14.5F,FontStyle.Regular, GraphicsUnit.Point)
//creates a form
let dateform=new Form(Text="Display Date Part",StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)
//use the random function to generate random numbers
let datelabel=new Label(Location=new System.Drawing.Point(20,40),BorderStyle=BorderStyle.FixedSingle,AutoSize=true)
let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 220),AutoSize=true)
datelabel.Font<-ffont
dateform.Load.Add(fun time->
//assigns the current date and time to the datetime variable
let datetime=Convert.ToString(System.DateTime.Now)
//removes the text from the datetime variable staring from the 11 character
let datepart=datetime.Remove(10)
//display the current time
datelabel.Text<-datepart)
//adds the exit button to our form
dateform.Controls.Add(datelabel)
dateform.Controls.Add(exitbutton)
//when the exit button is clicked
exitbutton.Click.Add(fun quit->dateform.Close())
//execute our application
Application.Run(dateform)
SystemSounds in F#
To play a system sound in Visual F#, use the SystemSounds object. For a simple example on using the SystemSounds object, try the following:
This will generate the following output:
// Learn more about F# at http://fsharp.net
//use the f# standard library
open System
//use media classes
open System.Media
//specify the memory location of the classes used in drawing objects
//required to draw the listbox item text
open System.Drawing
//specify the location of the form class
open System.Windows.Forms
//creates a form and assign a "Play System Sounds" caption to it
let soundform=new Form(Text="Play System Sounds",StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)
//creates a label and set its Text to “Count”
let lbl=new Label(Text="System sounds:", Location=new System.Drawing.Point(20,10),AutoSize=true)
//makes a listbox
let soundlistbox=new ListBox(Sorted=true,Location=new System.Drawing.Point(20,30),FormattingEnabled=true)
//adds an item to the listbox when the form is loaded
soundform.Load.Add(fun items->
//adds the items and ignore the passed index position values
soundlistbox.Items.Add("Asterisk")|>ignore
soundlistbox.Items.Add("Beep")|>ignore
soundlistbox.Items.Add("Exclaimation")|>ignore
soundlistbox.Items.Add("Hand")|>ignore
soundlistbox.Items.Add("Question")|>ignore)
soundlistbox.Click.Add(fun playsound->
if soundlistbox.SelectedIndex=0 then
SystemSounds.Asterisk.Play()
MessageBox.Show("Asterisk", "System Sounds", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)|>ignore
if soundlistbox.SelectedIndex=1 then
SystemSounds.Beep.Play()
MessageBox.Show("Beep", "System Sounds", MessageBoxButtons.OK)|>ignore
if soundlistbox.SelectedIndex=2 then
SystemSounds.Exclamation.Play()
MessageBox.Show("Exclaimation", "System Sounds", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)|>ignore
if soundlistbox.SelectedIndex=3 then
SystemSounds.Hand.Play()
MessageBox.Show("Hand", "System Sounds", MessageBoxButtons.OK, MessageBoxIcon.Hand)|>ignore
if soundlistbox.SelectedIndex=4 then
SystemSounds.Question.Play()
MessageBox.Show("Question", "System Sounds", MessageBoxButtons.OK, MessageBoxIcon.Question)|>ignore)
//displays the label to our form
soundform.Controls.Add(lbl)
//adds the listbox to our form
soundform.Controls.Add(soundlistbox)
soundform.Show()
Application.Run(soundform)
This will generate the following output:
Adding space to a concatenated String
There are several ways to add space to a contanated string in Visual F#:
1.Using the Tab Escape Character
Syntax:
For instance:
This will display “Adding Space” with 5-6 spaces in between in the output window. You can also use the unicode equivalent of tab which is \u0009. For example:
Syntax:
For instance:
Convert.ToChar converts a specified key code to character.
The key code for tab is 9 while Spacebar has 32.
Syntax:
For a simple example of application that adds space to a concatenated string:
1. Click Start>All Programs>Microsoft Visual Studio 2008>Microsoft Visual Studio 2008.
2. Click File>New>Project>Select Visual F# in the project types>Select F# application in the Visual Studio installed templates category.
3. Click the Project menu>Add reference>Click the .Net tab>Locate then double-click System.Windows.Forms. Do step 3 again and this time, select System.Drawing from the .Net tab.
4. Enter the following code after the line “// Learn more about F# at http://fsharp.net “:
1.Using the Tab Escape Character
Syntax:
“text1” + “\t” + “textn”
For instance:
Trace.WriteLine("Adding" +”\t”+ “Space”)
This will display “Adding Space” with 5-6 spaces in between in the output window. You can also use the unicode equivalent of tab which is \u0009. For example:
Trace.WriteLine("Adding" +”\u0009”+ “Space”)
2. Typing a space or “ “ between the concatenated stringsSyntax:
“text1” + “ ” + “textn”
For instance:
Trace.WriteLine("Adding" +” ”+ “Space”)
You can also use the unicode equivalent of space which is \u0032. For example:Trace.WriteLine("Adding" +”\u0032”+ “Space”)
3. Using the Convert.ToChar() string manipulation functionConvert.ToChar converts a specified key code to character.
The key code for tab is 9 while Spacebar has 32.
Syntax:
Convert.ToChar(key code)Examples:
//backspace key Trace.WriteLine(Convert.ToString(Convert.ToChar(8))) //enter key Trace.WriteLine(Convert.ToString(Convert.ToChar(13))) //adds space to a concatenated string Trace.WriteLine(“Adding” + Convert.ToString(Convert.ToChar(32)) + “Space” )
For a simple example of application that adds space to a concatenated string:
1. Click Start>All Programs>Microsoft Visual Studio 2008>Microsoft Visual Studio 2008.
2. Click File>New>Project>Select Visual F# in the project types>Select F# application in the Visual Studio installed templates category.
3. Click the Project menu>Add reference>Click the .Net tab>Locate then double-click System.Windows.Forms. Do step 3 again and this time, select System.Drawing from the .Net tab.
4. Enter the following code after the line “// Learn more about F# at http://fsharp.net “:
// Learn more about F# at http://fsharp.net
//specifies the namespace memory location of the classes that
//will be used in our application
open System
open System.Diagnostics
open System.Windows.Forms
open System.Drawing
//creates our form
let myform= new Form(Text="Adding Space")
//creates our controls
let label1=new Label(Text="Firstname:",Location=new System.Drawing.Point(0, 10),AutoSize=true)
let label2=new Label(Text="Lastname:",Location=new System.Drawing.Point(0, 50),AutoSize=true)
let fnametextbox=new TextBox(Location=new System.Drawing.Point(100,10),BorderStyle=BorderStyle.FixedSingle)
let lnametextbox=new TextBox(Location=new System.Drawing.Point(100,50),BorderStyle=BorderStyle.FixedSingle)
let okbutton=new Button(Text="Ok", Location=new System.Drawing.Point(120, 170))
let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 170))
myform.Controls.Add(label1)
myform.Controls.Add(label2
myform.Controls.Add(fnametextbox)
myform.Controls.Add(lnametextbox)
myform.Controls.Add(okbutton)
myform.Controls.Add(exitbutton)
myform.Click.Add(fun space->
//display our text in the ouput window
Trace.WriteLine("Adding Spaces")
Trace.WriteLine(label1.Text + "\u0009" + fnametextbox.Text)
Trace.WriteLine(label2.Text + "\u0009" + lnametextbox.Text))
//execute our application
myform.Show()
Application.Run(myform)
Subscribe to:
Posts (Atom)