Turbo Vision Tutorial

Part 6 -- Extending Elements




Welcome

Hi! Welcome to the last chapter of Turbo Vision (TV) tutorial. I hope that you are eager to finish this one. After following these tutorial, you may find that these are just fundamentals. Yes, it is. There are lots of other features yet uncovered. However, you can easily look into TV manual or help to guide you further after you understand these basics.

 

Can I Make My Own Elements?

Yes, of course. Thanks to the strength of OOP. Customizing elements to suit your need is merely inheriting a class and modify the appropriate methods to change its behavior. Yes, you don't need to redefine the class from scratch as you would in conventional programming style. If you think that the behavior is pretty much the same, just leave it alone and modify the thing that is different.

OK, now, how can I inherit a class to create my own elements? First, you can pick one class that is pretty close. Suppose that you want to make a view of your database. The current view is not enough. You need to display which record number to display. Well, you can pick TView as your base class and start extending it. You may came up with the question "Why can't we choose TWindow instead?" I know it needs practice (and somehow intuition) to pick which class to modify. If you have programmed with TV a bit longer, you may know which class to pick as you have a knack on them.

Let's suppose that we want to extend TView with a counter on it. Of course we then have to declare a class as a descendant of TView. We obviously need to add a field to store the counter and some methods to modify the counter. Then, the next (hard) question is to pick up which methods to inherit. Well, here, we obviously need to inherit the Init constructor since we need to reset our counter to 0 whenever it starts. The next method is Draw since we need to show the counter. The rest? Hmm... we don't need to touch them as everything else does not need to be changed when we have a counter.

OK, show me the code then. Here you go. :-)

type
   PMyView = ^TMyView;
   TMyView = object(TView)
               counter: Longint;
               constructor Init(var Bounds: TRect); virtual;
               procedure Draw; virtual;
               procedure SetCounter(ctr: Longint);
               function  GetCounter: Longint;
               procedure IncCounter;
               procedure DecCounter;
             end;

Remember that the parameters of the inherited methods must be exactly the same. Let's look on how the methods behave. The counter modification methods are simple:

procedure TMyView.SetCounter(ctr: Longint);
begin
  counter:=ctr;
end;

function TMyView.GetCounter: Longint;
begin
  GetCounter:=counter;
end;

procedure TMyView.IncCounter;
begin
  inc(counter);
end;

procedure TMyView.DecCounter;
begin
  dec(counter);
end;

How can we deal with the Init constructor? For all inherited methods, you need to call inherited and followed by the method name and its parameters. This is to call the superclass' routine to handle the standard stuff. You can add some code either before or after the inherited statement. For example:

constructor TMyView.Init(var Bounds: TRect);
begin
  :
  counter := 0;     { <-- your custom code }
  inherited Init(Bounds);
  :
end;

Choosing between before or after the inherited statement involves a bit of knowledge about the class that you extend. Here, in TView we have to put the counter reset before the statement since the inherited Init will call the method Draw once. Draw requires the counter value to be valid prior to display. Remember that other classes may be different than this.

Doing the Draw is just similar. However, you can experiment about that to control the behavior of your newly assembled component. I just leave it out for you to exercise.

 

Closing

Extending objects to customize them to your needs require a deeper knowledge about the behavior of the component. Of course, experience and practice is essential to make this succeed.

Albeit describing only the basics of Turbo Vision (TV), I hope that this tutorial can inspire you and acquaint you with TV. There are a lot of things not yet explained, such as color dialogs, palettes, calculators, and so on. However, once you get the basics, you can expand your knowledge by reading Turbo Vision help or manual.

OK, I think that's all for now. See you next time

 


Where to go

Chapter 13
News Page
Pascal Lesson 3 index
Contacting Me


Roby Joehanes © 2001