Topic: Clearing after delete...

Is there shorter way of clearing fields after deleting a record?


It's ok using .clear for few fields but when there are too many fields (i.e. over 500) writing a line for each field not an attractive proposition.


Below clears only a single field.

procedure Form1_tgSearch_Customers_OnChange (Sender: string);
begin
   Form1.EdSearch_InfoName.Clear;
end;

I have to add over 500 lines similar to
Form1.EdSearch_InfoName.Clear;


Perhaps there is a shorter way of achieving the task?

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

2 (edited by ehwagner 2017-01-26 23:07:07)

Re: Clearing after delete...

Adam, try this.

procedure Form1_Button6_OnClick (Sender: string; var Cancel: boolean);
var i : Integer;
begin
       For i := 0 to Form1.ComponentCount -1 do
       IF Form1.Components[i] is TdbEdit Then TdbEdit(Form1.Components[i]).Clear;
end;

You would place this on your delete button to clear after your delete.

Re: Clearing after delete...

ehwagner
I small correct your code.


Thanks.

Dmitry.

Re: Clearing after delete...

Thanks for the correction Dimitry.

5 (edited by AD1408 2017-01-27 18:53:00)

Re: Clearing after delete...

Thank you so much EHW and Dmitry...............................


Assuming I applied the code correctly, not all cleared after a record deleted.
Please see reference images below:
Before deleting Paul Low record
https://s28.postimg.org/7umngf68d/zzz_Test7.png
After delete:
https://s28.postimg.org/rvg3kzg25/zzz_Test8.png


Image, date, combobox fields and associated tGrid is not cleared?


-----------------------------------
ps/.
I didn't want to start a new thread for this small issue of mine.
I have a menu item to launch Lookups form and this form uses MVD v3.1 tGrid with on grid edit only.

showModal doesn't work. It needs newRecord action.

procedure MenuClick4 (Sender: string);
begin
     frmLookups.'NewRecord';
end;

the NewRecord in above script is not accepted. What's the correct word please?

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

Re: Clearing after delete...

Hello Ad1408

When you delete a customer (from Delete Button), if you want also delete all information in the panel on the right
about customer you are going to delete, add this code behind an event OnAfterClick

procedure Form1_Button8_OnAfterClick (Sender: string);
begin
     Form1.EdSearch_InfoName.Clear;
     Form1.EdSearch_InfoSurname.Clear;
     Form1.EdSearch_InfoStreet.Clear;
     Form1.EdSearch_InfoCity.Clear;
     Form1.EdSearch_InfoZip.Clear;
     Form1.dbiSearchInfoCustImage.Clear;
end;

One other point :

From form frmCustomer, get automatically focus to the first field.

procedure frmCustomer_OnShow (Sender: string; Action: string);
begin
    frmCustomer.EdName.Setfocus;
end;

JB

7 (edited by AD1408 2017-01-27 20:30:40)

Re: Clearing after delete...

Hi JB,


Thanks a lot...


I was looking for short script as in EHW script to handle many fields. Please see the post #1

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

Re: Clearing after delete...

Hello Ad1408 and ehwagner

Sorry, I've not read this post.
Your're right.
My code suits for a few fields to clear.
But in your case, if you have to clear more than thousand fields, the ehwagner's loop is more efficiently.
I used such a loop in one of my project and I suggested it on this forum.

JB

9 (edited by AD1408 2017-01-28 12:54:22)

Re: Clearing after delete...

np JB.


Yes, EHW script is good for clearing multiple fields.


If possible, it needs to clear images, checkboxes, dates and comboboxes field types too beside textboxes.


The following clears tGrid OK:

procedure Form1_tgSearch_Customers_OnChange (Sender: string);
begin
   Form1.tgSearchCustInvItems.ClearRows;
end;

To clear images, checkboxes, dates and comboboxes I have tried to add EHW script but it didn't work:

procedure Form1_Button8_OnClick (Sender: string; var Cancel: boolean);
var i : Integer;
begin
       For i := 0 to Form1.ComponentCount -1 do
       IF Form1.Components[i] is TdbEdit Then TdbEdit(Form1.Components[i]).Clear;
end;

begin
       For i := 0 to Form1.ComponentCount -1 do
       IF Form1.Components[i] is TCheckBox Then TCheckBox (Form1.Components[i]).Clear;
end;

begin
       For i := 0 to Form1.ComponentCount -1 do
       IF Form1.Components[i] is TComboBox Then TComboBox (Form1.Components[i]).Clear;
end;

begin
       For i := 0 to Form1.ComponentCount -1 do
       IF Form1.Components[i] is TDateTime Then TDateTime (Form1.Components[i]).Clear;
end;

begin
       For i := 0 to Form1.ComponentCount -1 do
       IF Form1.Components[i] is TImage Then TImage (Form1.Components[i]).Clear;
end;
Adam
God... please help me become the person my dog thinks I am.

Re: Clearing after delete...

Hello AD1408

What is the subject of your project?
This seems like a hell of a trick, so many fields to grasp

JB

Re: Clearing after delete...

Hi JB,


Jezebel from hell asked me to do some accounting app for her and she is very demanding...


On serious note, I still need a help on clearing images, checkboxes, dates and comboboxes field types as stated on my previous post #9.

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

Re: Clearing after delete...

Hello AD1408

I understand you have to clear many fields at once.

I thought that Ehwagner's proposal ist good to succeed it.

Why not gather all his scripts  in a global procedure (give it for example the name procedure GlobalDelete
triggered by an OnAfterClick event of the Delete button?

JB

Re: Clearing after delete...

Adam,  I understand what you are asking. Unfortunately I don't have an answer for the other field types clearing in the For loop. I am hoping Dimitry provides an answer. I am interested in knowing it also.

14 (edited by AD1408 2017-01-31 12:24:30)

Re: Clearing after delete...

Hi EHW,


Thanks for your kind help...... Always appreciated.


Lets hope Dmitry provides the remaining solution for clearing remaining field types after delete.

If possible, it needs to clear images, checkboxes, dates, memo and comboboxes field types too

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

Re: Clearing after delete...

Hi Dmitry,


Is there any solution please?


AD1408 wrote:

Hi EHW,


Thanks for your kind help...... Always appreciated.


Lets hope Dmitry provides the remaining solution for clearing remaining field types after delete.

If possible, it needs to clear images, checkboxes, dates, memo and comboboxes field types too

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

Re: Clearing after delete...

AD1408
A function to clear all components on form:

procedure ClearAllComponents(Form: TAForm);
var
    i,c : Integer;
begin
    c := Form.ComponentCount - 1;
    For i := 0 to c do
    begin
       IF Form.Components[i] is TdbEdit Then TdbEdit(Form.Components[i]).Clear;
       IF Form.Components[i] is TdbEditCount Then TdbEditCount(Form.Components[i]).Clear;
       IF Form.Components[i] is TdbMemo Then TdbMemo(Form.Components[i]).Clear;
       IF Form.Components[i] is TdbComboBox Then TdbComboBox(Form.Components[i]).dbItemID := -1;
       IF Form.Components[i] is TdbCheckBox Then TdbCheckBox(Form.Components[i]).State := TdbCheckBox(Form.Components[i]).dbDefaultState;
       IF Form.Components[i] is TdbDateTimePicker Then TdbDateTimePicker(Form.Components[i]).Checked := False;
       IF Form.Components[i] is TdbImageDataBase Then TdbImageDataBase(Form.Components[i]).Clear;
       IF Form.Components[i] is TdbMonthCalendar Then TdbMonthCalendar(Form.Components[i]).Date := now;
       IF Form.Components[i] is TdbStringGridEx Then TdbStringGridEx(Form.Components[i]).ClearRows;
       IF Form.Components[i] is TdbFileToDatabase Then
       begin
           TdbFileToDatabase(Form.Components[i]).Text := '';
           TdbFileToDatabase(Form.Components[i]).dbFileName := '';
       end;
    end;
end;

How to use, example:

procedure frmAbonent_Button1_OnClick (Sender: string; var Cancel: boolean);
begin
    ClearAllComponents(frmAbonent);
end;
Dmitry.

Re: Clearing after delete...

Thanks Dimitry. As I'm sure you are quite aware, software development can make one feel stupid. Sometimes you can't see the forest for the trees. I had your same "If" statements in my routine but it wasn't working. It took me a minute to figure out why yours worked and mine did not. As it turns out I forgot to place the "Begin and End" combination in my "For loop".  I should have known since I was not getting a syntax error. Thanks for opening my eyes (LOL) and helping Adam too.

18 (edited by AD1408 2017-01-31 23:30:30)

Re: Clearing after delete...

Thank you so much Dmitry............


I have multiple tabs on form1 with eact tab having tGrid record listing and details of selected grid.


I used just the following to test first tab on form1:

procedure Form1_Button48_OnClick (Sender: string; var Cancel: boolean);
begin
    ClearAllComponents(Form1);
end;

However, it cleared everything on the form1 including unselected tGrid records?
After clearing everything info dialog says "No record selected to delete."


I have c/p your A function to clear all components on form script as it's and after added the above script.
I'm sure that I making a mistake but cannot find it:


procedure ClearAllComponents(Form: TAForm);
var
    i,c : Integer;
begin
    c := Form.ComponentCount - 1;
    For i := 0 to c do
    begin
       IF Form.Components[i] is TdbEdit Then TdbEdit(Form.Components[i]).Clear;
       IF Form.Components[i] is TdbEditCount Then TdbEditCount(Form.Components[i]).Clear;
       IF Form.Components[i] is TdbMemo Then TdbMemo(Form.Components[i]).Clear;
       IF Form.Components[i] is TdbComboBox Then TdbComboBox(Form.Components[i]).dbItemID := -1;
       IF Form.Components[i] is TdbCheckBox Then TdbCheckBox(Form.Components[i]).State := TdbCheckBox(Form.Components[i]).dbDefaultState;
       IF Form.Components[i] is TdbDateTimePicker Then TdbDateTimePicker(Form.Components[i]).Checked := False;
       IF Form.Components[i] is TdbImageDataBase Then TdbImageDataBase(Form.Components[i]).Clear;
       IF Form.Components[i] is TdbMonthCalendar Then TdbMonthCalendar(Form.Components[i]).Date := now;
       IF Form.Components[i] is TdbStringGridEx Then TdbStringGridEx(Form.Components[i]).ClearRows;
       IF Form.Components[i] is TdbFileToDatabase Then
       begin
           TdbFileToDatabase(Form.Components[i]).Text := '';
           TdbFileToDatabase(Form.Components[i]).dbFileName := '';
       end;
    end;
end;


// --------->> form1 COMPANY tab
procedure Form1_Button48_OnClick (Sender: string; var Cancel: boolean);
begin
    ClearAllComponents(Form1);
end;

// --------->> form1 CUSTOMERS tab
procedure Form1_Button3_OnClick (Sender: string; var Cancel: boolean);
begin
    ClearAllComponents(Form1);
end;

// --------->> form1 PRODUCTS tab
procedure Form1_Button6_OnClick (Sender: string; var Cancel: boolean);
begin
    ClearAllComponents(Form1);
end;
Adam
God... please help me become the person my dog thinks I am.

Re: Clearing after delete...

Adam, in your case you will need to make some adjustments. In the ClearAllComponents procedure you need to remove or comment out the following line:

IF Form.Components[i] is TdbStringGridEx Then TdbStringGridEx(Form.Components[i]).ClearRows;

Keep in mind that any form in your project from here on will not clear tablegrid rows with this procedure.  In the button procedure which calls the ClearAllComponents you can place the individual tablegrid to clear.

procedure Form1_Button48_OnAfterClick (Sender: string);
begin
  ClearAllComponents(Form1);
  Form1.TableGrid1.ClearRows;  //Clear rows on selected individual tablegrids
end;

Also, notice that this procedure is setup to fire off on the button after click event. I think that's why you are getting the "No Record Selected to delete" message. You want to clear after the delete action. Clearing before the delete action means there is nothing to delete when the action takes place. Hope this helps.

Re: Clearing after delete...

Thank you so much EHW..........................................................


OnAfterClick did the trick.

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

Re: Clearing after delete...

Please, how can I use the procedure shown by Dmitry and select the fields that will not be clean?

Roberto Alencar

Re: Clearing after delete...

Add this line to also clear the new component RichEdit

IF Form.Components[i] is TdbRichEdit Then TdbRichEdit(Form.Components[i]).Clear;
@thezimguy