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:
  1. // Learn more about F# at http://fsharp.net  
  2. //specifies the memory location of the class files  
  3. //that will be needed in our application  
  4. open System.Collections.Generic  
  5. open System  
  6. open System.Windows.Forms  
  7. open System.ComponentModel  
  8. open System.Drawing  
  9. //creates a font  
  10. let ffont=new Font("Verdana", 9.75F,FontStyle.Regular, GraphicsUnit.Point)    
  11. 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)  
  12. //creates our controls  
  13. let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(190, 170))    
  14. let okbutton=new Button(Text="Ok", Location=new System.Drawing.Point(100, 170))   
  15. let label1=new Label(Text="Enter a name:",Location=new System.Drawing.Point(0, 10),AutoSize=true)  
  16. let nametxtbox=new TextBox(Location=new System.Drawing.Point(120,10),BorderStyle=BorderStyle.FixedSingle)  
  17. validateform.Font<-ffont  
  18. //adds the controls to our form  
  19. validateform.Controls.Add(exitbutton)  
  20. validateform.Controls.Add(okbutton)  
  21. validateform.Controls.Add(label1)  
  22. validateform.Controls.Add(nametxtbox)  
  23. //when the oK button is clicked  
  24. okbutton.Click.Add(fun ok->  
  25. //if the input begins with a letter then  
  26. if (Char.IsLetter(Convert.ToChar(nametxtbox.Text.Chars(0)))) then  
  27. //prompt that its a valid name  
  28. MessageBox.Show("The name you entered is valid ""Validate Inputs",MessageBoxButtons.OK, MessageBoxIcon.Information)|>ignore  
  29. //if the input begins with a number then                           
  30. if (Char.IsNumber(Convert.ToChar(nametxtbox.Text.Chars(0)))) then  
  31. //prompt that its an invalid name  
  32. MessageBox.Show("You must enter a valid name""Validate Inputs",MessageBoxButtons.OK, MessageBoxIcon.Information)|>ignore)  
  33. //when the exit button is clicked  
  34. exitbutton.Click.Add(fun exit->validateform.Close())  
  35. //executes our application  
  36. 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:

Paint Application in Visual F#

The following example demonstrates how to make a simple Paint-like application in Visual F#:

  1. // Learn more about F# at http://fsharp.net  
  2. //specifies the memory location of the class files  
  3. //that will be needed in our application  
  4. open System.Collections.Generic  
  5. open System  
  6. open System.Windows.Forms  
  7. open System.ComponentModel  
  8. open System.Drawing  
  9. open System.Drawing.Drawing2D  
  10. 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)  
  11. //creates our control  
  12. let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 200))  
  13. let erasebutton=new Button(Text="Erase", Location=new System.Drawing.Point(120, 200))  
  14. let colorbutton=new Button(Text="Brush Color", Location=new System.Drawing.Point(40, 200))  
  15. drawingform.Controls.Add(exitbutton)  
  16. drawingform.Controls.Add(erasebutton)  
  17. drawingform.Controls.Add(colorbutton)  
  18. //creates a color dialog box  
  19. let colordlg=new ColorDialog()  
  20. //creates a colorblend object  
  21. let mutable color=new ColorBlend()  
  22.   
  23. let gr=drawingform.CreateGraphics()  
  24. gr.SmoothingMode<-SmoothingMode.HighQuality  
  25. //when the form is loaded, change its color to white  
  26. drawingform.Load.Add(fun background->  
  27. //set the default brush color to indigo  
  28. color.Colors<-[|Color.Indigo|]  
  29.                         drawingform.BackColor<-Color.White)  
  30.   
  31. drawingform.MouseMove.Add(fun trail->  
  32. //when the mouse button is moved and the left button is clicked  
  33.   
  34. if (trail.Button=System.Windows.Forms.MouseButtons.Left)then  
  35. //draw the object assign the color seleted from the color dialog as a brush color  
  36. gr.FillRectangle(new SolidBrush(color.Colors.[0]),new Rectangle(trail.X,trail.Y,5,5)))   
  37. //when the erase button is clicked  
  38. //erase the object drawn in the form  
  39. erasebutton.Click.Add(fun erase->gr.Clear(Color.White))   
  40. //when the exit button is clicked  
  41. //quit the form                                                                                                                
  42. exitbutton.Click.Add(fun quit->drawingform.Close())  
  43. //when the brush color button is selected                                                            
  44. colorbutton.Click.Add(fun colors->  
  45. //display the Color Dialog box  
  46. if colordlg.ShowDialog()=DialogResult.OK then  
  47. //store the value selected by the user in our colorblend object  
  48. color.Colors<-[|colordlg.Color|])  
  49. //executes our application  
  50. 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: