Delphi Grid Visible Item - delphi

HI all,
I am working with Delphi 7. I am facing a problem with Grid.
My Grid having 100 rows, I am appending some more after that. For example, I am selected item is on 1oth. The grid shows 20 items on screen at a time. I scrolled the grid to downward. I reached last one. Here grid's Itemindex= 10; Please note the selected item is not showing on the visible window. When I adds the item, the grid refresh and moving to show 10th item.
I don't want to do this.
My requirement is When Adding new rows, Screen should remain same, as shown last time.
Expecting quick reply.
Thanks and Regards,
VIJESH V.NAIR
System Analyst.
Delhi, India.

Before adding an item bookmark the current row of your table that correspond to dbgrid.
Afer adding an item goto your bookmark
a sample for working with TBookmark:
(you can replace clientdataset1 with your tableName like table1)
unit MainForm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, DBClient, ExtCtrls, ActnList, Grids, DBGrids,
DBCtrls;
type
TfrmMain = class(TForm)
DataSource1: TDataSource;
pnlClient: TPanel;
pnlBottom: TPanel;
btnFirst: TButton;
btnLast: TButton;
btnNext: TButton;
btnPrior: TButton;
DBGrid1: TDBGrid;
ClientDataSet1: TClientDataSet;
btnSetRecNo: TButton;
DBNavigator1: TDBNavigator;
btnGetBookmark: TButton;
btnGotoBookmark: TButton;
procedure FormCreate(Sender: TObject);
procedure btnNextClick(Sender: TObject);
procedure btnLastClick(Sender: TObject);
procedure btnSetRecNoClick(Sender: TObject);
procedure btnFirstClick(Sender: TObject);
procedure btnPriorClick(Sender: TObject);
procedure btnGetBookmarkClick(Sender: TObject);
procedure btnGotoBookmarkClick(Sender: TObject);
private
{ Private declarations }
FBookmark: TBookmark;
public
{ Public declarations }
end;
var
frmMain: TfrmMain;
implementation
{$R *.dfm}
procedure TfrmMain.FormCreate(Sender: TObject);
begin
ClientDataSet1.LoadFromFile('C:\Employee.cds');
end;
procedure TfrmMain.btnFirstClick(Sender: TObject);
begin
ClientDataSet1.First;
end;
procedure TfrmMain.btnPriorClick(Sender: TObject);
begin
ClientDataSet1.Prior;
end;
procedure TfrmMain.btnNextClick(Sender: TObject);
begin
ClientDataSet1.Next;
end;
procedure TfrmMain.btnLastClick(Sender: TObject);
begin
ClientDataSet1.Last;
end;
procedure TfrmMain.btnSetRecNoClick(Sender: TObject);
var
Value: string;
begin
Value := '1';
if InputQuery('RecNo', 'Enter Record Number', Value) then
ClientDataSet1.RecNo := StrToInt(Value);
end;
procedure TfrmMain.btnGetBookmarkClick(Sender: TObject);
begin
if Assigned(FBookmark) then
ClientDataSet1.FreeBookmark(FBookmark);
FBookmark := ClientDataSet1.GetBookmark;
end;
procedure TfrmMain.btnGotoBookmarkClick(Sender: TObject);
begin
if Assigned(FBookmark) then
ClientDataSet1.GotoBookmark(FBookmark)
else
ShowMessage('No bookmark set!');
end;
end.

Related

For loop continue going after reaching goal. Delphi

This issue appears only with numbers, bigger, then 12 including.
Those two pictures captured in one time. How it is even possible?
For loop must go from 0 to 12-1=11, doesn't it?
Nevertheless, when I use while loop instead, it works fine.
Is it my fault or Delphi's?
P.S. Code down bellow.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids;
type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
Button1: TButton;
Edit1: TEdit;
Button2: TButton;
Label1: TLabel;
Button3: TButton;
Label2: TLabel;
Label3: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure StringGrid1KeyPress(Sender: TObject; var Key: Char);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
n:Integer;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject); //Button, that sets array length
var
i, index:Integer;
begin
val(Edit1.Text, n, index);
if(index<>0) then
begin
ShowMessage('Wrong number');
Edit1.Clear();
exit;
end;
StringGrid1.ColCount:=n;
for i:=0 to n-1 do
StringGrid1.Cells[i,0]:=IntToStr(i+1);
StringGrid1.SetFocus();
end;
procedure TForm1.Button2Click(Sender: TObject); //Main button
var
i, index:Integer;
a:array[0..10] of Real;
denom, sum:Real;
begin
i:=0;
sum:=0;
denom:=-1;
//that for loop from screenshot is here
for i:=0 to n-1 do
//while i<=(n-1) do
begin
Val(StringGrid1.cells[i,1], a[i], index);
if(index<>0) then
begin
ShowMessage('Wrong number with ' + IntToStr(i+1) + ' Id');
StringGrid1.Col:=i;
StringGrid1.Row:=1;
StringGrid1.SetFocus();
exit;
end;
a[i]:=a[i]/denom;
sum:=sum+a[i];
StringGrid1.Cells[i,2]:=FloatToStrF(a[i],ffFixed,5,3);
denom:=-denom*(i+2);
//Inc(i);
end;
Label2.Caption:=FloatToStrF(sum,ffFixed,5,3);
end;
//code down bellow just allow to go to another cell by pressing Enter
procedure TForm1.StringGrid1KeyPress(Sender: TObject; var Key: Char);
begin
if (Key=#13) and (StringGrid1.Col=(n-1)) then
Button2.SetFocus()
else if (Key=#13) and (StringGrid1.Col<>(n-1)) then
StringGrid1.Col:=StringGrid1.Col+1;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
Close();
end;
end.
As to answer your question of 'how is this even possible'...
In your screen, n is 12. As pointed out by Kermation, the highest index of a is 10, so when i is 11, unless you have range checking activated, when you write to a[11] (i=11) you will overwrite something else. This is in the local variable area so it might be i, for instance, or even internal variables you can't see like the limit used for the for loop, which is calculated at the start of the loop. Once you allow this to happen, pretty much anything is possible.
Of course the exact manifestation of the problem will very from one version of the compiler to another. In one version you might get away with it. in another you won't.
Array a size was smaller, then amount of cells.

Delphi - Panel & Button Click Effect

I have a CheckBox that shows and hides a Panel that has a Button & two TEdits (for entering an IP address & its port).
The problem is the Button has no effect, it stays gray, also the Panel still shows. I tried different methods, e.g. ModalResult := mrOk;, which didn't change anything.
Here is my code:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
CheckBox1: TCheckBox;
Panel1: TPanel;
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
procedure CheckBox1Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.CheckBox1Click(Sender: TObject);
begin
if CheckBox1.Checked = True then begin
Panel1.Visible := True;
end
else
if CheckBox1.Checked = False then begin
Panel1.Visible := False;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Button1: TButton;
begin
Button1 := Sender as TButton;
ShowMessage(Button1.Caption + ' Changes');
end;
end.
I hope I'm understanding your question..
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
CheckBox1: TCheckBox;
Panel1: TPanel;
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
procedure CheckBox1Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FormShow(Sender: TObject);
private
{ Private declarations }
procedure Showpanel(AShow: boolean);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Showpanel(AShow: boolean);
begin
Checkbox1.checked := AShow;
Panel1.Visible := AShow;
end;
procedure TForm1.FormShow(Sender: TObject);
begin
ShowPanel(false);
end;
procedure TForm1.CheckBox1Click(Sender: TObject);
begin
Panel1.visible := Checkbox1.Checked;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Showmessage(Button1.Caption + ' Changes');
end;
end.

Saving record to file Error 'file access denied'

When I run my code an select the save button which i created. The record doesnt save but i get an error 'file access denied'.
my code :
The code i split into 2 units MainUnit and AddTenantUnit.
I think the problem lies within the procedure at the end of the code. If you scroll down I made it clear which procedure (TAddTenantForm.SaveButtonClick).
unit MainUnit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TMainForm = class(TForm)
AddTenantButton: TButton;
procedure FormCreate(Sender: TObject);
procedure AddTenantButtonClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TTenantRecord = record
FirstName : string[20];
LastName : string[20];
end;
var
MainForm: TMainForm;
Tenant : TTenantRecord;
TenantFile : file of TTenantRecord;
implementation
uses AddTenantUnit;
{$R *.dfm}
procedure TMainForm.AddTenantButtonClick(Sender: TObject);
begin
AddTenantForm.ShowModal;
end;
procedure TMainForm.FormCreate(Sender: TObject);
begin
assignfile (TenantFile, 'Tenant.dat');
if not fileexists ('Tenant.dat')
then
begin
rewrite (TenantFile);
closefile (TenantFile)
end
{endif};
end;
end.
unit AddTenantUnit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, MainUnit, StdCtrls;
type
TAddTenantForm = class(TForm)
MainFormButton: TButton;
FirstNameLabel: TLabel;
FirstNameEdit: TEdit;
LastNameLabel: TLabel;
LastNameEdit: TEdit;
SaveButton: TButton;
ClearButton: TButton;
procedure SaveButtonClick(Sender: TObject);
procedure LastNameEditChange(Sender: TObject);
procedure ClearButtonClick(Sender: TObject);
procedure FirstNameEditChange(Sender: TObject);
procedure MainFormButtonClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
AddTenantForm: TAddTenantForm;
implementation
{$R *.dfm}
procedure TAddTenantForm.MainFormButtonClick(Sender: TObject);
begin
AddTenantForm.Close;
end;
procedure TAddTenantForm.FirstNameEditChange(Sender: TObject);
begin
Tenant.FirstName := FirstNameEdit.Text;
end;
procedure TAddTenantForm.ClearButtonClick(Sender: TObject);
begin
FirstNameEdit.Clear;
LastNameEdit.Clear;
end;
procedure TAddTenantForm.LastNameEditChange(Sender: TObject);
begin
Tenant.LastName := LastNameEdit.Text;
end;
// This is where the problem lies when I run this piece of
// code. This represents the Save button being clicked.
procedure TAddTenantForm.SaveButtonClick(Sender: TObject);
begin
assignfile (TenantFile, 'Tenant.dat');
write(TenantFile, Tenant);
closefile (TenantFile);
end;
end.
You are trying to write data into not opened file.
procedure TAddTenantForm.SaveButtonClick(Sender: TObject);
begin
assignfile (TenantFile, 'Tenant.dat');
// Rewrite(TenantFile) or Reset(TenantFile) missed here
write(TenantFile, Tenant);
closefile (TenantFile);
end;

Event OnChange doesn't fired when I call method LoadFromFile of TSynEdit.Lines

I have a project using TSynEdit. I found that when I call SynEdit1.Lines.LoadFromFile(), event OnChange doesn't be fired.
For instance:
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, SynEdit, Forms, Controls, Graphics, Dialogs,
StdCtrls;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
SynEdit1: TSynEdit;
procedure Button1Click(Sender: TObject);
procedure SynEdit1Change(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
begin
SynEdit1.Lines.LoadFromFile('unit1.pas');
end;
procedure TForm1.SynEdit1Change(Sender: TObject);
begin
Caption:=Caption + '!';
end;
end.
In above example, I created a form which have a button and a SynEdit.
How can I make event OnChange be fired when SynEdit1.Lines.LoadFromFile() was called?
If you try to assign all the events handler of a synedit, the best you can get is a notification when the editor is cleared. For example if you test the following code the form caption will be set to 'cleared' after the button click event:
uses
Classes, SysUtils, FileUtil, SynEdit, Forms, Controls, Graphics, Dialogs,
StdCtrls, LazSynEditText;
type
TSynEditEx = class helper for TSynEdit
function getTextBuffer: TSynEditStrings;
end;
TForm1 = class(TForm)
Button1: TButton;
SynEdit1: TSynEdit;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
procedure textCleared(sender: TObject);
procedure textHistoryModified(sender: TObject);
procedure textBuffChanged(sender: TObject);
procedure textLineChanged(sender: TObject);
procedure textEditAction(sender: TObject);
procedure textModdChanged(sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
function TSynEditEx.getTextBuffer: TSynEditStrings;
begin
exit(TextBuffer);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
SynEdit1.getTextBuffer.AddNotifyHandler(senrCleared, #textCleared);
SynEdit1.getTextBuffer.AddNotifyHandler(senrUndoRedoAdded, #textHistoryModified);
SynEdit1.getTextBuffer.AddNotifyHandler(senrTextBufferChanged, #textBuffChanged);
SynEdit1.getTextBuffer.AddNotifyHandler(senrLineChange, #textLineChanged);
SynEdit1.getTextBuffer.AddNotifyHandler(senrModifiedChanged, #textModdChanged);
end;
procedure TForm1.textModdChanged(sender: TObject);
begin
Caption := Caption + ' ModdChanged';
end;
procedure TForm1.textEditAction(sender: TObject);
begin
Caption := Caption + ' EditAction';
end;
procedure TForm1.textLineChanged(sender: TObject);
begin
Caption := Caption + ' LineChanged';
end;
procedure TForm1.textBuffChanged(sender: TObject);
begin
Caption := Caption + ' BuffChanged';
end;
procedure TForm1.textCleared(sender: TObject);
begin
Caption := Caption + ' Cleared';
end;
procedure TForm1.textHistoryModified(sender: TObject);
begin
Caption := Caption + ' HistoryModified';
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
SynEdit1.Lines.LoadFromFile('unit1.pas');
end;
So the only solution is to subclass TSynEdit and introduce a custom LoadFromFile method and then trigger the onChange event inside. AFAIK, based on a short investigation, this is the only way, a bit like this:
uses
Classes, SysUtils, FileUtil, SynEdit, Forms, Controls, Graphics, Dialogs,
StdCtrls, LazSynEditText;
type
TSynEditEx = class helper for TSynEdit
procedure LoadFromFile(const aFilename: string);
end;
TForm1 = class(TForm)
Button1: TButton;
SynEdit1: TSynEdit;
procedure Button1Click(Sender: TObject);
procedure SynEdit1Change(Sender: TObject);
private
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
procedure TSynEditEx.LoadFromFile(const aFilename: string);
begin
Lines.LoadFromFile('unit1.pas');
if assigned(onChange) then
onChange(self);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
SynEdit1.LoadFromFile('unit1.pas');
end;
procedure TForm1.SynEdit1Change(Sender: TObject);
begin
caption := caption + '!';
end;
In the real world you wouldn't use a class helper but you'd rather subclass, but I think you should get the idea.

How To Use RxChar ComPort in another form

I have problem with delphi code... I have code:
MAIN FORM
unit MainForm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, CPort, Menus, ComObj, StdCtrls;
type
TMainForm = class(TForm)
MainMenu1: TMainMenu;
Berkas1: TMenuItem;
Alat1: TMenuItem;
erminal1: TMenuItem;
ComPort1: TComPort;
Button1: TButton;
Memo1: TMemo;
Button2: TButton;
procedure erminal1Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure ComPort1RxChar(Sender: TObject; Count: Integer);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
MainForm: TMainForm;
implementation
uses
ChildForm;
{$R *.dfm}
procedure TMainForm.erminal1Click(Sender: TObject);
var
ChildForm: TChildForm;
begin
ChildForm := TChildForm.Create(Application);
ChildForm.Show;
end;
procedure TMainForm.Button1Click(Sender: TObject);
begin
ComPort1.ShowSetupDialog;
end;
procedure TMainForm.ComPort1RxChar(Sender: TObject; Count: Integer);
var
ComPort: TComPort;
data: string;
begin
inherited;
ComPort := TComPort.Create(Self);
ComPort1.ReadStr(data, 5);
ChildForm.Memo1.Text := ChildForm.Memo1.Text+''+data+'';
end;
end.
CHILD FORM:
unit ChildForm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComObj;
type
TChildForm = class(TForm)
Memo1: TMemo;
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
ChildForm: TChildForm;
implementation
uses
MainForm;
{$R *.dfm}
procedure TChildForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caFree;
end;
procedure TChildForm.Button1Click(Sender: TObject);
begin
MainForm.ComPort1.Open;
end;
end.
I want to show data input from my device to memo in child form. I put the comport component in main form. But when I run the program, it says:
Project Data.exe raised exception class EAccessViolation with message 'Access violation at address 00466051 in module 'Data.exe'. Read of address 000002F8'. Process stopped. Use Step or Run to continue.
How can i solve the problem?
There are many problems with your code as mentioned in the comments.
To make a better implementation of your parent/child form interaction with the comport component,
do as follows:
Create a TDataModule (ex: DataModule1), put the comport component there.
Now you can access the comport component from the main form and the child form.
Add a private method to your child form:
procedure TChildForm.ComPort1RxChar(Sender: TObject; Count: Integer);
var
data: string;
begin
DataModule1.ComPort1.ReadStr(data, 5);
Self.Memo1.Text := Self.Memo1.Text+''+data+'';
end;
When you open the comport in the child form, set the comport OnRxChar event to your TChildForm.ComPort1RxChar method.
In the TChildForm.OnClose event, set the comport OnRxChar event to nil and close the comport.

Resources