×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

How can I add MultiSelect functionality to a grid or allow to select multiple cell values
3

How can I add MultiSelect functionality to a grid or allow to select multiple cell values

How can I add MultiSelect functionality to a grid or allow to select multiple cell values

(OP)
Hi,
can i get some guides here, on how to select from a vfp grid a few raws or a portion of some cells value and copy them and be able to paste later ?
i am a novice, as you know, so i just exploring how to
thanks a lot

RE: How can I add MultiSelect functionality to a grid or allow to select multiple cell values

This is actually quite a common question. Unfortunately, there is no easy solution.

To select multiple rows, probably the best way is to add a checkbox to the grid. The user then ticks the checkbox in the rows he wants to select. Rather than my trying to give you the steps, I suggest you read this article:

Understanding the Visual FoxPro grid control

Scroll down to the section "Adding different controls to the grid".

In fact, given that you say you are a novice, I suggest you study that article quite carefully before you go much further.

As you will see from the article, the checkbox must be bound to a field in the underlying table or cursor. After the user has ticked or unticked the relevant checkboxes, the corresponding field in the table will contain .T. (if ticked) or .F. (if unticked). You then have to look through the table for the records where the field is .T., and perform your action on them.

Another approach - much simpler - is to use a listbox rather than a grid. Unlike the grid, a listbox has a multi-select feature, which is exactly what you want. However, listboxes have pros and cons compared to grids, so it might not be ideal solution for you.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads

RE: How can I add MultiSelect functionality to a grid or allow to select multiple cell values

(OP)
Hi Mike,
Yes the approach using a checkbox, i already accomplished it, i was looking for the other way, as selecting as we do in Excel, the listbox if you have so many raws or records shows it is inconvenient, plus in a listbox so many fields it is not a choice
Thanks

RE: How can I add MultiSelect functionality to a grid or allow to select multiple cell values

Well, the gird is not a spreadsheet, so you can't select an arbitrary rectangle of rows and columns. It's just not designed to do that.

One option would be to actually embed an Excel worksheet into your VFP form. You can do that using OLE Automation. Users would be able to highlight an arbitrary range of cells, which you could then copy to a table and process in some way from within your application. But the coding would be far from simple, and given that you say you are a novice, is probably not to be recommended.

Perhaps there is some other way of achieving your ultimate goal?

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads

RE: How can I add MultiSelect functionality to a grid or allow to select multiple cell values

(OP)
i believe what you are saying here, is to display the result of a query(select sql) into an Excel sheet, then allow users, just to make any selection from the excel sheet by just copying and then after copied, then do whatever and put that into something else ?
Thanks

RE: How can I add MultiSelect functionality to a grid or allow to select multiple cell values

Here is an example (probably not much use to you) of a drag and drop from a grid to a listbox.

oForm = Createobject('myForm')
oForm.Show(1)

Define Class myForm As Form
  Height = 300
  Width = 470
  DoCreate=.T.
  DataSession=2
  nxtwips = .F.
  nytwips = .F.

  Add Object myTree As OleControl With ;
    Top = 0, ;
    Left = 0, ;
    Height = 300, ;
    Width = 200, ;
    Name = "myTree", ;
    OleClass = 'MSComCtlLib.TreeCtrl'

  Add Object myGrid As myGrid With ;
    Top = 0, ;
    Left = 220, ;
    Height = 300, ;
    Width = 250, ;
    Name = "myGrid"

  Procedure pixeltotwips
    *-- Code for PixelToTwips method
    Local liHWnd, liHDC, liPixelsPerInchX, liPixelsPerInchY
    #Define cnLOG_PIXELS_X 88
    #Define cnLOG_PIXELS_Y 90
    #Define cnTWIPS_PER_INCH 1440

    * Declare some Windows API functions.
    Declare Integer GetActiveWindow In WIN32API
    Declare Integer GetDC In WIN32API Integer iHDC
    Declare Integer GetDeviceCaps In WIN32API Integer iHDC, Integer iIndex

    * Get a device context for VFP.
    liHWnd = GetActiveWindow()
    liHDC = GetDC(liHWnd)

    * Get the pixels per inch.
    liPixelsPerInchX = GetDeviceCaps(liHDC, cnLOG_PIXELS_X)
    liPixelsPerInchY = GetDeviceCaps(liHDC, cnLOG_PIXELS_Y)

    * Get the twips per pixel.
    Thisform.nxtwips = ( cnTWIPS_PER_INCH / liPixelsPerInchX )
    Thisform.nytwips = ( cnTWIPS_PER_INCH / liPixelsPerInchY )
    Return
  Endproc

  Procedure Load
    Create Cursor myTest (myID i, myType c(20))
    For ix = 1 To 20
      Insert Into myTest (myID,myType) Values (ix,Sys(2015))
    Endfor
    Locate
    This.pixeltotwips()
  Endproc

  Procedure myTree.Init
    #Define tvwFirst    0
    #Define tvwLast    1
    #Define tvwNext    2
    #Define tvwPrevious    3
    #Define tvwChild    4

    With This
      .linestyle =1
      .labeledit =1
      .indentation = 5
      .PathSeparator = '\'
      .Scroll = .T.
      .OLEDragMode = 1
      .OLEDropMode = 1

      For ix=1 To 3
        .Nodes.Add(,tvwFirst,"root"+Ltrim(Str(ix)),'Main node '+Ltrim(Str(ix)))
        For jx=1 To 4
          .Nodes.Add("root"+Ltrim(Str(ix)),tvwChild,;
            "child"+Ltrim(Str((ix-1)*4+jx)),;
            'Child '+Ltrim(Str(jx))+' of '+Ltrim(Str(ix)))
        Endfor
      Endfor
    Endwith
  Endproc

  Procedure myTree.MouseDown
    *** ActiveX Control Event ***
    Lparameters Button, Shift, x, Y
    With Thisform
      oHitTest = This.HitTest( x * .nxtwips, Y * .nytwips )
      If Type("oHitTest")= "O" And !Isnull(oHitTest)
        This.SelectedItem = oHitTest
      Endif
    Endwith
    oHitTest = .Null.
  Endproc

  Procedure myTree.OLEDragOver
    *** ActiveX Control Event ***
    Lparameters Data, effect, Button, Shift, x, Y, state
    oHitTest = This.HitTest( x * Thisform.nxtwips, Y * Thisform.nytwips )
    If Type("oHitTest")= "O"
      This.DropHighlight = oHitTest
      If !Isnull(oHitTest)
        If Y <= This.Top + 150     And Type('oHitTest.Previous')='O'     And !Isnull(oHitTest.Previous)
          oHitTest.Previous.EnsureVisible
        Endif
        If Y >= This.Top + This.Height - 150 And Type('oHitTest.Next')='O' And !Isnull(oHitTest.Next)
          oHitTest.Next.EnsureVisible
        Endif
      Endif
    Endif
  Endproc

  Procedure myTree.OLEDragDrop
    *** ActiveX Control Event ***
    Lparameters Data, effect, Button, Shift, x, Y
    #Define tvwChild    4
    With This
      If Data.GetFormat(1)    And ;
          type(".DropHighLight") = "O" And !Isnull(.DropHighlight) &&CF_TEXT
        loTarget = .DropHighlight
        .Nodes.Add(loTarget.Key,tvwChild,;
          Sys(2015),;
          Data.GetData(1))
      Endif
    Endwith
    This.DropHighlight = .Null.
  Endproc
Enddefine

Define Class myGrid As Grid
  OLEDropMode = 1
  OLEDragMode = 1

  Procedure OLEStartDrag
    Lparameters oDataObject, nEffect
    With This
      .OLEDropMode = 0
      Amouseobj(arrMouse,1)
      lnActiveRow = Ceiling( ;
        ( arrMouse[4] - (.Top + .HeaderHeight) ) / .RowHeight )
      .ActivateCell(lnActiveRow,2)
    Endwith
    oDataObject.SetData(myTest.myType,1)
  Endproc

  Procedure OLECompleteDrag
    Lparameters nEffect
    This.OLEDropMode = 1
  Endproc

  Procedure OLEDragOver
    Lparameters oDataObject, nEffect, nButton, nShift, nXCoord, nYCoord, nState
    Local nWhere,nRelRow,nRelCol,nView
    Store 0 To nWhere,nRelRow,nRelCol,nView
    This.GridHitTest(m.nXCoord,m.nYCoord,@nWhere,@nRelRow,@nRelCol,@nView)
    If m.nWhere = 3
      This.ActivateCell(m.nRelRow, m.nRelCol)
    Endif
  Endproc

  Procedure OLEDragDrop
    Lparameters oDataObject, nEffect, nButton, nShift, nXCoord, nYCoord
    If oDataObject.GetFormat(1)
      With This
        .Columns(2).Text1.Value = oDataObject.GetData(1)
      Endwith
    Endif
  Endproc
Enddefine
 

Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483: How to get the best response from the forum first.

RE: How can I add MultiSelect functionality to a grid or allow to select multiple cell values

(OP)
Mr. Gagnon,
Yes this is not exactly what i meant but it is very interesting, specially for me, as i can see how we can drag and drop, very good example, will try to study this to learn the trick and the needed code
Thanks

RE: How can I add MultiSelect functionality to a grid or allow to select multiple cell values

My point being there is a lot of code for a single cell to move, you can imagine multiple cells at the same time.

Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483: How to get the best response from the forum first.

RE: How can I add MultiSelect functionality to a grid or allow to select multiple cell values

(OP)
Yes Mike, it is a lot of code as this only allow you to drag one a value at time from a cell, i am wondering if someone has already a Grid class that does whatever range of cell from the grid ?
Thanks

RE: How can I add MultiSelect functionality to a grid or allow to select multiple cell values

Quote:

... a Grid class that does whatever range of cell from the grid ?

No. As I said at the outset, this is not possible. This is not a limitation of drag-and-drop. It's the nature of the grid.

Essentially, there is no object that represents a row in a grid. You can do things with columns but not with individual rows. That's the root of the problem.

There might be some way of simulating a grid, perhaps using a container control that you populate with textboxes or labels in a rectangular pattern, but I doubt you will find anything ready made.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads

RE: How can I add MultiSelect functionality to a grid or allow to select multiple cell values

I mentioned earlier the possibility of using OLE to show the data in the form of an Excel spreadsheet. I don't know if this will meet your needs, but you might like to experiment along these lines:

1. First get your data into a file or a cursor (you can use SELECT - SQL for that). Then use COPY TO ... TYPE XL5 to save it as an Excel file

2. Open a form in the VFP form designer.

3. Drop an OLE Control on it. This is the button labelled "ActiveX Control (OLE Control)" on the toolbar - not "Activex Bound Control".

4. In the resulting dialogue, select "Create From File". Click Browse, and navigate to the file that you saved in step 1.

5. Save and run the form. You should see your data in a grid-like layout.

6. Double-click on the data. You will now see the data in the form of an Excel worksheet, embedded in your form.

You can now interact with that data, just as you would in Excel, including selecting ranges and copying data to the clipboard.

Obviously, this isn't a complete solution. It's just to give you a flavour of what's possible. But it could be a good starting point for your further experiments.

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads

RE: How can I add MultiSelect functionality to a grid or allow to select multiple cell values

(OP)
Thank you so much Mike, your steps are very clear, i hope i can follow them and be successful, at least it is something to try i never has done and from there once i understood all i can continue improving it for the users in a better way, as always the users are the one requesting,can have this or that and so on, of course i guess once i show that data embedded in the form if i want to manipulate that data with other controls, like a "search button cmd " added to the form, this won't work on that embedded excel grid correct ? or it is possible to bind the cmd button to that Ole control ?
Thanks again

RE: How can I add MultiSelect functionality to a grid or allow to select multiple cell values

Quote:

a "search button cmd " added to the form, this won't work on that embedded excel grid correct ? or it is possible to bind the cmd button to that Ole control ?

Probably not (to both questions). At least, I can't off hand see any way of doing that. I'll let you know if I have any other bright ideas. But don't let that stop you from experiment.

Quote:

as always the users are the one requesting,can have this or that and so on,

Ah, how much easier software development would be if we didn't have users.

One thing you will find is that users will ask for the most difficult things, assuming they will be trivial to implement, and they can't understand when you say it's too much work. But they will hesitate asking for simple things on ther assumption they will be difficult or impossible to do.

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads

RE: How can I add MultiSelect functionality to a grid or allow to select multiple cell values

(OP)
you are 10000% correct on about the users, they always believe that because we can do some things, all them expect, if i can tell you or explain you that, you can do it as well with your programming, well in my case it is not so easy as i am not even 1/8 of what you know but that is the reason on continuing trying and finding thanks a lot

RE: How can I add MultiSelect functionality to a grid or allow to select multiple cell values

The closest thing to that I did was a grid with a tricky addition of a transparent container over its full size. This container was used with mouse events to determine coordinates of a rectangle which were then used to make a GriHhitTest (also used in mgagnon's code) on the underlying grid, to see which cells are in that rectangle.

That was a simple case, as I had similar sized small cells which only should be marked for a visual time scheduling, there was no data involved. Even if you get this going for any grid below this container, you'll have a harder time to select a section of cells including scrolling, if you want to copy&paste a section larger than can be seen.

So really, better go for an embedded Excel grid and it's native selection features.

Bye, Olaf.

Olaf Doschke Software Engineering
https://www.doschke.name

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close