Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can one control button action? 3

Status
Not open for further replies.

terrytype

Programmer
Jun 1, 2011
97
0
0
ZA
I use Delphi 6 and have an application in which one should either tab forwards or backwards from a particular input field in a grid. If one depresses the Down-button or Up-Button from that field the application freezes up - for good reasons which I need not go into here. Ability to use the Up/Down-buttons anywhere else is fine [required in fact.] Is there a method whereby during RunTime I can prevent the Up/Down-buttons having any effect from a particular field? Thanks in advance.

Old Man Delphi
 
Sorry, it just seems like the whole scenario has become confusing... but...

What are the chances you can wrap this DBGrid up into your own component, or at least your own class? In this manner, you won't have to use the events, you can catch everything with the windows messages. I myself am still fuzzy in the subject, but you can catch the key down windows message to the control and do your own custom handling.

Also, it seems you misunderstood what VK_Up and VK_Down mean. These represent the up and down arrows on the keyboard. There's also for example VK_Left, VK_Right, VK_F8, and so on, everything on the keyboard has a VK constant. It seems as if you are mistaken the VK for whether the key is pressed or depressed (up/down). I know I'm sure getting depressed trying to figure out these windows messages meself :|


JD Solutions
 
Excellent point Andrew. Perhaps I have got to have the bull by the tail subsequent to my opener to this thread. In the opener you will see I refer to Up/DownButtons in which I actually meant with reference to the Numeric Key-pad. The misunderstanding appears to have krept in from enquiry WhoserDaddy "So if I understand you correctly, you want to disable up/down keyevents for a particular column in the TDBGrid?", from which I misunderstood him to mean Up/DownButtons, and unfortunatekly confirmed his misunderstanding. So I am back to .... needing a method whereby during RunTime I can prevent the Up/Down-buttons in the numeric ke-pad having any effect from a particular field? If that is indeed possible.

Old Man Delphi
 
djjd47130 from your comment "it seems you misunderstood what VK_Up and VK_Down mean. These represent the up and down arrows on the keyboard." KEEREKT! That is what I got to taking them to mean. So I assure you I am also "sure getting depressed trying to figure out" what the problem is then. Especially since [on that assumpiton and whilst if Key in [VK_Down] produces the required result if Key in [VK_UP]does not.
I need to prevent Up/Down movements in the grid from where SelectedField.Index = 7.

Old Man Delphi
 
Lemme just put that in some different **uncoded** terminology...

IF user tries to press either the UP or DOWN when the currently selected grid cell is in column # 7, then cancel the UP or DOWN key action.

More detailed...

When user presses any key on the keyboard...
...If currently selected column is the 7th column (at index 6) then...
......If key pressed is either UP (VK_Up) or DOWN (VK_Down) then...
.........Abort DBGrid's standard action of the UP or DOWN keys

Now I kinda assume you want to add special handling for when either the UP or DOWN arrows are pressed. That would be handled similar to the case statement I posted above... Let's add some extra code so you can see your different options...

Code:
procedure TfrmCB.grdCBKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  case grdCB.SelectedField.Index of
    0: begin //1st column
      //Not necessary, just as an example
    end;
    //detect other columns....
    6: begin //7th column
      case KEY of
        VK_Up: begin
          Key:= 0;
          //Do some other special handling for when up arrow is pressed
        end;
        VK_Down: begin
          Key:= 0;
          //Do some other special handling for when down arrow is pressed
        end;
        VK_Return: begin
          //You may wish to apply/save the new value or something here, assuming user pressed "Enter" key
        end;
        VK_Tab: begin
          Key:= 0;
          //You may also wish to block this, as it can jump out of the cell just as the Up/Down keys can
        end;
      end;
    end;
  end;
end;

And sorry, but proper indenting of code is one of my pet peeves.


JD Solutions
 
JD Solutions You deserve 4 stars [and 1Ltr bottle of whisky!- where can I have it delivered?] for taking all that time and trouble to solve my problem. The bad news being that when I went to school [counting from the left - starting with column '0'] the 6th column is in fact in column 5. [Sorry! But when it comes to counting - I am also a "stickler!":) Ho! Ho!]

But if it makes you feel any better a BreakPoint on
grdCB.SelectedField.Index reveals that D6 decides,
for reasons best known to itself, that it is indeed column 7. I don't actually care - especially since your code works perfectly if I have 7: begin . :)

So this thread now really can be put to rest.

Thanks once again to you and this great website without which I would probably never have solved this problem!

Old Man Delphi
 
The reason Delphi decides for reasons best known to itself that the columns aren't what you expect is that SelectedField.Index does not refer to the column in the DBGrid. It refers to the TField mapped to that column in the underlying TDataset.

If, at design time, you double click on the DBGrid component you are shown the DBGrid columns editor. You can select and order the fields you wish to see visible on the DBGrid.

Suppose your SQL statement is
Code:
SELECT a, b, c, d FROM table
If you don't use the DBGrid columns editor you will see in the grid all four fields displayed in the order

a b c d

By using the DBGrid columns editor, you can actually display, say, three fields on the DBGrid in the order

d c b

In which case the first column (column 0) showing the d field would have grid.SelectedField.Index having a value of 3 (as it was the fourth field in the underlying dataset); the second column (column 1) would have grid.SelectedField.Index having a value of 2 and the third column would have a grid.SelectedField.Index of 1.

Hope that helps!

Andrew
 
Ahh, yes that makes tons of sense towerbase... I don't usually work with a DBGrid, only a StringGrid - but regardless of the index of the column you need, that's not necessarily what this forum thread was about to begin with, that was just a sample of how to implement it. I just chose the wrong index property to read, instead you need to identify the "Column" not "Field" which is selected. You can have that bottle of whiskey delivered to 369 Somestreet - Somewhere, USA 54321.


JD Solutions
 
Ah yeeeesss!!!!! I can see cleeeearly now......
Apologies to some-or-other pop singer

Old Man Delphi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top