Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

saving delphi routines and memory

delphi

  • Please log in to reply
10 replies to this topic

#1 ployo60

ployo60

    CC Regular

  • Just Joined
  • PipPipPip
  • 28 posts

Posted 06 February 2011 - 08:02 AM

This question is about being able to save routines, and being able to select them from a list…. When you select one it knows what to link where etc.

Say I want to create a routine in a Delphi form. And I want to create several different ones. They wont be exactly the same but some might be similar. I want to know how you can save things in Delphi and when you close or terminate the application they will remain remembered when you reopen it. I have no idea where to start and how to work this. Any help would be great. Just a hint or a direction, maybe a website with more info or even examples. I’m going to try to give a simpler description below about how it would look on the form…. Just for the idea and I think if I understand this then it would be enough, or a good start at least.

The form will contain a list box a save button and 4 different edit boxes. Lets say I type in edit1;1 and edit2;2 and edit3;3 and edit4;4. Then click the save button and it remembers these 4 value to each edit box and lets say saves in under the value in the list box of ≔edit1.text + ‘to’ + edit4.text. Hope it makes sense so far and then I type in the edit boxes everything the wrong way around. edit1;4 and edit2;3 and edit3;2 and edit4;1. And click save button and it does that again (≔edit1.text + ‘to’ + edit4.text) into the list box. Then I want to close the application. Open it again and still have this in there and still be able to add more of these odd samples….

Can anyone help me?
  • 0

#2 Luthfi

Luthfi

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1320 posts
  • Programming Language:PHP, Delphi/Object Pascal, Pascal, Transact-SQL
  • Learning:C, Java, PHP

Posted 06 February 2011 - 09:06 AM

You can always store these values into a text file upon saving and load upon starting the applications. Perhaps and .ini file would make it easier, since with .ini file you could store those values like this:

Edit1=1
Edit2=2
Edit3=3
...

  • 0

#3 ployo60

ployo60

    CC Regular

  • Just Joined
  • PipPipPip
  • 28 posts

Posted 06 February 2011 - 09:26 AM

yeah but i think i want things more complicated.... I really want to be able to save something, open it and apply it..... Its hard to explain, but saving a routine is what i want to do.....
  • 0

#4 ployo60

ployo60

    CC Regular

  • Just Joined
  • PipPipPip
  • 28 posts

Posted 06 February 2011 - 12:08 PM

Ok what if I explain it differently..... I place the followin things on a form: 4x Tedit and x Tbutton and 1x TListBox. One Tedit you place on the left and on the other 3 on the right underneath eachother. The button is used to create a new 'link' from the first Tedit to either one of the 3 and you can select them from listbox. What the user will be doing is selecting one of the 3 Tedit boxes and this is the one Tedit1 on the left will be linked too. This gets saved in the listbox under any name, doesnt matter.... The user will do this for the 2 other edit boxes as well..... This way he/she can select the from the listbox which connection is prefered. I am not interested in the code of selecting or edit boxes linking. that can just easily be done with Edit1.text:=Edit2.Text, im sure we know that. but how to create that saving option is what i am interested in. and that the form remembers it after closing it and reopening it. Its difficult to explain..... but i suppose its like recording a macro in excel....
  • 0

#5 Luthfi

Luthfi

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1320 posts
  • Programming Language:PHP, Delphi/Object Pascal, Pascal, Transact-SQL
  • Learning:C, Java, PHP

Posted 06 February 2011 - 01:58 PM

Sorry, I am not sure that I can follow you. Tried to comprehend your post a few time, I still don't understand. Maybe you can try to explain what is you try to accomplish, I mean try to explain the basic problem.
  • 0

#6 ployo60

ployo60

    CC Regular

  • Just Joined
  • PipPipPip
  • 28 posts

Posted 06 February 2011 - 02:19 PM

Sorry, its difficult to explain. Lets me try again. Ok, first of all, I want my form to remember the thing I save to it.... So when I close it and reopen it, things will still be there. Sort of like autosave i suppose. That is one of the things I need to work.

Secondly, I sort of want to create a profile. In a list box I can select that profile. Lets just connect it to the previous thread. but we do it differently. We through 2 editboxes and two possible selecting points in the listbox. One will connect to edit1 and the other to edit2. But I want to know how you can do this on the form not in code. So saying with a button 'new edit connection' you click it, and you select the new edit box on the form and it automatically adds an extra line to listbox. does that help?
  • 0

#7 Luthfi

Luthfi

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1320 posts
  • Programming Language:PHP, Delphi/Object Pascal, Pascal, Transact-SQL
  • Learning:C, Java, PHP

Posted 06 February 2011 - 02:34 PM

Sorry, its difficult to explain. Lets me try again. Ok, first of all, I want my form to remember the thing I save to it.... So when I close it and reopen it, things will still be there. Sort of like autosave i suppose. That is one of the things I need to work.


Well, I think you already know the answer. Just save the form when it is closing and load the saved data when it is newly created. You can choose to use custom code to save specific information only, or write a "generic" code to store any information of any kind of components owned/displayed in a given form. For the latter approach you may want to use a descendant of TStream (like TFileStream) and use WriteComponent and ReadComponent methods. Or if you want to dig deeper and write your own more efficient code you want to check with RTTI (run time type information). For RTTI you must look into TypInfo unit.


...this on the form not in code...


Sorry, you can't do that without writing code. But you can write the code once and reuse it anywhere.
  • 0

#8 ployo60

ployo60

    CC Regular

  • Just Joined
  • PipPipPip
  • 28 posts

Posted 07 February 2011 - 07:57 AM

here I emailed this to a friend of mine who said i was unclear as well. thought it might be better for you as well......

Ok I am going to rewrite my question to make more sense. My questions has two parts.
Part 1: See on my form I have a simple routine. I have a edtmain and 4 other edit(edt1,edt2,edt3,edit4). I have found the code to be able recognize the selected edit box by mouse. What I want to do, is great a list box a newlink button and a edit button. When the new link button is clicked I want this code to kick in and I want from that point the form to recognize or “record” which edit box I select by mouse. First I will select edtmain and then one of the other edt1,2,3 or 4. When this is done it automatically stops “recording” and places the name of the edt1,2,3 or 4 in the list box. I can repeat this step another 3 times for the other edit boxes. So in the end they are placed in the list box. Maybe all of them maybe not. Then what I want the program to do is to recognize that when I click one of the list box elements that I have just created and know that edtmain is connected to the one that was selected when I created the link. And by creating a link it just means that when I type in edtmain it types it in edt1,2,3 or 4 as well. It’s like recording I suppose. Like macro for MS excel. The Edit button will allow me to redo the selected list box element.
Part 2: How are you able to create a form (application) that will open the same as you closed it.

I'm now going to see if i can understand what you wrote..... thx
  • 0

#9 Luthfi

Luthfi

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1320 posts
  • Programming Language:PHP, Delphi/Object Pascal, Pascal, Transact-SQL
  • Learning:C, Java, PHP

Posted 09 February 2011 - 02:28 AM

On Part1, you have to create a simple scripting scheme. Whenever a user performs an action on a control, the event handler of that control automatically insert a line of your script. For example, when start recording you clear your script variable or object (i recommend to use a TStringList to hold the script). When user clicks on Edit1, its OnMouseClick will add this line the the script.

Edit1.Click;


When the user click on Button2, its OnMouseClick would add:

Button2.Click;


They do this until the user stop the recording. Upon stopping you can choose to save the script to a file or just leave it in memory.

Now you can play the script whenever you want. The logic is like this (if we use the example format above). For each line of the script you have to find corresponding control. For "Button2.Click;" for example, you have to find control named Button2, and make it to fire click event (call Button2.Click is fine for buttons).

If you want, you can also add time stamps in the beginning of each line, and play the line accordingly. With time stamping you can achieve the exact timing when recording the script.

About part2, I believe I have provided the one and only answer, ie. save the state to a file and load it later. Please check my previous posts.
  • 0

#10 ployo60

ployo60

    CC Regular

  • Just Joined
  • PipPipPip
  • 28 posts

Posted 10 February 2011 - 05:14 AM

thx, i got that too work. does this work the same way???

if I'm going to place the following elements on the form: 2 listboxes(with each 3 lines, in the first listbox: wood, plastic and glass. in the second listbox: tree,cup,window.) Now I want to link the correct ones, they are in order here, but what is they were not. In a table or in a memory of the application which is not visible on the form I want to link them. Then if i were to put two edit boxes on the form as wel and I type in the first one wood or tree, it places the other one in the other edit box. So in a way I suppose you are creating a table which knows which one correcsponds with which but also looksup up when you type in edit box. hope that makes sense
  • 0

#11 Luthfi

Luthfi

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1320 posts
  • Programming Language:PHP, Delphi/Object Pascal, Pascal, Transact-SQL
  • Learning:C, Java, PHP

Posted 13 February 2011 - 12:46 AM

[quote name='ployo60']thx, i got that too work. does this work the same way???[QUOTE]

The same way with what?

[QUOTE]if I'm going to place the following elements on the form: 2 listboxes(with each 3 lines, in the first listbox: wood, plastic and glass. in the second listbox: tree,cup,window.) Now I want to link the correct ones, they are in order here, but what is they were not. In a table or in a memory of the application which is not visible on the form I want to link them. Then if i were to put two edit boxes on the form as wel and I type in the first one wood or tree, it places the other one in the other edit box. So in a way I suppose you are creating a table which knows which one correcsponds with which but also looksup up when you type in edit box. hope that makes sense[/QUOTE]

Sorry, you confused me here. But it looks like you want to find solution for recognizing controls from the script? If so, your current approach is too complex given that it's still in early stage and you still don't have to recognize controls created on the fily (by code). Since the controls you want to recognize are all created in design time, you can simply query all the component names of the form. See the following code snippet (it's to find a specific control name, i.e. Button1, you should modify it to make it be able to find any name you supply).

  for i := 0 to Form1.ComponentCount-1 do
    if Form1.Components[i].Name = 'Button1' then
    begin
      ShowMessage('Button1 is found')
      Exit;
    end;
    ShowMessage('Button1 is not exist in this form')

  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download