// 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 //creates a font let ffont=new Font("Verdana", 9.75F,FontStyle.Regular, GraphicsUnit.Point) let validateform = new Form(Text="Validate Inputs",AutoScaleDimensions=new System.Drawing.SizeF(60.0F, 13.0F),ClientSize=new System.Drawing.Size(300, 200),StartPosition=FormStartPosition.CenterScreen) //creates our controls let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(190, 170)) let okbutton=new Button(Text="Ok", Location=new System.Drawing.Point(100, 170)) let label1=new Label(Text="Enter a name:",Location=new System.Drawing.Point(0, 10),AutoSize=true) let nametxtbox=new TextBox(Location=new System.Drawing.Point(120,10),BorderStyle=BorderStyle.FixedSingle) validateform.Font<-ffont //adds the controls to our form validateform.Controls.Add(exitbutton) validateform.Controls.Add(okbutton) validateform.Controls.Add(label1) validateform.Controls.Add(nametxtbox) //when the oK button is clicked okbutton.Click.Add(fun ok-> //if the input begins with a letter then if (Char.IsLetter(Convert.ToChar(nametxtbox.Text.Chars(0)))) then //prompt that its a valid name MessageBox.Show("The name you entered is valid ", "Validate Inputs",MessageBoxButtons.OK, MessageBoxIcon.Information)|>ignore //if the input begins with a number then if (Char.IsNumber(Convert.ToChar(nametxtbox.Text.Chars(0)))) then //prompt that its an invalid name MessageBox.Show("You must enter a valid name", "Validate Inputs",MessageBoxButtons.OK, MessageBoxIcon.Information)|>ignore) //when the exit button is clicked exitbutton.Click.Add(fun exit->validateform.Close()) //executes our application Application.Run(validateform)Click the run icon when done entering these codes in Visual F# code editor window. Try entering any number in the name text box and clicking the ok button. You should see an output similar to the following:
Simplest input validation using Char.IsNumber and Char.IsLetter
The following example demonstrates how to validate a user input by using the Char.IsNumber and Char.IsLetter character manipulation functions:
Paint Application in Visual F#
The following example demonstrates how to make a simple Paint-like application in Visual F#:
// 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.Drawing2D let drawingform = new Form(Text="Draw Objects",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 erasebutton=new Button(Text="Erase", Location=new System.Drawing.Point(120, 200)) let colorbutton=new Button(Text="Brush Color", Location=new System.Drawing.Point(40, 200)) drawingform.Controls.Add(exitbutton) drawingform.Controls.Add(erasebutton) drawingform.Controls.Add(colorbutton) //creates a color dialog box let colordlg=new ColorDialog() //creates a colorblend object let mutable color=new ColorBlend() let gr=drawingform.CreateGraphics() gr.SmoothingMode<-SmoothingMode.HighQuality //when the form is loaded, change its color to white drawingform.Load.Add(fun background-> //set the default brush color to indigo color.Colors<-[|Color.Indigo|] drawingform.BackColor<-Color.White) drawingform.MouseMove.Add(fun trail-> //when the mouse button is moved and the left button is clicked if (trail.Button=System.Windows.Forms.MouseButtons.Left)then //draw the object assign the color seleted from the color dialog as a brush color gr.FillRectangle(new SolidBrush(color.Colors.[0]),new Rectangle(trail.X,trail.Y,5,5))) //when the erase button is clicked //erase the object drawn in the form erasebutton.Click.Add(fun erase->gr.Clear(Color.White)) //when the exit button is clicked //quit the form exitbutton.Click.Add(fun quit->drawingform.Close()) //when the brush color button is selected colorbutton.Click.Add(fun colors-> //display the Color Dialog box if colordlg.ShowDialog()=DialogResult.OK then //store the value selected by the user in our colorblend object color.Colors<-[|colordlg.Color|]) //executes our application Application.Run(drawingform)Click the run icon once you are done entering these codes in the code editor window. You should see the following outputs:
Subscribe to:
Posts (Atom)