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

set Grid cmd buttons independantly? 1

Status
Not open for further replies.

brainzebra

Programmer
Aug 7, 2002
6
US
Every month there are different CommAcct files (and dif commaccts) that need to be imported into our system. I want to use the grid control to show the different CommAccts followed by a button to allow importing that CommAcct file.
Column1 is default texbox showing the CommAcct from a recordsource, and column2 is a command button named btnImport. Each rows button should be independantly enabled based on a field in the recordsource. How do I independantly set the btnImport enabled = .t. or enabled = .f. in different rows.

I need each rows button to possibly be enabled or disabled, and also probably need different code for each rows button based on CommAcct.

* this does not do it...
select commacct, imported ;
from CommAcctTable ;
into cursor cr

with thisform.grid1
.recordsource = "cr"
.column1.controlsource = "cr.commacct"
.column2.controlsource = "cr.imported"

sele cr
nRow = 1
.setfocus
scan
.activatecell(nRow, 2)
.column2.btnImport.enabled = cr.imported
nRow = nRow + 1
endscan

endwith

 
You may be able to use something like this in the Init event of the form. I don't have a grid with a command button in it so I didn't test it but you may get the idea from this:

ThisForm.Grid1.SetAll("Enabled", "IIF(cr.imported,.T. , .F.)", "CommandButton")

Dave S.
 
.column2.btnImport.enabled = iif(cr.imported = condiction,.t., .f.)
Attitude is Everything
 
Thank you very much for your suggestions, but I tried both of them with the same result; the buttons all end up with a uniform enabled setting. If I introduce an error immediately after setting all of the buttons to their appropriate enabled state, the grid does show the buttons to be enabled and disabled appropriately (while the error dialog is displayed). As soon as I acknowlege the error, the grid buttons all take on the same enabled property. There is no other code in any event, so this is default behavior of the grid. There are 4 rows in my current test scenario. Is it because each button has the same name that they are being treated as one object as opposed to 4 independant objects? This is what I'm starting to think, in which case is it even possible to get the behavior I am looking for from the grid? I must be missing something. Any other ideas??? Please, and thank you again.
 
BrainZebra

May I suggest you create two buttons (one enabled and one disabled ), and in the grid's Init event, play with the DynamicCurrentControl to switch between buttons.
1. Modify the column that requires the command buttons. Add two command buttons as possible CurrentControls. Edit one of them to make it disabled.
2. In the grid's init use something like:
Code:
this.column2.DynamicCurrentControl="thisform.checkcontrol()"
3. Create a new method in your form (checkcontrol):
Code:
IF myCursor.TrueFalse=.t.
   return("COMMAND1") && Enabled button
ELSE
   return("COMMAND2") && Disabled button
ENDIF

And that should do it.
If you need a demo of this, give me your e-mail address.

 
mgagnon,
That worked, thanks, BUT I would really like to get away with using just one object. One reason (out of many) is I may want to set the caption for each button to reflect the type of import (or lack thereof) that may occur for the CommAcct in that row. If there were dozens of different CommAccts, this solution would require having to programatically add dozens of new buttons on the fly to the column and change each ones caption. It just doesn't seem reasonable. Everything that one button needs to know about itself and how to behave is in my record source at a row level.

What is it about the grid that causes all the rows to act like one for an object placed in a column? They are independant in terms of what is displayed in the text box bound to the recordsource. Why can an object's properties not be bound to the content in a recordsource?

The examples provided by earlier posters should have worked in my eyes, but they do not (or I am doing something wrong). The grid seems like such an elegant solution in so many ways if it wasn't for this shortcoming. I was planning on using this control throughout the project, but if i can not control the behavior of objects at a row level, I may need to look for another control.

Who can pleeeease explain this for me?
 
If it's only to put a different caption on the button, I would suggest you further expand the function I proposed to include the caption.
 
mgagnon,
Let's assume the bosses requirement is that the button is to display captions like: "IMPORT DON'S ACH FILE", "IMPORT FRANKS CREDITCARD FILE" (enabled), and "XYZ ATM FILE ALREADY IMPORTED" (disabled).

With a caption, how would you know how many buttons to add? One month there might be 4 different records, therefore requiring 4 different buttons, COMMAND1, COMMAND2, COMMAND3, COMMAND4. I would modify form, add these 4 buttons to the column, and then manually set each ones caption to be what I see in my table? Now, the next month let's say there are 30 records, requiring 30 different buttons added to the column, one for each different caption. I know this could be done programmatically, but it just seems... well, wrong.

I fear the answer to my original question is:
It is NOT possible to set the properties (caption, color, enabled, picture, etc) for the currentcontrol object in a column based on the row data of the underlying recordsource. I need to create a seperate/new object (command button in this case) to hold the unique property data of each row in my recordsource.

Please tell me I am wrong.
 
BrainZebra

I'm not sure that I understand the problem. Here is a few lines of code, that will reproduce the the form, the grid and the two buttons, is it really the amount of coding that is the problem? Your captions could be picked up from a table, that would be maintained, without changing any code.
Code:
PUBLIC var1
PUBLIC oForm1
oForm1=CREATEOBJECT("myBaseForm")
oForm1.ADDOBJECT("myGrid1","myGrid")
oForm1.myGrid1.Column2.ADDOBJECT("mySuperButton1","mySuperbutton1")
oForm1.myGrid1.Column2.ADDOBJECT("mySuperButton2","mySuperbutton2")
oForm1.SHOW(1)
DEFINE CLASS mySuperButton1 AS COMMANDBUTTON
	ENABLED = .F.
	VISIBLE = .T.
	CAPTION = "Button Disabled"
ENDDEFINE
DEFINE CLASS mySuperButton2 AS COMMANDBUTTON
	ENABLED = .T.
	VISIBLE = .T.
	CAPTION = "Button Enabled"
ENDDEFINE
DEFINE CLASS myBaseForm AS FORM
	PROCEDURE LOAD
	CREATE CURSOR myCursor (NAME c(20),TrueFalse L)
	CREATE CURSOR myCursor (NAME c(10),TrueFalse L)
	INSERT INTO myCursor (NAME,TrueFalse) VALUES ("Mike",.T.)
	INSERT INTO myCursor (NAME,TrueFalse) VALUES ("John",.F.)
	INSERT INTO myCursor (NAME,TrueFalse) VALUES ("Frank",.T.)
	GO TOP
ENDPROC

	PROCEDURE checkcontrol
	IF myCursor.TrueFalse=.T.
		var1 = "Hello"
		RETURN("mySuperbutton1")
	ELSE
		var1 = "Hello2"
		RETURN("MySuperButton2")
	ENDIF
ENDPROC

ENDDEFINE

DEFINE CLASS myGrid AS GRID
	COLUMNCOUNT = 2
	VISIBLE = .T.
	Column2.SPARSE = .F.
	column2.width = 100
	PROCEDURE INIT
	THIS.Column2.DYNAMICCURRENTCONTROL="THISFORM.CHECKCONTROL()"
ENDPROC
ENDDEFINE



 
I don't think you can dynaically bind the caption to the data in the field for the command button, I'm not sure about enabling or disabling dynamically either. I could be wrong, lord knows I've been wrong before.
Is it necessary to use a grid? You could create a form large enough to allow for 20 or 30 buttons on it. For that matter it could be sized programmatically or scrolled. Then you could add the buttons and maybe a corresponding text field for each record in the form's init event, enabling as needed.


The form size is 410 high X 500 wide. I have 15 records in the table right now, which has a structure like:
Sometext C(15) - for Comman Button caption
OtherText C(15) - for display of tetx box
ImpCommand M(10) - the procedure to do the work
Completed L(1) - completed of not, toggle enabled status

Here's the startup prg:
Code:
DO FORM ImpForm
READ EVENTS

DEFINE CLASS cmd AS COMMANDBUTTON
   HEIGHT = 20
   WIDTH = 220
   LEFT = 10

   PROCEDURE CLICK
      GOTO VAL(SUBSTR(THIS.NAME, 4))
      MyCmd = testtest.ImpCommand
      &MyCmd
   ENDPROC
ENDDEFINE
*****************************************

In the form's init:

Code:
nCurrentTop = 10
USE testtest
SCAN
   STORE 'cmd' + ALLTRIM(STR(RECNO())) TO oCurrobj
   THISFORM.ADDOBJECT(ALLTRIM(oCurrobj), 'cmd' )

   WITH THISFORM.&oCurrobj.
      .HEIGHT  = 20
      .WIDTH   = 220
      .LEFT    = 10
      .TOP     = nCurrentTop
      .VISIBLE = .T.
      &&... whatever the field name is
      .CAPTION = testtest.SomeText   
      &&... same here
      .ENABLED = !testtest.completed 

   ENDWITH
   
   STORE 'Txt' + ALLTRIM(STR(RECNO())) TO oCurrobj
   THISFORM.ADDOBJECT(ALLTRIM(oCurrobj), 'TextBox' )
   WITH THISFORM.&oCurrobj.
      .HEIGHT   = 20
      .WIDTH    = 220
      .LEFT     = 230
      .TOP      = nCurrentTop
      .VISIBLE  = .T.
      &&... whatever the field name is
      .VALUE    = testtest.OtherText
      &&... same here
      .READONLY = testtest.completed
   ENDWITH

   nCurrentTop = nCurrentTop + 20  &&..height of button
ENDSCAN


Dave S.
 
it has bee interesting to watch this tread grow. I find a lot of work and code. I guess I'm old fashion, in the odl DOS memory limits, size was importen. what ever happen to the KISS princaple.

why a button for each row???

why not one button on a form. you get the file from referencing the activerow to read the file to import.

Not expecting a reply, only my sence of humor. Attitude is Everything
 
danceman,

That's my opinion too. However, bosses requirement has a lot to do with it. I think there are a dozen 'better' ways to go about the process.

Dave S.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top