Windows Form Navigation in F#

Introduction

In this article you'll see how to create a Windows Forms form at run time and how to navigate from one Windows Forms form to form. I have used the Show() and Hide() methods to show how to use these methods in the application.

Show Method

The Show method is a very essential method in Windows Forms. When we call the Show method it shows the form modeless and returns true to the application. The Show method is an overloaded function of the Form class. After calling the Show method the visible property of the Form is true until the Hide method is called in the application.

Hide Method

This is the Form hiding method that hides the form when we navigate from one form to another form. The Hide method hides the selected form in the application until we do not show the form. When we call the Hide method the window is not closed, it just hides the form. The Hide Method is also an overloaded function of the Form class.

Now let's use the following procedure.

Step 1:

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

create-app.jpg

Step 2:

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

selec-add-ref.jpg

Right-click on References:

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 for the F# application:

//use the F# standard library 

open System 

//use the drawing class 

open System.Drawing  

//Specifies the memory location of the form class 

open System.Windows.Forms  

//create a font object 

let ffont=new Font("Times New Roman",12.0f,FontStyle.Bold, GraphicsUnit.Point) 

//creates a form named "firstform" 

let firstform=new Form(Text="Display First Form",StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font,ClientSize=new System.Drawing.Size(350,350),FormBorderStyle=FormBorderStyle.FixedSingle) 

//creates a label,sets its caption to "This is the first form" 

let fformlabel=new Label(Text="This is first form with Basic Information",AutoSize=true)

let fformlblfname=new Label(Text="Pankaj Lohani",Location=new System.Drawing.Point(20,50))

let fformlbllname=new Label(Text="MCN-SOLUTIONS",Location=new System.Drawing.Point(20,80))

let fformlblstate=new Label(Text="Noida",Location=new System.Drawing.Point(20,110))

let fformlblqul=new Label(Text="Qualification details click on second form",Location=new System.Drawing.Point(0,150),AutoSize=true)

//make a button,sets its caption to Second Form 

let nxtbutton=new Button(Text="Second Form", Location=new System.Drawing.Point(100, 200),AutoSize=true

//creates another button, set its text to exit 

let fexitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 200),AutoSize=true

//adds the controls we made to the first form 

firstform.Controls.Add(fformlabel) 

firstform.Controls.Add(fformlblfname) 

firstform.Controls.Add(fformlbllname) 

firstform.Controls.Add(fformlblstate) 

firstform.Controls.Add(fformlblqul)

//applies font format to our label 

fformlblqul.Font<-ffont

fformlabel.Font<-ffont 

firstform.Controls.Add(nxtbutton) 

firstform.Controls.Add(fexitbutton) 

//when the "Second Form" button of the first form is clicked 

nxtbutton.Click.Add(fun nxtform-> 

//hides the first form 

firstform.Hide() 

//create a new form named secondform 

let secondform=new Form(Text="Second Form", StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font,ClientSize=new System.Drawing.Size(350,350),FormBorderStyle=FormBorderStyle.FixedSingle) 

//create a label and sets its caption to "This is the second form" 

let sformlabel=new Label(Text="This is the second form with qualification Details",AutoSize=true

let fformlblhigh=new Label(Text="10th",Location=new System.Drawing.Point(20,50))

let fformlbllinter=new Label(Text="12th",Location=new System.Drawing.Point(20,80))

let fformlblgrd=new Label(Text="B.C.A",Location=new System.Drawing.Point(20,110))

let fformlblpgrd=new Label(Text="M.C.A",Location=new System.Drawing.Point(20,140))

let sformlblinfo=new Label(Text="Basic Information detalis click on First Form",Location=new System.Drawing.Point(0,170),AutoSize=true)

//creates a button and sets its text to "First Form" 

let prevbutton=new Button(Text="First Form", Location=new System.Drawing.Point(150, 200)) 

//adds a button, set its text to "Exit" 

let sexitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(250, 200)) 

//show the second form 

secondform.Show() 

//add the controls we made on the second form 

secondform.Controls.Add(sformlabel) 

secondform.Controls.Add(fformlblhigh)

secondform.Controls.Add(fformlbllinter)

secondform.Controls.Add(fformlblgrd)

secondform.Controls.Add(fformlblpgrd)

secondform.Controls.Add(sformlblinfo)

//applies font format to our label 

sformlabel.Font<-ffont 

sformlblinfo.Font<-ffont

secondform.Controls.Add(prevbutton) 

secondform.Controls.Add(sexitbutton) 

//when the "First Form" button of the second form is clicked 

prevbutton.Click.Add(fun prevform-> 

//show the first form 

firstform.Show() 

//hide the second form 

secondform.Hide()) 

//when the exit button of the second form is clicked 

sexitbutton.Click.Add(fun quit-> 

//close the first form 

firstform.Close() 

//close the second form 

secondform.Close())) 

//when the exit button of the first form is clicked 

//close the first form 

fexitbutton.Click.Add(fun quit->firstform.Close())                      

firstform.Show() 

//executes our application 

Application.Run(firstform) 

Step 5:

Debug the application by pressing F5 and the following result will be shown in the application:

after-debug.jpg

Step 6:

Now if you want to activate a new form or navigate to another form then just click on the second form.

Second-Form.jpg

Step 7:

Now if you want to return to previous page then just click on the first form.

after-debug.jpg

Navigate to the first form.

Summary

In this article, we discussed how to activate a new window form at run time. First I discussed the Show() method then the Hide() method and how to use these methods in the application. I hope it helps you understand.


Similar Articles