{"id":16100,"date":"2022-10-14T05:34:02","date_gmt":"2022-10-14T00:04:02","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-focus-cells-in-reverse-order-for-any-utility-dont-support-rtl-closed\/"},"modified":"2022-10-14T05:34:02","modified_gmt":"2022-10-14T00:04:02","slug":"solved-focus-cells-in-reverse-order-for-any-utility-dont-support-rtl-closed","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-focus-cells-in-reverse-order-for-any-utility-dont-support-rtl-closed\/","title":{"rendered":"[Solved] Focus cells in reverse order for any utility don&#8217;t support RTL [closed]"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-39203178\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"39203178\" data-parentid=\"39198684\" data-score=\"4\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>The following code, <strong>without<\/strong> an OnKeyUp handler, does what you seem to want<\/p>\n<pre><code>    type\n      TMyDBGrid = class(TDBGrid);\n\n    procedure TForm1.DBGrid1KeyDown(Sender: TObject; var Key: Word; Shift:\n        TShiftState);\n    begin\n      if Key = VK_Tab then begin\n        Key := 0;\n        if ssShift in Shift then\n          DBGrid1.SelectedIndex := DBGrid1.SelectedIndex + 1\n        else begin\n          if TMyDBGrid(DBGrid1).Col = 1 then begin\n            \/\/  The following goes to the rightmost cell in the next row \n            \/\/  if the focus is already on the leftmost column, as specified\n            \/\/  in the original version of the q\n            DBGrid1.DataSource.DataSet.Next;\n            TMyDbGrid(DBGrid1).Col := DBGrid1.Columns.Count;\n          end\n          else\n            DBGrid1.SelectedIndex := DBGrid1.SelectedIndex - 1;\n        end;\n      end;\n    end;\n<\/code><\/pre>\n<p><strong>Update<\/strong>  In a comment to this, you asked for a fuller implementation.  Here it is:<\/p>\n<pre><code>procedure TForm1.DBGrid1KeyDown(Sender: TObject; var Key: Word; Shift:\n    TShiftState);\n\n  procedure TabForwards;\n  begin\n    if TMyDBGrid(DBGrid1).Col = DBGrid1.Columns.Count then begin\n      DBGrid1.DataSource.DataSet.Next;\n      if DBGrid1.DataSource.DataSet.Eof then\n        DBGrid1.DataSource.DataSet.Append;\n      TMyDbGrid(DBGrid1).Col := 1;\n    end\n    else\n      DBGrid1.SelectedIndex := DBGrid1.SelectedIndex + 1;\n  end;\n\n  procedure TabBackwards;\n  begin\n    if DBGrid1.DataSource.DataSet.State = dsInsert then begin\n       DBGrid1.DataSource.DataSet.Cancel;\n       Exit;\n    end;\n\n    if TMyDBGrid(DBGrid1).Row = 1 then begin\n      if TMyDBGrid(DBGrid1).Col = 1 then\n        TMyDBGrid(DBGrid1).Col := DBGrid1.Columns.Count\n      else\n        DBGrid1.SelectedIndex := DBGrid1.SelectedIndex - 1;\n    end\n    else begin\n      if TMyDBGrid(DBGrid1).Col = 1 then begin\n        DBGrid1.DataSource.DataSet.Prior;\n        TMyDbGrid(DBGrid1).Col := DBGrid1.Columns.Count;\n      end\n      else\n        DBGrid1.SelectedIndex := DBGrid1.SelectedIndex - 1;\n    end;\n  end;\n\nbegin\n  Caption := IntToStr(TMyDbGrid(DBGrid1).RowCount);\n  if cbNormal.Checked then\n    Exit;\n  if Key = VK_Tab then begin\n    Key := 0;\n    if ssShift in Shift then\n      TabForwards\n    else\n      TabBackwards;\n  end;\nend;\n<\/code><\/pre>\n<p>Notice that this uses <code>TabForwards<\/code> and <code>TabBackwards<\/code> sub-procedures and effectively reverses the roles of Tab and Shift Tab.  The reason for this is because it separates thinking, and talking, about the movement behaviour from the key combination which produces it, which I found easier easier to code and to describe what the movement behaviour actually is.<\/p>\n<p>With Tab and Shift Tab behaving as standard, this is how the DBGrid behaves:<\/p>\n<ol>\n<li>Tabbing forwards<\/li>\n<\/ol>\n<p>If the cursor is in the New Record row, focus move to the next cell in the row until it reaches the RH column, then it wraps to the first column.<\/p>\n<p>Otherwise, focus moves to the next cell in the current row until it reaches the RH column, then it wraps to the first column of the next row, if there is one, otherwise it calls Append on the grid&#8217;s Dataset, and moves to the first column of the New Record row.<\/p>\n<p>In the New Record row, pressing the Tab Backwards key abandons the new record.<\/p>\n<ol start=\"2\">\n<li>Tabbing backwards<\/li>\n<\/ol>\n<p>If the cursor is in the New Record row, the new record is abandoned.  Otherwise, focus moves to the prior cell in the current row until it reaches the LH column, then it wraps to the RH column of the prior row.  Once it reaches the LH column of the first row, it wraps to the RH column in that row.<\/p>\n<p>That is what the second example does.<\/p>\n<p>This does not, however, do what you said in your comment <\/p>\n<blockquote>\n<p>when I reach last cell in DBgrid, the tab key (not shift-Tab) need to open new record.<\/p>\n<\/blockquote>\n<p>and this is why:<\/p>\n<p>Your way, the behaviour of the grid in response to the Tab key is different in the last cell compared with every other one.  In the other cells, Tab will move left and wrap upwards to the prior row, whereas in the last cell it will move downwards.  That would puzzle me as a user.  Anyway, if that&#8217;s really what you want, you can rearrange the second example to behave that way.<\/p>\n<p>When you use Tab and Shift Tab in the normal way, the behaviour is as follows:<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">8<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Focus cells in reverse order for any utility don&#8217;t support RTL [closed] <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] The following code, without an OnKeyUp handler, does what you seem to want type TMyDBGrid = class(TDBGrid); procedure TForm1.DBGrid1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin if Key = VK_Tab then begin Key := 0; if ssShift in Shift then DBGrid1.SelectedIndex := DBGrid1.SelectedIndex + 1 else begin if TMyDBGrid(DBGrid1).Col = 1 then begin \/\/ &#8230; <a title=\"[Solved] Focus cells in reverse order for any utility don&#8217;t support RTL [closed]\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-focus-cells-in-reverse-order-for-any-utility-dont-support-rtl-closed\/\" aria-label=\"More on [Solved] Focus cells in reverse order for any utility don&#8217;t support RTL [closed]\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[740],"class_list":["post-16100","post","type-post","status-publish","format-standard","hentry","category-solved","tag-delphi"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>[Solved] Focus cells in reverse order for any utility don&#039;t support RTL [closed] - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-focus-cells-in-reverse-order-for-any-utility-dont-support-rtl-closed\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Focus cells in reverse order for any utility don&#039;t support RTL [closed] - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] The following code, without an OnKeyUp handler, does what you seem to want type TMyDBGrid = class(TDBGrid); procedure TForm1.DBGrid1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin if Key = VK_Tab then begin Key := 0; if ssShift in Shift then DBGrid1.SelectedIndex := DBGrid1.SelectedIndex + 1 else begin if TMyDBGrid(DBGrid1).Col = 1 then begin \/\/ ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-focus-cells-in-reverse-order-for-any-utility-dont-support-rtl-closed\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-10-14T00:04:02+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-focus-cells-in-reverse-order-for-any-utility-dont-support-rtl-closed\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-focus-cells-in-reverse-order-for-any-utility-dont-support-rtl-closed\\\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/person\\\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Focus cells in reverse order for any utility don&#8217;t support RTL [closed]\",\"datePublished\":\"2022-10-14T00:04:02+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-focus-cells-in-reverse-order-for-any-utility-dont-support-rtl-closed\\\/\"},\"wordCount\":416,\"publisher\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\"},\"keywords\":[\"delphi\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-focus-cells-in-reverse-order-for-any-utility-dont-support-rtl-closed\\\/\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-focus-cells-in-reverse-order-for-any-utility-dont-support-rtl-closed\\\/\",\"name\":\"[Solved] Focus cells in reverse order for any utility don't support RTL [closed] - JassWeb\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#website\"},\"datePublished\":\"2022-10-14T00:04:02+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-focus-cells-in-reverse-order-for-any-utility-dont-support-rtl-closed\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-focus-cells-in-reverse-order-for-any-utility-dont-support-rtl-closed\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-focus-cells-in-reverse-order-for-any-utility-dont-support-rtl-closed\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Focus cells in reverse order for any utility don&#8217;t support RTL [closed]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#website\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/jassweb.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/jass-website-logo-1.png\",\"contentUrl\":\"https:\\\/\\\/jassweb.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/person\\\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"contentUrl\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\\\/\\\/jassweb.com\"],\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/author\\\/jaspritsinghghumangmail-com\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Focus cells in reverse order for any utility don't support RTL [closed] - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-focus-cells-in-reverse-order-for-any-utility-dont-support-rtl-closed\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Focus cells in reverse order for any utility don't support RTL [closed] - JassWeb","og_description":"[ad_1] The following code, without an OnKeyUp handler, does what you seem to want type TMyDBGrid = class(TDBGrid); procedure TForm1.DBGrid1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin if Key = VK_Tab then begin Key := 0; if ssShift in Shift then DBGrid1.SelectedIndex := DBGrid1.SelectedIndex + 1 else begin if TMyDBGrid(DBGrid1).Col = 1 then begin \/\/ ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-focus-cells-in-reverse-order-for-any-utility-dont-support-rtl-closed\/","og_site_name":"JassWeb","article_published_time":"2022-10-14T00:04:02+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-focus-cells-in-reverse-order-for-any-utility-dont-support-rtl-closed\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-focus-cells-in-reverse-order-for-any-utility-dont-support-rtl-closed\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Focus cells in reverse order for any utility don&#8217;t support RTL [closed]","datePublished":"2022-10-14T00:04:02+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-focus-cells-in-reverse-order-for-any-utility-dont-support-rtl-closed\/"},"wordCount":416,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["delphi"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-focus-cells-in-reverse-order-for-any-utility-dont-support-rtl-closed\/","url":"https:\/\/jassweb.com\/solved\/solved-focus-cells-in-reverse-order-for-any-utility-dont-support-rtl-closed\/","name":"[Solved] Focus cells in reverse order for any utility don't support RTL [closed] - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-10-14T00:04:02+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-focus-cells-in-reverse-order-for-any-utility-dont-support-rtl-closed\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-focus-cells-in-reverse-order-for-any-utility-dont-support-rtl-closed\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-focus-cells-in-reverse-order-for-any-utility-dont-support-rtl-closed\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Focus cells in reverse order for any utility don&#8217;t support RTL [closed]"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/16100","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=16100"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/16100\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=16100"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=16100"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=16100"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}