Sunday, May 8, 2016

Program 5: Windows form application : Navigation from one form to another.

1.         1.      Open Visual studio 2013.
2.     On the menu bar, choose File, New, and Project. Choose Programming language       as Visual C#. In the templates list, choose the Windows Forms Application icon. 
3.      Name the project as PageNavigation and specify the address of your folder.
4.      In the main window design the form as shown below and set the properties as 
        given in the table.




Now design another form as shown below. Our aim is to redirect from first form to second form as soon as we click on the button in the first form.



Now write the code given below in the button_click event....

 In the btnMultiply_Click method write the following code.

private void btnMultiply_Click(object sender, EventArgs e)
        {
            float a, b, c;
            a = float.Parse(txtNumber1.Text);
            b = float.Parse(txtNumber2.Text);
            c = a * b;
            Result f2 = new Result(c);
            f2.Show();
            this.Hide();
        }
Explanation: a & b are variables to hold the content of the textboxes in the first form. C stores the multiplication result. Now the result must be shown in the second form hence we have to create an object of the second form.(i.e. Result class). f2 is an object of Result class which calls the show method as a result of which the second form appears. The result c is passed as an argument to the method. The value of C must be displayed in the textbox present in the second form during page loading. Hence appropriate code is required for the page-load event.

similarly,

 In the btnMainForm_Click method write the following code.

private void btnMainForm_Click(object sender, EventArgs e)
        {
            this.Hide();
            Multiply f1 = new Multiply();
            f1.Show();
        }

This indicates the currently visible form or object. This.Hide() will hide the current object. f1 is an object of first form which calls the show function as a result of which the first form will be displayed.


Enjoy programming..

No comments:

Post a Comment