previous page main page next page

Hello World Revisited

Recall that sometimes buttons are greyed out, or inactive, and that there are good reasons why this is desirable. In the Hello World example there's little point in having a button available which can Clear an already empty text box. Similarly, if the message is already being displayed, then we would prefer that the Display button is disabled.

We'll make few changes to the program...
 

  • click the View Code icon and select the Form control
  • type in this code for the Load event:
     


 

What this does is to enable the Display button and disable the Clear button when the form is first loaded. If you run this version of the program you'll see what happens....but you'll also discover that the program doesn't quite work as we would like it to.

  • run the program and see if can discover what the problem is
     

Here are some additions to the cmdDisplay control code...
 

Private Sub cmdDisplay_Click()

txtMessage.Text = "Hello World!"
cmdDisplay.Enabled = False
cmdClear.Enabled = True

End Sub
 

...and here are some more for the cmdClear control code:
 

Private Sub cmdClear_Click()

txtMessage.Text = ""
cmdDisplay.Enabled = True
cmdClear.Enabled = False

End Sub
 

  • make these changes and run the program
     


 

You'll see that the program now works in a more acceptable way.

We can, in fact, go a step further and make the disabled button disappear altogether! As well as the Enabled property, there is also a Visible property which can also be set to True or False.
 

  • see if you can make the necessary changes such that the Display and Clear buttons are only visible when they need to be
     

At this stage, you should appreciate that forms contain controls. The controls have various types of properties which can be set at design time and also at run time. The controls can also have associated code which is designed to deal with events. If you've managed to cope with all this, then you're over one of the biggest hurdles. If you're still a little puzzled, then let's work through another example and see if we can clear up any problems...
 

previous page main page next page ©  ePublish Scotland  1999