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:
  1. “text1” + “\t” + “textn”  

For instance:
  1. 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:
  1. Trace.WriteLine("Adding" +”\u0009”+ “Space”)  
2. Typing a space or “ “ between the concatenated strings
Syntax:
  1. “text1” + “ ” + “textn”  

For instance:
  1. Trace.WriteLine("Adding" +” ”+ “Space”)  
You can also use the unicode equivalent of space which is \u0032. For example:
  1. Trace.WriteLine("Adding" +”\u0032”+ “Space”)  
3. Using the Convert.ToChar() string manipulation function
Convert.ToChar converts a specified key code to character.
The key code for tab is 9 while Spacebar has 32.

Syntax:
  1. Convert.ToChar(key code)  
Examples:
  1. //backspace key  
  2. Trace.WriteLine(Convert.ToString(Convert.ToChar(8)))  
  3. //enter key  
  4. Trace.WriteLine(Convert.ToString(Convert.ToChar(13)))  
  5. //adds space to a concatenated string  
  6. 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 “:

  1. // Learn more about F# at http://fsharp.net  
  2. //specifies the namespace memory location of the classes that  
  3. //will be used in our application  
  4. open System  
  5. open System.Diagnostics  
  6. open System.Windows.Forms  
  7. open System.Drawing  
  8. //creates our form  
  9. let myform= new Form(Text="Adding Space")  
  10. //creates our controls    
  11. let label1=new Label(Text="Firstname:",Location=new System.Drawing.Point(0, 10),AutoSize=true)  
  12. let label2=new Label(Text="Lastname:",Location=new System.Drawing.Point(0, 50),AutoSize=true)  
  13. let fnametextbox=new TextBox(Location=new System.Drawing.Point(100,10),BorderStyle=BorderStyle.FixedSingle)  
  14. let lnametextbox=new TextBox(Location=new System.Drawing.Point(100,50),BorderStyle=BorderStyle.FixedSingle)  
  15. let okbutton=new Button(Text="Ok", Location=new System.Drawing.Point(120, 170))  
  16. let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 170))  
  17. myform.Controls.Add(label1)  
  18. myform.Controls.Add(label2  
  19. myform.Controls.Add(fnametextbox)  
  20. myform.Controls.Add(lnametextbox)  
  21. myform.Controls.Add(okbutton)  
  22. myform.Controls.Add(exitbutton)  
  23. myform.Click.Add(fun space->  
  24. //display our text in the ouput window  
  25. Trace.WriteLine("Adding Spaces")  
  26. Trace.WriteLine(label1.Text + "\u0009" + fnametextbox.Text)  
  27. Trace.WriteLine(label2.Text + "\u0009" + lnametextbox.Text))  
  28.   
  29. //execute our application  
  30. myform.Show()  
  31. Application.Run(myform)  

Gradient and Textured Form in Visual F#

To create a gradient form in Visual F#, simply use a gradient brush and use that brush to draw a rectangle that will cover the whole form area. To create a LinearGradientBrush, use the following syntax:
  1. let newbrush=new LinearGradientBrush(brushpoint,color1,color2,strokeangle)  
For instance:
  1. let newbrush=new LinearGradientBrush(rect,Color.Green,Color.YellowGreen,45.0F)  
Follow these steps to create a gradient form using the LinearGradientBrush drawing method:

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. // Learn more about F# at http://fsharp.net  
  2. //specifies the namespace memory location of the classes that  
  3. //will be used in our application  
  4. open System  
  5. open System.Windows.Forms  
  6. open System.Drawing  
  7. open System.Drawing.Drawing2D  
  8. //creates our form  
  9. let myform= new Form(Text="Add Gradient Background")  
  10. //create a rectangle  
  11. //change its upper left x and y coordinates to 0,0  
  12. //sets it width and height to the width and height of the form  
  13. let rect=new Rectangle(0,0,myform.Width,myform.Height)  
  14. //creates a gradient brush  
  15. let newbrush=new LinearGradientBrush(rect,Color.Green,Color.YellowGreen,45.0F)  
  16. //paints our form with a solid and filled rectangle  
  17. myform.Paint.Add(fun fillrect->fillrect.Graphics.FillRectangle(newbrush,rect))   
  18. //executes our application  
  19. myform.Show()  
  20. Application.Run(myform)  

This will display the following output:



To create a textured form, use a TextureBrush to draw a rectangle that will cover the whole form area then display that rectangle on your form.

For instance:
  1. // Learn more about F# at http://fsharp.net  
  2. //specifies the namespace memory location of the classes that  
  3. //will be used in our application  
  4. open System  
  5. open System.Windows.Forms  
  6. open System.Drawing  
  7. open System.Drawing.Drawing2D  
  8.   
  9. //creates our form  
  10. let myform= new Form(Text="Add Textured Background")  
  11. //draws a rectangle with the same width and height as our form  
  12. let drawrect=new Rectangle(0,0,myform.Width,myform.Height)  
  13. //creates a texturedbrush using a windows default image  
  14. //tile the image Vertically and Horizontally  
  15. let newbrush=new TextureBrush(new Bitmap("C:\Windows\Zapotec.bmp"),WrapMode=WrapMode.TileFlipXY)  
  16. //applies the brush in drawing our rectangle  
  17. myform.Paint.Add(fun fillrect->fillrect.Graphics.FillRectangle(newbrush, drawrect))   
  18. //executes our application  
  19. myform.Show()  
  20. Application.Run(myform)