Dialog boxes in visual basic 2010

DIALOG BOXES

Let us look at colour dialog box.



This is a continuation of our tutorial from sub routines

Sub routine in visual basic

So with your project still loaded locate the colour dialog from the tool box and add it to the form.

sub4

The one highlighted above under dialogs

Once its loaded on the it appears on the lower area and not on the form this is an example of an invisible control. It is given the name colordialog1.

sub5

We have now to write a code to show the dialog at run time and perm some manipulation on our form.

In our sub colour delete what was there and enter the following

 

Private Sub colour()

ColorDialog1.ShowDialog()

End Sub

Test the program right away.

This dialog box appears.

sub6

Select a colour and click okay. Nothing should happen so we require a code now to change the colour of the form.

This is it.

Me.BackColor = ColorDialog1.Color

So enter this code after the one of showing the dialog.

This is the full code here below.

Public Class Form1

    Private Sub colour()

        ColorDialog1.ShowDialog()

        Me.BackColor = ColorDialog1.Color

End Sub

 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

colour()

End Sub

 End Class

Run your program and test it, click the change colour button , when the dialog box appears select a colour and click okay did it change. The answer is yes.

A sample output

sub7

Show we can change the form to different colours.



EXERCISE.

Create a new project  once the form loads and a richtextbox  control to the form and  two buttons

Button one change its text property to text colour

Button two change its text property to back colour

The program will change text colour of selected text using button one

And back colour using button two.

This should be the interface.

sub8

Enter the appropriate code and ensure your program runs well.

This is an example of the output from the program.

 

sub10

Another output

sub9

You can come up with other various examples.

MORE ON FUNCTIONS.

Parameters.

These are values that are passed onto a function

EXAMPLE

Public function interest(principle as double, rate as integer, years as integer) as double.

 

The function in this case is interest

principle , rate , years are the parameters for the function to work with.

Let as look at an example.

Design a project that looks like the  interface below.

sub11

The user is supposed to enter the principle