TStringGrid with BOTH editing and range selection?

Question:

Can anyone point out a sample article or code anywhere, how to provide BOTH editing and range selection in TStringGrid?

Yes, I KNOW There are third-party networks that do this, but it's a disappointment that the embedded grid does not have this basic capability.

Background:

It’s quite normal to expect that they will be able to edit a cell in the grid, as well as select a range of cells, for example, for a copy operation.

As indicated, TStringGrid does not. It is either / or. In fact, docs tell us about the grid options: "When goEditing is included in Options, goRangeSelect has no effect."

However, it looks like it's possible to do the editing and rangeselects in a TStringGrid anyway !!! Thanks to careful use of musculature, mouseup, selectcell and exit events, you can get dang close by switching editing elements at the right time. But I still don’t have it perfectly, and this only applies to using the mouse, and not changing the keyboard.

+4
source share
2 answers

I have not used TStringGrid for this, so I cannot provide a specific answer. But do I correctly assume that you can manually (in code) run an edited cell ? This link implies that this is possible, even if the grid does not have goEditing included in its Options . (See below to get around this if this is not true.)

If so, I would suggest the following approach:

Combined selection and editing behavior

I believe this is a good approach, similar to the Windows-standard behavior:

  • Leave the grid in select mode, so interacting with the mouse and keyboard selects cells

  • Run a cell that you can edit yourself based on certain criteria (I think that you are on the way to doing this from what you said in your last paragraph.) There are the usual ways to start editing and the following criteria as my programs follow when they do something similar with other controls:

    • The choice is normal. That is, click to select, click and drag to select multi-select, use the keyboard arrows and Shift or Control to select, etc.

    • A cell goes into edit mode if:

      • The cell is selected and the user presses Enter or F2 ( F2 is the standard Rename or Change combination that works in several programs)

      • User "slow-double click" on the cell, i.e. double-click slowly, select and edit, or click again after a pause in an already selected cell. This mimics the behavior of the explorer, where if a file is selected and you later click on it, it enters the built-in editing / renaming mode. To accomplish this, record when the camera was last clicked (and selected.) If it is pressed again, and if the time is longer than GetDoubleClickTime then they double-clicked, slowly, and entered into edit mode. This allows you to distinguish between the first click to select, double-click (to perform some action) and slow second click to enter edit mode.

        I also try to check the position of the mouse, so if the object is slow, double-click and it was not selected first (i.e. this selects the object and then goes into edit mode). I'm sure the mouse hasn’t changed much. I use GetSystemMetrics to find the double-click distance, and make sure that the slow double-click was in this field. (Because this is not a real double click, I really check the space time 2. My action code:

        const int iMAX_MOVE_AMOUNT = ::GetSystemMetrics(SM_CYDOUBLECLK) * 2; (Sorry, C ++ is not Delphi, but should be easily convertible!)

        but I'm really not sure if this is completely and completely 100% for Windows. In practice, users find that they work as they expect.)

This will allow you to switch between selection and editing at the appropriate time from the keyboard and mouse.

Different thoughts

You may find that some of them are cleaner and easier to implement by subclassing TStringGrid and creating a new component. This will allow you to implement this in regular code and override the built-in behavior (rather than event handlers), while keeping it invisible to form code. It will also give you lower level access to mouse events or Windows messages than simply through events such as OnMouseDown . Finally, if there is a problem displaying the editor when goEditing enabled in Options , this will allow you to change this behavior. You can also add your own events if you want your code to respond to certain events, for example, creating an OnBeginEdit event, say.

Creating your own components is usually seen as an advanced Delphi theme, but it is actually remarkably easy once you know how to do it! There are some good topics on this site that will introduce you to the subject as a whole, and if you go this route and run into problems, Qaru will certainly be a good place to ask questions :) The Embarcadero Delphi "VCL" Writing Components newsgroup / forum is also an excellent resource, in fact, perhaps even better than SO for this particular topic.

Hope this helps!

+3
source

Yes, this is an old post, but the problem still exists on Delphi XE3. To control this function, I used the following "trick" in the SelectCell procedure: if (ARow = StringGridParam.Row) then begin StringGridParam.Options:= StringGridParam.Options + [goEditing] - [goRowSelect]; end else begin StringGridParam.Options:= StringGridParam.Options + [goRowSelect] - [goEditing]; end; if (ARow = StringGridParam.Row) then begin StringGridParam.Options:= StringGridParam.Options + [goEditing] - [goRowSelect]; end else begin StringGridParam.Options:= StringGridParam.Options + [goRowSelect] - [goEditing]; end;

+1
source

Source: https://habr.com/ru/post/1310168/


All Articles