Freeeee 1 Posted Wednesday at 08:46 PM Newbie exploring string grid. simple program w string grind and a button. and 3 debugging labels. grid is 5 col 20 rows expanded (testing dynamic changes) to 6 col Populated with row and column titles, then populated with test data. Wanted to have a way of 'selecting' a specific row. each button push advances to the 'next' row. supposedly saving the contents of cell[1, row] and replacing it with '***' + original string in that cell. Next button push intended to restore original string and advance to next row. code for unit attached. my comments are in the code. row one (special case) works fine except the original string is not restored to the cell. should be exactly the same string I moved to a local variable 'S' on the 1st button bush but doesn't appear to be. I've read you can change the color of a single Cell in a string but haven't been able to find where I read it. changing the color would actually be better if it's possible. Unit2.pas Share this post Link to post
Renate Schaaf 75 Posted Thursday at 05:17 AM (edited) if (Switch > 1) and (switch < 20) then begin MySg.Cells [1, switch + B] := S; //replase '***' + S w just S S is a local variable. What it contains is undefined at the beginning of the procedure and independent of what you assigned to it in the previous click. Use the debugger, if you have a problem like you describe. Step through your procedure, and examine the variables. You will see why your code is not doing what you expect. The variable B has the same problem. Edited Thursday at 05:20 AM by Renate Schaaf Share this post Link to post
Freeeee 1 Posted Thursday at 09:06 PM 15 hours ago, Renate Schaaf said: if (Switch > 1) and (switch < 20) then begin MySg.Cells [1, switch + B] := S; //replase '***' + S w just S "S is a local variable. What it contains is undefined at the beginning of the procedure and independent of what you assigned to it in the previous click. " What I hear you saying is : S , a local Variable, Does not persist once the procedure is exited. It can be anything or nothing at all when you call the procedure again. That does corresponds with what the labels were telling me. I just ran into a situation where I was doing just that with indices into an array. The compiler tells me that they can NOT be global, they have to be local. so again how do you even fill an array if the indices can be any thing each time you enter a procedure. ??? This was with an {$APPTYPE CONSOLE} application where I was trying to fill an array from the keyboard. How would you fill an array with user input data.? Other than ALL at Once. First Question: Do I have to make S a global 'variable' to use it the way I'm intending. Second question: Is there reserved word to make a local variable persistent.? Maybe "Persistent"? Third question: Is a Global 'constant' changeable programmatically? example: Low and High. once they're set could you change them in a procedure? From what your saying the answer to 3 is no. Share this post Link to post
Renate Schaaf 75 Posted Friday at 07:47 AM (edited) 10 hours ago, Freeeee said: I just ran into a situation where I was doing just that with indices into an array. The compiler tells me that they can NOT be global The compiler was probably not complaining for the reason you thought it did. Without code impossible to tell. 10 hours ago, Freeeee said: How would you fill an array with user input data.? Other than ALL at Once. I don't like lengthy explanations, so here is an example to get you started: program FillTestArray; {$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils; var TestArray: array[0..3] of string; i: integer; begin try for i := 0 to 3 do begin write('Type input for ' + i.ToString +': '); readln(TestArray[i]); end; for i := 0 to 3 do begin write('TestArray['+ i.ToString +']: '+TestArray[i]); readln; end; except on E: Exception do Writeln(E.ClassName, ': ', E.Message); end; end. 10 hours ago, Freeeee said: First Question: Do I have to make S a global 'variable' to use it the way I'm intending. Make them fields of the form, that's the logical thing, since they are only used in that context. I've changed your unit, read my comments. Also, if you attach a form-unit, include the .dfm and zip it up. Makes it ever so much easier to give you an answer. unit Unit2; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Grids, Vcl.StdCtrls; type TTest = class(TForm) MySG: TStringGrid; CBCycle: TButton; procedure FormCreate(Sender: TObject); procedure CBCycleClick(Sender: TObject); procedure MySGClick(Sender: TObject); private switch, A, B: integer; //make these variables of the form S: string; procedure AdjustMySg; //make this a method of the form { Private-Deklarationen } public { Public-Deklarationen } end; //This variable should only be used for auto-creation of the form. //Never use it in your code. //If you can comment these lines, and your form-code is still error-free //you've done it right. var Test: TTest; implementation {$R *.dfm} Procedure TTest.AdjustMySg; var I : integer; begin MySg.ColCount := 6; MySg.RowCount := 21; MySg.DefaultColWidth := 100; MySg.Cells[0,0] := 'Test'; MySg.Cells[1,0] := 'TESTA'; MySg.Cells[2,0] := 'TESTB'; MySg.Cells[3,0] := 'TESTC'; MySg.Cells[4,0] := 'TESTD'; MySg.Cells[5,0] := 'TESTE'; for i := 1 to 20 do Mysg.Cells[0,i] := inttoStr(i); end; procedure TTest.CBCycleClick(Sender: TObject); begin switch := switch + A; // switch starts at zero If Switch = 1 then // switch point to last button push begin // or 'the Selected' row if B<>0 then //not first time MySg.Cells[1, switch + B]:=S; A := 1; B := -1; S := MySg.Cells[1, switch]; // S as it 'is' MySg.Cells [1, switch] := '***' + S; end; if (Switch > 1) and (switch <= 20) then // include last row begin MySg.Cells [1, switch + B] := S; //replase '***' + S w just S S := MySg.Cells [1, Switch]; // new S MySg.Cells [1, switch] := '***' + S; // show as 'selected'. end; If (Switch = 20) then begin A := -1 ; B := 1; end; end; procedure TTest.FormCreate(Sender: TObject); begin AdjustMySg; //Do any initialization here switch := 0; A := 1; B := 0; end; procedure Ttest.MySGClick(Sender: TObject); Var I, J, K : Integer; begin AdjustMySg; K := 23; with MySG do for I := 1 to ColCount - 1 do for J:= 1 to RowCount - 1 do begin K := K + 1; Cells[i, j] := IntToStr(k); end; MySG.Cells[1,1] := 'longtest'; MySg.Cells[2,1] := 'Blongtest'; MySg.Cells[3,1] := 'Clongtest'; MySg.Cells[4,1] := ' short'; MySg.Cells[5,1] := IntToStr(k); end; You could replace your unit2 by this one. In the designer you then need to attach TTest.FormCreate to the OnCreate event of the form. To the rest of your questions: No, and there are better ways to achieve what you want. When I changed from Borland-Pascal to Delphi (30 years ago) I read the manuals front to back, and then I thought I knew what I was doing (wrong). Don't get discouraged, read up on the stuff, download some simple sample projects and understand what they are doing! Renate Edited Friday at 07:50 AM by Renate Schaaf Share this post Link to post
Freeeee 1 Posted Friday at 01:06 PM thank You very much, Renate. Using the Info you gave me in the 1st post I did move the variables into global which made everything but the last push of the button going up, work. just as you did I used the B variable as the 'special case' and the whole thing worked fine. putting variables in the PFR file didn't occur to me. It's difficult to get the IDE to load toad it. I had decided that the IDE didn't 'want' you to change that. I had been editing the text version of the Form and determined the IDE definitely does not want you 'efn' around with that. However I still find it the easiest way to line up objects on the form. but if I do eff it up, I might as well start over. I get errors that I can't fix and can't figure out why they're errors. Something about hidden variable Remy said. I find working in the IDE very strange with all of the popups right over what I'm typing. I realize that the popup are so I can seleect from them what I'm typing but I could type it faster/sooner if it wasn't in the way. and the mouse clk to remove the popup moves the damned cursor where I was typing. Like you, I read the turbo pascal and early editions of Delphi (up to 5) cover to cover . My eyesight was a lot better then and the delphi 5 paper back from SAMS if I recall correctly was huge. must weigh 4 pounds The whole concept of a local variable 'going away' when you left the called procedure was never considered. We EXPECTED and counted on that Not happening. Quite an adjustment for me . It's why I've asked two or three times now, in this forum if any one has written a Delphi Cook Book with all of the reserved words explained and used in a non-trivial way. If you know of one, please let me know. If it';s a PDF file I can read it in my browser and adjust the font so I can read it. (need at least a 16 font for me) Before I get too long winded here. Thanks again for the great examples. I will study them. and learn. I can tell that my recreation of a genealogy program I wrote in Turbo Pascal in MSDos on one 22 by 80 character screen is going to be a lot harder than I thought. Share this post Link to post
Renate Schaaf 75 Posted Friday at 02:10 PM (edited) 1 hour ago, Freeeee said: putting variables in the PFR file didn't occur to me. What do you mean by that? Do you mean the .dfm-file? I certainly did not suggest any such thing! You leave that alone (until you know what you're doing:), or there's hell to pay! The variables I put into the private part of the form-class in the form-unit. Please read my example. The .dfm-file is where the designer stores all the properties of the form and the controls and components it uses. If you change anything in that the form may become unusable. 1 hour ago, Freeeee said: if any one has written a Delphi Cook Book with all of the reserved words explained and used in a non-trivial way. If you know of one, please let me know. My favorite is "Delphi in a nutshell" by Ray Lischner, a bit aged now, of course, but great as a reference. Edited Friday at 02:14 PM by Renate Schaaf Share this post Link to post
Freeeee 1 Posted Friday at 04:43 PM thanks again!!!! 🙂 I've learned more in the 1st chapter of this book that I have in 2 months of trying to make my way thru the documentation on Embarcadaro's web page. Wifi and Ebooks on Kindle are fast. Share this post Link to post
Pat Foley 54 Posted Friday at 07:59 PM Did you use Turbo pascal Programmers Library by Jamsa back then and and how about Ready-to_Run Delphi 3 Algorithms both mention using generic coding for code reuse back then should help in learning code reuse and simply reusing someone else code now. Tstringlist.Commatext may be able to lift your old code and Format('%s,%,s', ['name', Number.tostring])to feed Stringgrid.Rows[0].commaText := 'name,Number'; Share this post Link to post
Freeeee 1 Posted Friday at 10:06 PM never heard of either. Will look for them. Turbo pascal & Delphi was done on my own time. I was originally an Intel assembly language programmer starting back with the Z80 and 8086 . Among other titles. Share this post Link to post