Tooltips are typically used to display a descriptive text when the mouse pointer hovers a control. To create a tooltip, use the following syntax:
- let tooltipobjvaribale=new ToolTip()
let tooltipobjvaribale=new ToolTip()
For example:
let tip=new ToolTip()
One of the essential methods of the ToolTip object is the SetToolTip method which allows user to connect the tooltip to a specified control. SetToolTip has the following syntax:
- tooltipobjvariable.SetToolTip(object variable of the control where you wanted to display the tooltip,”tooltiptext”)
tooltipobjvariable.SetToolTip(object variable of the control where you wanted to display the tooltip,”tooltiptext”)
Example:
- tip.SetToolTip(addbutton,"Click here to add”)
tip.SetToolTip(addbutton,"Click here to add”)
The following steps demonstrates a simple example in using ToolTips.
The following example demonstrate a simple application using WebBrowser control:
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.
4. Enter the following code after the line “// Learn more about F# at http://fsharp.net “:
-
- open System
-
- open System.Drawing
-
- open System.Windows.Forms
-
- let myfont=new Font("Arial",8.0f,FontStyle.Bold, GraphicsUnit.Point)
- let tipform=new Form(Text="Add Tooltip",StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)
-
- let desclabel=new Label(Text="Hover your mouse on the button...",Width=400,AutoSize=false)
-
- let exitbutton=new Button(Text="Exit",Location=new System.Drawing.Point(200,220))
-
-
-
- let tip=new ToolTip(ToolTipTitle="Use ToolTip",IsBalloon=true,ToolTipIcon=ToolTipIcon.Warning)
-
- tipform.Controls.Add(desclabel)
- desclabel.Font<-myfont
- desclabel.ForeColor<-Color.Red
- tipform.Controls.Add(exitbutton)
-
- exitbutton.MouseHover.Add(fun tipmsg->
-
- tip.SetToolTip(exitbutton,"Click here to quit"))
-
- exitbutton.Click.Add(fun quit->tipform.Close())
- tipform.Show()
-
- Application.Run(tipform)
//uses the F# standard library
open System
//use the drawing classes
open System.Drawing
//specifies the location of the form class
open System.Windows.Forms
//make a form add set its caption to “Add Tooltip”
let myfont=new Font("Arial",8.0f,FontStyle.Bold, GraphicsUnit.Point)
let tipform=new Form(Text="Add Tooltip",StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)
//create a label
let desclabel=new Label(Text="Hover your mouse on the button...",Width=400,AutoSize=false)
//make a button and assign an “Exit” to it
let exitbutton=new Button(Text="Exit",Location=new System.Drawing.Point(200,220))
//create a tooltip and set its Title to “Use ToolTip”
//enable the balloon form
//add a warning icon. Other icon values are warning and info
let tip=new ToolTip(ToolTipTitle="Use ToolTip",IsBalloon=true,ToolTipIcon=ToolTipIcon.Warning)
//add the controls to our form
tipform.Controls.Add(desclabel)
desclabel.Font<-myfont
desclabel.ForeColor<-Color.Red
tipform.Controls.Add(exitbutton)
//when the mouse hovers the exit button
exitbutton.MouseHover.Add(fun tipmsg->
//display our tip
tip.SetToolTip(exitbutton,"Click here to quit"))
//quits the form when the exit button is clicked
exitbutton.Click.Add(fun quit->tipform.Close())
tipform.Show()
//executes our application
Application.Run(tipform)
5. Click the run icon to execute your application. You should now see an output similar to the following screen shot: