Board index » delphi » Preventing user from moving in/out of fields

Preventing user from moving in/out of fields

Hi,

I'm an experienced Builder/Delphi wiz but a complete newbie when it
comes to database programming, so I could use some help on this one.

I'm trying to write (using D4) a very simple program just to get the
hang of databases. I have a two-column local P7 dataset where the first
column is a programmatically-generated integer value that's the key and
the second column is a required field of type alpha.

I've created a one-form project that contains a TDBNavigator, TDBGrid,
TDataSource, and TTable and nothing else. All that stuff is working as
advertised in the docs.

I have two problems.

The first problem I have is that if the user inserts a new record and
doesn't enter anything in the required field (column 2), TDBGrid allows
the user to move out of the field anyway (I wouldn't expect otherwise).
Then I get an EDatabaseError exception when the user moves out of the
record.

I'd like to prevent the user from moving out of a field until the bad
data has been corrected or the input is canceled. I'm guessing that I
need to:

1. somehow detect that the user is trying to move out of the field,
either to another field or to another record
2. edit the input to the field
3. re-select the field if it's bad, and display a message

It's #1 that has me stumped. I can handle 2, and I'm pretty sure I know
how to do 3, though I'm receptive to any guidance on this.

My second problem is that Column 1 (the record key) is a read-only field
and I'd like to keep users from moving into it at all. However, TDBGrid
allows the user to move into the field and to type data. As soon as the
user moves out of the field, the field reverts to the original data, but
this is not going to be acceptable behavior in a commercial-quality
product. Is there some way to just prevent the user from ever entering
that column?

Thanks in advance for any suggestions.

Lin
--
Linda K. Sherman <linsh...@gte.net>
{*word*137} Writer: Technical - Business - Government

 

Re:Preventing user from moving in/out of fields


Quote
Linda Sherman wrote in message <36589028.9841...@gte.net>...
>Hi,
>I have two problems.

>The first problem I have is that if the user inserts a new record and
>doesn't enter anything in the required field (column 2), TDBGrid allows
>the user to move out of the field anyway (I wouldn't expect otherwise).
>Then I get an EDatabaseError exception when the user moves out of the
>record.

>I'd like to prevent the user from moving out of a field until the bad
>data has been corrected or the input is canceled. I'm guessing that I
>need to:

>1. somehow detect that the user is trying to move out of the field,
>either to another field or to another record
>2. edit the input to the field
>3. re-select the field if it's bad, and display a message

>It's #1 that has me stumped. I can handle 2, and I'm pretty sure I know
>how to do 3, though I'm receptive to any guidance on this.

>My second problem is that Column 1 (the record key) is a read-only field
>and I'd like to keep users from moving into it at all. However, TDBGrid
>allows the user to move into the field and to type data. As soon as the
>user moves out of the field, the field reverts to the original data, but
>this is not going to be acceptable behavior in a commercial-quality
>product. Is there some way to just prevent the user from ever entering
>that column?

>Thanks in advance for any suggestions.

>Lin
>--
>Linda K. Sherman <linsh...@gte.net>
>{*word*137} Writer: Technical - Business - Government

There are many ways to accomplish what you would like to do. In this
particular case check out the OnColEnter and OnColExit events for the
TDBGrid.

With regards to exiting a column, use the OnColExit event to check the input
by the user, if it is OK let them exit, if it is not then display your error
message and prevent your user from exiting the column.

In the case of preventing the user from accessing the column use the
OnColEnter event to find out which column they are trying to enter. If it is
the record key then you can block them from entering the column, or redirect
them to a different column.

You will find that most data aware components provide access to a wide
variety of events, where you can write code to control the behavior of that
or other components.

Wes

Re:Preventing user from moving in/out of fields


For problem one, instantiate your field objects at design time using the
Fields Editor.  Select the alpha field in the Fields Editor and create an
OnValidate event handler. In the OnValidate event handler raising an
exception or calling the Abort procedure, which raises a silent exception,
will prevent the user from leaving the field or the row.

For problem two you could create OnColEnter and OnColExit event handlers.
Turn the grid's ReadOnly property on when the user enters the key column and
off when the user enters any other. This will remove the ability to type
something in the grid.

Bill

--
Bill Todd
(Sorry but TeamB cannot answer questions received via email)
(Remove nospam from my email address to contact me for any other reason)

Re:Preventing user from moving in/out of fields


Quote
Bill Todd (TeamB) wrote:

> For problem one, instantiate your field objects at design time using the
> Fields Editor.  [etc.]

Thanks! I'll give it a try and let you know how it turns out.

L.
--
Linda K. Sherman <linsh...@gte.net>
{*word*137} Writer: Technical - Business - Government

Re:Preventing user from moving in/out of fields


Quote
Bill Todd (TeamB) wrote:

> For problem one, instantiate your field objects at design time using the
> Fields Editor.  Select the alpha field in the Fields Editor and create an
> OnValidate event handler. In the OnValidate event handler raising an
> exception or calling the Abort procedure, which raises a silent exception,
> will prevent the user from leaving the field or the row.

> For problem two you could create OnColEnter and OnColExit event handlers.
> Turn the grid's ReadOnly property on when the user enters the key column and
> off when the user enters any other. This will remove the ability to type
> something in the grid.

Thanks for the ideas. It turns out in both cases...perhaps this is
something new to this release of Delphi?...that all I needed to do to
get the particular results I wanted was to go into the Field Editor and
create the field objects. It wasn't necessary to catch any events.
Apparently as long as you define field objects and set the read-only and
required attributes correctly in the database itself, the code will
enforce things that far. In fact, if the field is required and nothing
is entered, the OnValidate handler never even gets called...apparently
some sort of check for data is done first. I'm not sure I care for that,
as I'd like to put up a MessageBox rather than just letting the user
stare at an empty field and wonder what the problem is.

Lin
--
Linda K. Sherman <linsh...@gte.net>
{*word*137} Writer: Technical - Business - Government

Other Threads