Board index » cppbuilder » How to "control" key Tab from keyboard

How to "control" key Tab from keyboard


2006-03-11 05:21:24 AM
cppbuilder114
Hi guys, i'm totally new in BCB6 and i need some help here!
In my project's form i have several Edit boxes, DBEdit boxes, a Data source
and a DBGrid. In a data module now i have a table and several queries. I
have made my project to store the values which i pass through the edit boxes
to my table (at run time). I even found a way to change the already stored
values in the table with some help from the DBEdits but there is a serious
problem (again at run time). The values that i pass are checked from an
error function that i created and i placed it OnKeyPress Event of each
DBEdit box (if Key == 13....// then do the check for duplicate values). All
works fine when i press enter (at run time). The problem starts if I press
Tab to pass from one DBEdit to another (at run time). The values I write,
pass to the table without any warning for duplicate values!!! I tryied all
the numbers (0 to 32) from the table ASCII in the OnKeyPress function (the
code i wrote is if ((Key == 13)||(Key == X) where X = 1...32), but nothing
seems to work. By pressing the Tab key, it happens the following:
Project runs .... DBEdit1->SetFocus()....Then i press Tab
From DBEdit1->Text it goes to DBEdit2->Text,
From DBEdit2->Text it goes to DBEdit3->Text,
From DBEdit3->Text it goes to the last record of the table in DBGrid!!!
In OnKeyPress Event for DBEdit4 i wrote :
if ((Key == 13) || (Key == 9))
DBEdit1->SetFocus();
Is it or is it not the number 9 the decimal number of Horizontal Tab in
ASCII Table???
I even tried with the OnKeyDown Event where i wrote:
if (Key == VK_TAB)
DBEDit1->SetFocus();
but nothing changed!!!
How can i take control of the key Tab?
Thanks for your time!!!
 
 

Re:How to "control" key Tab from keyboard

"Jimakos Bilakis" < XXXX@XXXXX.COM >wrote in message
Quote
The problem starts if I press Tab to pass from one DBEdit to another
(at run time). The values I write, pass to the table without any warning
for duplicate values!!!
As it should be, because the Tab key does not trigger the OnKey... events by
default. It is a navigation key that is reserved by the OS. In order for
the Tab key to trigger the events, you have to subclass each control in
order to intercept the WM_GETDLGCODE message. You can then specify
DLGC_WANTTAB in the message's return value.
Why not just configure the DB itself to not allow duplicate values for the
desired fields? Then you don't have to validate the values yourself.
Quote
Is it or is it not the number 9 the decimal number of Horizontal Tab
in ASCII Table???
Yes, it is.
Gambit
 

Re:How to "control" key Tab from keyboard

Hi Jim
Quote
In OnKeyPress Event for DBEdit4 i wrote :
if ((Key == 13) || (Key == 9))
DBEdit1->SetFocus();

Instead use OnExit of DBEdit4
void __fastcall TForm1::DBEdit4Exit(TObject *Sender)
{
DBEdit1->SetFocus();
}
You have to press Ctrl + I to catch a Key = = 9 in keyDown event (just to
know )
-minas charokopos
 

{smallsort}

Re:How to "control" key Tab from keyboard

Quote
It is a navigation key that is reserved by the OS.
OS ???? What initial letters are these???
I think i have heard it somewhere but i don't remember where....
Quote
In order for the Tab key to trigger the events,
you have to subclass each control in order to intercept the
WM_GETDLGCODE message. You can then specify
DLGC_WANTTAB in the message's return value.
Can you an example with some lines of code??? (If it is simply enough to do
it so,
cause i don't really understand what do you mean by that)
Quote
Why not just configure the DB itself to not allow duplicate values for the
desired fields? Then you don't have to validate the values yourself.
Again, how do you mean to configure the DB itself??? Where i have to do that
and what do i have to write in general words (of code of coursse - if it is
easy for you )?
Thank you very much for your reply Gambit!
 

Re:How to "control" key Tab from keyboard

Quote
Instead use OnExit of DBEdit4
void __fastcall TForm1::DBEdit4Exit(TObject *Sender)
{
DBEdit1->SetFocus();
}
It did work!!! Now every time i press Tab and the "focus" from DBEdit4,
it passes to the DBEdit1 and not to the DBGrid as exactly i wanted to do
so!!!
Quote
You have to press Ctrl + I to catch a Key = = 9 in keyDown event (just to
know )
Why to write Key = = 9 in KeyDown Event??? I thought from the examples (Help
files)
in the OnKeyDown Event has more meaning to use the Virtual Keys (VK). For
example,
if (Key = = VK_TAB) or if (Key = = VK_ESCAPE) etc. Am i understand something
wrong?
Thank you so much for your reply too mina. BTW, are you greek???
 

Re:How to "control" key Tab from keyboard

Hi Jim,
Quote
Why to write Key = = 9 in KeyDown Event??? I thought from the examples
(Help files)
You don't.Just to know that as 9 in keyDown event will catch Ctrl + I
combination and not TAB key although they have the same value (9).
Quote
in the OnKeyDown Event has more meaning to use the Virtual Keys (VK). For
example,
if (Key = = VK_TAB) or if (Key = = VK_ESCAPE) etc. Am i understand
something wrong?
The TAB key is normally used to switch focus from one control to another ,
according to tab order.Also never produces a KeyDown or KeyUp event.That is
what Remy said in his post.
You can change tab order by right clcking on your from or on a component
that contains other components and select Tab Order.
Quote
Thank you so much for your reply too mina. BTW, are you greek???
nai = yes :-)
-minas
 

Re:How to "control" key Tab from keyboard

Quote
>Why not just configure the DB itself to not allow duplicate
>values for the desired fields? Then you don't have to validate
>the values yourself.

Again, how do you mean to configure the DB itself??? Where i
have to do that and what do i have to write in general words (of
code of coursse - if it is easy for you )?
Every 'modern' database has the ability to to be configured to
have Primary Keys, Indexes, Referential Integrities and so on...
One of the ordiary things when setting Indexes is to say if an
Indexed filed can accept Duplicated Values.
That way, the database is "secure" ond the 'system level' and does
not depend of the quality (bugs) of your interface.
So, you are not supposed to catch DBField and similar events, and
insted of that you could only catch OnPostError() for that.
That's the way you should deal with DB's.
--
Best Regards,
Vladimir Stefanovic
 

Re:How to "control" key Tab from keyboard

Quote
That's the way you should deal with DB's.
You're totally right. I haven't think of it that way. I'll try to catch the
duplicate items from the DB that i use (Paradox). I believe to have this
ability. Thanks for your valuable informations. :)
 

Re:How to "control" key Tab from keyboard

Hi mina,
Quote
You can change tab order by right clcking on your from or on a component
that contains other components and select Tab Order.
Indeed i can! I was wonder what tab order was about, but now you have solve
my question! Thanks a lot! You have helped me so much! BTW i'm greek too. :P
 

Re:How to "control" key Tab from keyboard

Hi Mina,
sorry to bother you again, i just don't understand a detail!
You told me that the TAB key is normally used to switch focus from one
control to another , according to tab order. I agree, but why every time
that i pass from a DBEdit to another through Tab, it applies the
"containment" of my DBEdit to the table of my database? Isn't that odd??? I
believed that it only set focus to another component in my form and just it.
Why does it keep "playing" the role of the transporter in my project? Is
there any way to avoid it?
I faced the same problem before! OnKeyPress Event i wrote code:
if (Key == 13)
{
if(!(DBEdit1->Text.IsEmpty()))
{
if (blah blah) // check for the validity of the
value
{
// everything ok pass the values to the
table.
DBEdit2->SetFocus();
}
else // The DBEdit is not empty but the value in it
is not acceptable.
{
MessageDlg("Problem", mtWarning,
TMsgDlgButtons() << mbOK, 0);
Abort(); // With this function i solve the
problem. :)
DM_Pinakas_Syntelestvn->PinakasSynt->Refresh();
// The datamodule (name = DM_Pinakas_Syntelestvn) where i keep my table (=
PinakasSynt)
}
}
else // totally empty DBEdit - something that i don't want
at all.
{
MessageDlg("Problem", mtWarning, TMsgDlgButtons() <<
mbOK, 0);
DBEdit1->Undo();
DBEdit1->SetFocus();
}
}
As you can see the function Abort() "saved" me! But now i face the same
problem with Tab! I tried with OnExit Event to do something similar but no
result till now..:((! Do you have any idea plz? Thank you in advance = Σε
ευχαριστ?εκ τω?προτέρων γι?το χρόν?σο?κα?τη?θέλησή σο?να με
βοηθήσει?:))
 

Re:How to "control" key Tab from keyboard

Σε κάποιο μήνυμα μο?κάνε δεξί κλίκ ->ιδιότητε?γι?το μαιλ μο?:-)
guys, sorry about these greeks :-)
-minas
 

Re:How to "control" key Tab from keyboard

Hi Jim,
Quote
that i pass from a DBEdit to another through Tab, it applies the
"containment" of my DBEdit to the table of my database?
well, because of DataSource. DBEdits use the same DataSource as your DBGrid.
Right :-)
-minas
 

Re:How to "control" key Tab from keyboard

"Jimakos Bilakis" < XXXX@XXXXX.COM >wrote:
Quote

[...] How can i take control of the key Tab?
My choice is to use the TApplication::OnMessage event. If you
listen for WM_KEYDOWN/UP, you can change the key value before
it ever gets to a control. For example:
private: // User declarations
void __fastcall AppMessage( TMsg &Msg, bool &Handled );
public: // User declarations
__fastcall ~TForm1();
//-------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{
Application->OnMessage = AppMessage;
}
//-------------------------------------------------------------
__fastcall TForm1::~TForm1()
{
Application->OnMessage = NULL;
}
//-------------------------------------------------------------
void __fastcall TForm1::AppMessage( TMsg &Msg, bool &Handled )
{
if( Msg.message == WM_KEYDOWN || Msg.message == WM_KEYUP )
{
if( Msg.wParam == VK_TAB )
{
if( dynamic_cast<TDBEdit*>( Screen->Active->Control ) )
{
Msg.wParam = VK_RETURN;
}
}
}
}
//-------------------------------------------------------------
~ JD
 

Re:How to "control" key Tab from keyboard

"Jimakos Bilakis" < XXXX@XXXXX.COM >wrote in message
Quote
OS ???? What initial letters are these???
OS = Operating System (Windows, Linux, Mac, etc).
Quote
Can you an example with some lines of code???
TWndMethod OldWndProc;
__fastcall TForm1::TForm1(TComponent *Owner)
: TForm(Owner)
{
OldWndProc = DBEdit1->WindowProc;
DBEdit1->WindowProc = DBWndProc;
}
void __fastcall TForm1::DBWndProc(TMessage &Message)
{
OldWndProc(Message);
if( Message.Msg == WM_GETDLGCODE )
Message.Result |= DLGC_WANTTAB;
}
Quote
Again, how do you mean to configure the DB itself???
How can you use a database and not understand how it works? Have you never
worked with indexes or primary keys before? A primary key can be set up
inside the DB itself to ignore duplicate values that are posted to the DB.
Gambit
 

Re:How to "control" key Tab from keyboard

"Jimakos Bilakis" < XXXX@XXXXX.COM >wrote in message
Quote
You're totally right. I haven't think of it that way. I'll try to catch
the
duplicate items from the DB that i use (Paradox). I believe to have
this ability. Thanks for your valuable informations. :)
Any further DB-related questions should be posted to the database
newsgroups.
Gambit