1 (edited by AD1408 2020-12-16 15:59:26)

Topic: Moving panels on a form

Hi all,


Is it possible to move panels on a form with mouse using script?
If so, could you please apply to attached sample project.

Post's attachments

Attachment icon Sample21.zip 3.11 kb, 218 downloads since 2020-12-16 

Adam
God... please help me become the person my dog thinks I am.

Re: Moving panels on a form

AD1408 wrote:

Hi all,


Is it possible to move panels on a form with mouse using script?
If so, could you please apply to attached sample project.

var
move:boolean;
x0,y0:integer;

procedure OnMouseDown (Sender: TObject; MouseLeft, MouseRight, MouseMiddle: boolean; Shift, Alt, Ctrl: boolean; X, Y: Integer);
begin
  move:=true;
  x0:=x; //запоминаем начальные координаты
  y0:=y; //запоминаем начальные координаты
end;

procedure OnMouseUp (Sender: TObject; MouseLeft, MouseRight, MouseMiddle: boolean; Shift, Alt, Ctrl: boolean; X, Y: Integer);
begin
  move:=false;
end;

procedure OnMouseMove (Sender: TObject; Shift, Alt, Ctrl: boolean; X, Y: Integer);
begin
  if move then begin
   if (TdbLabel(Sender).Name='Label1')  or (TdbPanel(Sender).Name='Panel2') then begin
      Form1.Panel1.Left:=Form1.Panel1.Left+x-x0; // Изменяем позицию левого края
      Form1.Panel1.Top:=Form1.Panel1.Top+y-y0; // Изменяем позицию верхнего края
   end;

   if (TdbLabel(Sender).Name='Label2')  or (TdbPanel(Sender).Name='Panel4') then begin
      Form1.Panel3.Left:=Form1.Panel3.Left+x-x0; // Изменяем позицию левого края
      Form1.Panel3.Top:=Form1.Panel3.Top+y-y0; // Изменяем позицию верхнего края
   end;

  if (TdbLabel(Sender).Name='Label3')  or (TdbPanel(Sender).Name='Panel6') then begin
      Form1.Panel5.Left:=Form1.Panel5.Left+x-x0; // Изменяем позицию левого края
      Form1.Panel5.Top:=Form1.Panel5.Top+y-y0; // Изменяем позицию верхнего края
   end;

   if (TdbLabel(Sender).Name='Label4')  or (TdbPanel(Sender).Name='Panel8') then begin
      Form1.Panel7.Left:=Form1.Panel7.Left+x-x0; // Изменяем позицию левого края
      Form1.Panel7.Top:=Form1.Panel7.Top+y-y0; // Изменяем позицию верхнего края
   end;
  end;
end;
Post's attachments

Attachment icon Sample21.rar 4.09 kb, 221 downloads since 2020-12-16 

3 (edited by AD1408 2020-12-17 11:33:46)

Re: Moving panels on a form

Hi sibprogsistem,


Great stuff.... Thank you very much... Truly appreciated.


How could we restrain move into form edges.. In other word user cannot move panels outside the edges of the form?
Plus how can I save moved panels position so that next time application run shows last set position?


Once again thanks for your kind help......

Adam
God... please help me become the person my dog thinks I am.

4 (edited by brian.zaballa 2020-12-17 14:58:31)

Re: Moving panels on a form

AD1408 wrote:

Hi sibprogsistem,


Great stuff.... Thank you very much... Truly appreciated.


How could we restrain move into form edges.. In other word user cannot move panels outside the edges of the form?
Plus how can I save moved panels position so that next time application run shows last set position?


Once again thanks for your kind help......

One way I can think of is to save its top and left in database.

Here, I made some modification in sample21 with regards to your question.

Post's attachments

Attachment icon Sample21_modified.zip 497.95 kb, 248 downloads since 2020-12-17 

brian

Re: Moving panels on a form

Hi Brian,


Thank you very much.....
Truly appreciated.........

Adam
God... please help me become the person my dog thinks I am.

Re: Moving panels on a form

Hi Brian,

I'm getting an error message:
"table settings has 97 columns but 3 values were supplied"
I have 96 fields in Settings db table


when trying to apply the following script - Using your script:

procedure Form1_OnShow (Sender: TObject; Action: string);
var
SData : TDataSet;

begin
SQLQuery('SELECT * FROM settings LIMIT 501', SData);
if Not SData.EoF then begin
       Form1.mainPanel501.Top := SData.FieldByName('mainPanel501Top').asInteger;
       Form1.mainPanel501.Left := SData.FieldByName('mainPanel501Left').asInteger;
   end else SQLExecute('INSERT INTO `settings` VALUES (501,501,501)');

SQLQuery('SELECT * FROM settings LIMIT 502', SData);
if Not SData.EoF then begin
       Form1.mainPanel502.Top := SData.FieldByName('mainPanel502Top').asInteger;
       Form1.mainPanel502.Left := SData.FieldByName('mainPanel502Left').asInteger;
   end else SQLExecute('INSERT INTO `settings` VALUES (502,502,502)');

SQLQuery('SELECT * FROM settings LIMIT 503', SData);
if Not SData.EoF then begin
       Form1.mainPanel503.Top := SData.FieldByName('mainPanel503Top').asInteger;
       Form1.mainPanel503.Left := SData.FieldByName('mainPanel503Left').asInteger;
   end else SQLExecute('INSERT INTO `settings` VALUES (503,503,503)');
end;
Adam
God... please help me become the person my dog thinks I am.

7 (edited by brian.zaballa 2020-12-19 07:19:26)

Re: Moving panels on a form

AD1408 wrote:

Hi Brian,

I'm getting an error message:
"table settings has 97 columns but 3 values were supplied"
I have 96 fields in Settings db table


when trying to apply the following script - Using your script:

procedure Form1_OnShow (Sender: TObject; Action: string);
var
SData : TDataSet;

begin
SQLQuery('SELECT * FROM settings LIMIT 501', SData);
if Not SData.EoF then begin
       Form1.mainPanel501.Top := SData.FieldByName('mainPanel501Top').asInteger;
       Form1.mainPanel501.Left := SData.FieldByName('mainPanel501Left').asInteger;
   end else SQLExecute('INSERT INTO `settings` VALUES (501,501,501)');

SQLQuery('SELECT * FROM settings LIMIT 502', SData);
if Not SData.EoF then begin
       Form1.mainPanel502.Top := SData.FieldByName('mainPanel502Top').asInteger;
       Form1.mainPanel502.Left := SData.FieldByName('mainPanel502Left').asInteger;
   end else SQLExecute('INSERT INTO `settings` VALUES (502,502,502)');

SQLQuery('SELECT * FROM settings LIMIT 503', SData);
if Not SData.EoF then begin
       Form1.mainPanel503.Top := SData.FieldByName('mainPanel503Top').asInteger;
       Form1.mainPanel503.Left := SData.FieldByName('mainPanel503Left').asInteger;
   end else SQLExecute('INSERT INTO `settings` VALUES (503,503,503)');
end;

That sql query is only applicable to settings table that I've created in the sample which only have 3 fields including id just to give you an idea.

that settings table I created is only intended for 1 record only to handle system settings and that insert query is only applicable if the table only have 3 fields. Safe way to represent it in your case is something like this

SQLQuery('SELECT * FROM settings LIMIT 1', SData);
if Not SData.EoF then begin
    Form1.mainPanel501.Top := SData.FieldByName('mainPanel501Top').asInteger;
    Form1.mainPanel501.Left := SData.FieldByName('mainPanel501Left').asInteger;

    Form1.mainPanel502.Top := SData.FieldByName('mainPanel502Top').asInteger;
    Form1.mainPanel502.Left := SData.FieldByName('mainPanel502Left').asInteger;

    Form1.mainPanel503.Top := SData.FieldByName('mainPanel503Top').asInteger;
    Form1.mainPanel503.Left := SData.FieldByName('mainPanel503Left').asInteger;
end else SQLExecute('INSERT INTO `settings` (id) VALUES (1)');

Note: Make sure you set all fields in your settings table to not null and have a default value 0 or 1, or 2, your choice to prevent an unexpected error.

brian

Re: Moving panels on a form

Hi Brian,
Thank you very much for your kind help..................

Adam
God... please help me become the person my dog thinks I am.