// 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)
Subscribe to:
Comments (Atom)