Using PropertyGrid Control in F#

Introduction

This article explains the PropertyGrid Control and how to use this control in a Windows Forms application and specify the editing setting and change the properties of the control in your application. For this article  I have used Visual Studio 2010.

PropertyGrid Control

The PropertyGrid Control is used to display the prporties for any objet or user control and it retrieves the item properties dynamically. To identify what the PropertyGrid displays set the "PropertyGrid.SelectedObject" to an object instance. Whenever you set the "SelectedObject" the PropertyGrid refreshes the properties of the control shown. You can also call the "PropertyGrid.Refresh" method to refresh the properties. You can easily customize the properties of an object at run-time.

Namespace : System.Windows.Forms.PropertyGrid

Syntax:

let prpgrid=new PropertyGrid()   

SelectedObject Property

The "SelectedObject" property sets a single object into the grid to be browsed. If multiple objects are browsed then this property returns the first object in the list. If there is no object selected then it returns the null reference. When you pass an object to the "SelectedObject" property of the PropertyGrid Control, the PropertyGrid control will display all the properties of your object that has "BrowsableAttribute" set to true.

Syntax:

prpgrid.SelectedObject<-ObjectName

Features of PropertyGrid Control

  •  Multilanguage Support.

  •  Gets/sets any property at run-time.

  •  Image Preview.

  •  Date Calendar.

  •  Numeric interval automatic validation.

  •  Customizable Boolean type (yes/no, true/false).

  • Standard DialogBox, Texttitle.

Now let's use the following procedure.

Step 1:

Open Visual Studio and select "Create New Project" --> "F# Console Application".

CreateApp.jpg

Step 2:

Now go the Solution Explorer on the right side of the application. Right-click the References and select "Add references".

selct-ref.jpg


add-ref.jpg

Step 3:

After selecting "Add References", in the framework template you need to select "System.Windows.Forms" and "System.Drawing" while holding down the Ctrl key and Click on "Ok".

import-namespace.jpg

Step 4:

Write the following code in the F# application.

// Learn more about F# at http://fsharp.net

 

// Learn more about F# at http://fsharp.net  

open System   

open System.Drawing  

open System.Windows.Forms  

let PrpGridform=new Form(Text="PropertyGrid Control")   

let lblname=new Label(Text="Name:",Location=new System.Drawing.Point(0, 10),AutoSize=true)

let lname=new Label(Text="Last Name:",Location=new System.Drawing.Point(0, 40),AutoSize=true)

let lemail=new Label(Text="EmailId:",Location=new System.Drawing.Point(0, 70),AutoSize=true)

let nametxtbox=new TextBox(Location=new System.Drawing.Point(120,10))

let lnametxtbox=new TextBox(Location=new System.Drawing.Point(120,40))

let lemailtxtbox=new TextBox(Location=new System.Drawing.Point(120,70))

let Submitbtn=new Button(Text="Submit", Location=new System.Drawing.Point(30, 100))  

let prpgrid=new PropertyGrid(Size=new System.Drawing.Size(200, 270),Left=250)   

PrpGridform.Controls.Add(lblname) 

PrpGridform.Controls.Add(Submitbtn)

PrpGridform.Controls.Add(lblname)  

PrpGridform.Controls.Add(lname)  

PrpGridform.Controls.Add(lemail)

PrpGridform.Controls.Add(lemailtxtbox)

PrpGridform.Controls.Add(nametxtbox)  

PrpGridform.Controls.Add(lnametxtbox)  

PrpGridform.Controls.Add(prpgrid)      

nametxtbox.Click.Add(fun prptxt->prpgrid.SelectedObject<-nametxtbox)

lnametxtbox.Click.Add(fun prptxt->prpgrid.SelectedObject<-lnametxtbox)

lblname.Click.Add(fun prplbl->prpgrid.SelectedObject<-lblname)

lemail.Click.Add(fun prplbl->prpgrid.SelectedObject<-lemail)

lemailtxtbox.Click.Add(fun prptxt->prpgrid.SelectedObject<-lemailtxtbox)

lname.Click.Add(fun prptxt->prpgrid.SelectedObject<-lname)    

Submitbtn.Click.Add(fun prpbtn->prpgrid.SelectedObject<-Submitbtn)

PrpGridform.Show()  

Application.Run(PrpGridform) 

 

Step 5:

Debug the application by pressing F5 and the result will appear in the application as in the following figure.

after-debug.jpg

Step 6:.

txtproperty.jpg

txtnameprpty.jpg

Step 7:

You can change the properties of the label control shown in the figure below.

lblproperty.jpg

Step 8:

emailprpty.jpg

Step 9:

You can change the properties of the Button.

buttnprpty.jpg

Summary

In this article, we explained how to create and use a PropertyGrid control. First we explained how to create a PropertyGrid Control in a Windows Forms application and then the use of the SelectedObject property.

Up Next
    Ebook Download
    View all
    Learn
    View all