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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

chechkbox keeps the last selected row

Status
Not open for further replies.

oscar lopez

Programmer
Dec 11, 2022
5
CO
I have a form with a grid, the first column is not in the packages table, I added a checkbox. but the last marked row is being selected in the checkbox, it is maintained despite closing the form.

The value that is configured in the check1.value is 0, after configuring the recordsource and the grid source control, I have this code but the problem continues.

checkbox_-_Copy_khw4jm.jpg


Code:
SET PATH TO "C:\Users\oscar\Documents\Visual FoxPro Projects\transport\dbfs"

      USE IN 0 SHARED packages

      Thisform.packages.RecordSource = "packages"
      Thisform.packages.column2.ControlSource = "packages.code"
      Thisform.packages.column3.ControlSource = "packages.name"
      Thisform.packages.column4.ControlSource = "packages.address"
      Thisform.packages.column5.ControlSource = "packages.phone"
      Thisform.packages.refresh
      thisform.packages.setfocus()
      thisform.refresh
 
Oscar, I'm afraid your question isn't very clear. What exactly do you mean by "the last marked row is being selected in the checkbox"? And what exactly "is maintained despite closing the form"?

Also, you have given us the ControlSource values for most of the columns, but what we really need to know is the ControlSource of Column 1.

(Welcome to the forum, by the way.)

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
For example, if I select the second row in the checkbox, it stays selected even after closing the form.
The value of the checkbox is not stored in the table, how do I do it so that when the form is reopened the checkbox does not have any rows selected?
 
I understand what you say is contradictory, if nothing is saved, the status should not reappear. But the fact it is coming back as it was speaks against what ou think happens.

Have you single stepped through the code with the debugger, to see where the selection flag may be saved? And how is this all set up? With an extra cursor and a relation between the grid recordsource and it?

The last row being shown in a grid reminds me of a very common error to scan through a table and forget to GO TOP or LOCATE at the end, so the current record is the top record. It's very natural to scan through all data when you need to add an extra record in an extra cursor for every record in the main table, and then you forget to go back to the top again. Then the last row is showm and you need to scroll back.

And if this extra cursor isn't a cursor, but a dbf is generated if it doesn't exists and reused, if it does, this would perfectly explain why the last selection comes back. If you don't want that, change the mechanism, but first you have to understand what happens. There surely must be some extra workarea used, as the logical field is not part of the main table and not added there, it must be in an extra dbf or cursor. The grid can always only display fields of workareas, there are no grid cell objects that can have an empty value (0 or .f. for checkboxes), there must be an extra field in some workarea that feeds this column, that's the way grids work.

Chriss
 
The code that I publish in this post is the only thing that the init of the form has. I only work with the packages table in that form
 
There is more, likely inherited. It won't show up in the form, but there is no extra column without a cursor or extra dbf, impossible.

Chriss
 
Oscar, you can help us to help you if your carefully read our replies and answer our questions. I have twice asked you for the ControlSource of the first column. This might not be the solution to the problem, but it could at least help us understand the situation.

And another point: You say the value is maintained after closing and re-opening the form. But is the value also maintained after closing and re-opening the table - given that it's possible to close a form without closing all of the tables used by the form?

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
checkbox1 and the select column only exist in the grid, it does not exist in the table. for that reason I omit the line Thisform.packages.column1.ControlSource.

I'm trying a solution, add this code

thisform.Packages.Column1.Check1.caption=''
If thisform.Packages.Column1.Check1.Value = 1
thisform.Packages.column1.check1.Value=0
endif

before this line
Thisform.packages.column2.ControlSource.

It works partially for me, if I select row two and close the form. when reopening the form row two is still selected
 
Again, you can't have a grid column without a table column feeding it. An unbound checkbox will just behave randomly, and surely not keep it's state.

you have to use a grid class that does add a colum. Not to the packages.dbf, but in the form of an additionl DBF or cursor that is joined "life" by a RELATioN. No extra field is necessary and packages being unmodified does not prove you have no extra data at runtime.

Ad a button to the form, and put the command SUSPEND into its Click() event.
Then open up the datasession window and see what workareas are used at that moment.

And if thats really a baseclass grid with no further code than what you do, I am not even slightly surprised by what's happening. You have to feed a colun data and make it sparse to get what you want. And then simply don't persist the state to have all checkboxes blank next time.

but that you get to a saved state alone proves you're not knowing hat happens on top of what YOU programmed. There is more to it, there must be more to it.

Chriss
 
So, from you are saying, it looks like Column1 has no ControlSource. (If that's right, I wish you had told us that when I first asked about it.)

In that case, there is no point in even placing the checkbox in the column. If there is no ControlSource, there is nowhere for the value of the checkbox to be saved. I can't explain the behaviour you are seeing, but equally I can't see how you can achieve anything the way things are.

Also, you new code - where you are saying: If the value of the checkbox is 1 then change it zero - also seems pointless. It's just another way of saying that the value will always be zero. But even that's not right, because when you programmatically change the value of a control within a grid column, it only affects the current record. Except that there is no current record for the checkbox, as it has no ControlSource.

Believe it or not, we really do want to help you. But we need to understand what you are trying to do. Perhaps you could explain what this whole thing is meant to achieve - and especially why you are placing the checkbox in the grid.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
It's not a new thing to use a checkbox in a grid, but what you see is just the result of a sparse column only showing the actual checckbox in one row. And there only is one checkbox, for the whole column, it can only have differetn values for all rows when there is a column it's bound to.

Make that column a sparse=.t. column, and you'd see checking the checkbox checks it in all rows, if you refresh the form. or it unchecks all rows. You need an extra column, and that can be in another dbf or cursor, instead of adding to packages, but it has to be preapred and exist. Otherwise you just have a nonsense column,

Chriss
 
I tried using if which is the active row but it didn't work either

Code:
nCurrentRow = Thisform.Packages.ActiveRow 
 if nCurrentRow <> 0   
    If thisform.Packages.Column1.Check1.Value = 1 
      thisform.Packages.column1.check1.Value=0
      endif 
  endif

changing the sparse =.t. property of column1 was the only thing that fixed the behavior of the checkbox
 
That won't do what you thought it does. Activerow does not compare to a recno or the id of a record. You don't use that for remembering your position or checking whether you're in the active row.

Notice the grid colums only have one control each, these controls are drawn repeatedly, when sparese is .f., the default .t. means in every row but the currently selected a standard grid textno is drawn. For the usual case of a textbox in a column that means almost no difference between sparse .t. or .f., but whenever you want to use any other control, then it has to be an automatism to also set the columns sparse .f.

Aside of that the rows are populated by controlsource of the columns, a column without a controlsource has no value, that's why both Mike and I repeated the fact you need a controlsource for every column, also an extra column that's not corresponding to a field of your recordsource.

I don't even understand what the code you now posted should do, react to a click? a checkbox toggles from checked to unchecked automatically without any code. All you need to set is a controlsource. That's the most important setting of a data driven control, no matter if within a grid or not. Controls without controlsource (or recordsource/rowsource in some controls) are only of rare use, and within the grid of no use, really.

Chriss
 
The only mystery still unsolved is why the state persists. But I won't be surprised if there's more code you didn't show yet. you know you can easily show all (but inherited) code of a form, by opening it in the class browser (yes, you can open forms in the class browser, too, not just classes or class libraries) and then using the toolbar button "view class code"

The resulting code sometimes does not work standalone, especially when grids are involved. It's mainly there to give a code overview and to do version control with the source code equivalent to binary files.
For showing what you have, it's fine though, I could even point out where you used non-native classes that in this case I would assume would be a grid class with a multiselect feature.

Chriss
 
Just 2 more cents:

If you do this column for selecting one column then there is no need for it, simply clicking a row already makes it the current record in the workarea that is the grid recordsource. You therefore already have a simple selection of a single record.

It's not unusual to have a checkbox in a grid for multi-select, but then the easier to use control is a listbox with its multiselect property you simply set 1.

And then even if you make multiselect in a grid working, if a user selects from a large set of data and the multiple selections are on multiple pages of the data, the overview of what is selected is missing. then it's better for the user to be able to doubleclick a record to add it into a separate grid or listbox showing just the selected rows, which are removed from the selectable rows. VFP has a sample of that called mover control, made of two listboxes and a few buttons to select one/all and deselect one/all items. You may already have used such a composition of controls, as the form wizard and view wizards and builders make use of that all the time. It takes more area, but is more practical for the user than multiselect listboxes or grids.

Chriss
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top