Delphi and Kylix

Moderators: pritaeas
Number of threads: 7264
Number of posts: 19073

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
How do I STORE Control values or properties in a file ? Posted by mydelphi on 20 Jul 2003 at 8:50 AM
This message was edited by mydelphi at 2003-7-20 9:13:34

This message was edited by mydelphi at 2003-7-20 9:11:35

Hi,

I'm looking for some suggestions.

I've pretty much exhausted myself of a theral search in all of my
libraries of books for any example or hints or whatever,

Mastering Delphi 3
Secrets of Delphi 2
Delphi 3 Super Bible
And several other books
Plus, the Internet (doing actual web searches)

So, here I am, asking some more questions.

Ok then, I have, yet another problem. I'm trying to STORE my controls
values into a file. I've mimiced ONLY the controls values/property
that I will end up using.

I've thought about using the TIniFle unit and save to and INI file,
and this would be simple, if all I was doing was saving ONE dementional
strings, Intergers, even checkbox values ie, .checked=boolean,
but that turned out to be a bad idea. If I have a combobox for instance,
with a list of items, say 15 of them, how am I going to store them in this
INI file structure, and then put them back into the combobox later, and
I go to retreave the complete file (control structure on my form)

I would love to just store the ALL the controls AND their values from my
form, and just recall them as they were last set, but after trying that,
using a file structure below, either my system would crash or I did not
get back my values (they did not get stored?)

Example:

Type
  TmyControls = record
                  txtTmpListLastStr: String[100]; // last highlisted string
                  cbTmpList: TCombobox;
                   chbYesNo: TCheckbox;
                    txtSize: Integer;                  
                end;

var
  F : file of TmyControls;
  myControlsRecStruc: TmyControls;
.
.
  myControlsRecStruc.txtTmpListLastStr := form1.txtTmpListLastStr;
          myControlsRecStruc.cbTmpList := form1.cbTmpList.checked;
           myControlsRecStruc.chbYesNo := form1.chbYesNo;
            myControlsRecStruc.txtSize := form1.txtSize;
.
.
  // Write the data to our file structure
  assign(f,'h:\ctrldef.ini');
    rewrite(f);
      write(f,myControlsRecStruc);
  close(f);

  // Read the data back, from our file structure, into our Controls
  assign(f,'h:\ctrldef.ini');
    reset(f);
    read(f,myControlsRecStruc);
  close(f);
.
.
  // populate our Controls from file structure here.
.
.


Not to forget that all the above are Controls on my form.

but the above presents too many problems:
1 - combobox: If I add a new item (which I will) how to they get inserted
and properly sorted. If I have a mechanism for storing the last position
or selection in the combobox, and I go and insert a new item into this
list, won't I throw off the order AND the new item ?
2 - some of my controls are mixed values. Example, there are some
Intergers, Strings (only works if I say String[nnn], NOT String) and
checkboxes. I use/store the chbYesNo.checked values.

I'm sorry for being so lenghthy, but it was difficult to explain.
I was sure I read somewhere's in one of my Books on how to do this, but I
could not find anything in my latest search.

I really hope that it's something very simple that I over looked

Thank you for any suggestions

-mydelphi




Report
Re: How do I STORE Control values or properties in a file ? Posted by zibadian on 20 Jul 2003 at 9:32 AM
: Hi,
:
: I'm looking for some suggestions.
:
: I've pretty much exhausted myself of a theral search in all of my
: libraries of books for any example or hints or whatever,
:
: Mastering Delphi 3
: Secrets of Delphi 2
: Delphi 3 Super Bible
: And several other books
: Plus, the Internet (doing actual web searches)
:
: So, here I am, asking some more questions.
:
: Ok then, I have, yet another problem. I'm trying to STORE my controls
: values into a file. I've mimiced ONLY the controls values/property
: that I will end up using.
:
: I've thought about using the TIniFle unit and save to and INI file,
: and this would be simple, if all I was doing was saying ONE dementional
: strings, Intergers, even checkbox values ie, .checked=boolean,
: but that turned out to be a bad idea. If I have a combobox for instance,
: with a list of items, say 15 of them, how am I going to store them in this
: INI file structure, and then put them back into the combobox later, and
: I go to retreave the complete file (control structure on my form)
:
: I would love to just store the ALL the controls AND their values from my
: form, and just recall them as they were last set, but after trying that,
: using a file structure below, either my system would crash or I did not
: get back my values (they did not get stored?)
:
: Example:
:
:
: Type
:   TmyControls = record
:                   txtTmpListLastStr: String[100]; // last highlisted string
:                   cbTmpList: TCombobox;
:                    chbYesNo: TCheckbox;
:                     txtSize: Integer;                  
:                 end;
: 
: var
:   F : file of TmyControls;
:   myControlsRecStruc: TmyControls;
: .
: .
:   myControlsRecStruc.txtTmpListLastStr := form1.txtTmpListLastStr;
:           myControlsRecStruc.cbTmpList := form1.cbTmpList.checked;
:            myControlsRecStruc.chbYesNo := form1.chbYesNo;
:             myControlsRecStruc.txtSize := form1.txtSize;
: .
: .
:   // Write the data to our file structure
:   assign(f,'h:\ctrldef.ini');
:     rewrite(f);
:       write(f,myControlsRecStruc);
:   close(f);
: 
:   // Read the data back, from our file structure, into our Controls
:   assign(f,'h:\def.ini');
:     reset(f);
:     read(f,myControlsRecStruc);
:   close(f);
: .
: .
:   // populate our Controls from file structure here.
: .
: .
: 

:
: Not to forget that all the above are Controls on my form.
:
: but the above presents too many problems:
: 1 - combobox: If I add a new item (which I will) how to they get inserted
: and properly sorted. If I have a mechanism for storing the last position
: or selection in the combobox, and I go and insert a new item into this
: list, won't I throw off the order AND the new item ?
: 2 - some of my controls are mixed values. Example, there are some
: Intergers, Strings (only works if I say String[nnn], NOT String) and
: checkboxes. I use/store the chbYesNo.checked values.
:
: I'm sorry for being so lenghthy, but it was difficult to explain.
: I was sure I read somewhere's in one of my Books on how to do this, but I
: could not find anything in my latest search.
:
: I really hope that it's something very simple that I over looked
:
: Thank you for any suggestions
:
: -mydelphi
:
Saving the controls into an inifile is a smart idea, since you can create a single section to hold all the values you want for a single control. The easiest way to code this is to create a descendant of a TIniFile or TMemIniFile, which adds several new methods to handle certain classes of controls. Here is an example in code.
type
  TControlsFile = class(TIniFile)
  private
    procedure LoadComboBox(Control: TComboBox);
    procedure SaveComboBox(Control: TComboBox);
    procedure LoadEdit(Control: TEdit);
    procedure SaveEdit(Control: TEdit);
  public
    procedure LoadControls(Form: TForm);
    procedure SaveControls(Form: TForm);
  end;

procedure TControlsFile.LoadComboBox(Control: TComboBox);
begin
  with Control do begin
    // first read if the list is sorted
    Sorted := ReadBool(Name, 'Sorted', true);
    // read all the items, which are automatically sorted (if sorted = true)
    Items.CommaText := ReadString(Name, 'Values', 'DefaultValues');
    // read the selected item
    ItemIndex := Items.IndexOf(ReadString(Name, 'Selected', ''));
    // add more properties as you like, by calling the correct ReadXXXX method
  end;
end;

procedure TControlsFile.LoadControls(Form: TForm);
var
  i: integer;
begin
  with Form do
    if ComponentCount > 0 then
      // loop through all the components on the form and
      for i := 0 to ComponentCount-1 do
        // and call the load routine for its class
        if Components[i] is TComboBox then
          LoadComboBox(Components[i] as TComboBox)
        else if Components[i] is TEdit then
          LoadEdit(Components[i] as TEdit);
        // Add more control types by adding more "else-if-then"s
end;

procedure TControlsFile.LoadEdit(Control: TEdit);
var
  s: string;
begin
  with Control do begin
    // read the text
    Text := ReadString(Name, 'Text', 'Some default text');
    // read the font string
    s := ReadString(Name, 'Font', 'MS Sans Serif@8@----@0@');
    // set the font properties based on the font string
    Font.Name := Copy(s, 1, Pos('@', s)-1);
    Delete(s, 1, Pos('@', s));
    Font.Size := StrToInt(Copy(s, 1, Pos('@', s)-1));
    Delete(s, 1, Pos('@', s));
    Font.Style := [];
    if s[1] = '+' then
      Font.Style := Font.Style + [fsBold];
    if s[2] = '+' then
      Font.Style := Font.Style + [fsItalic];
    if s[3] = '+' then
      Font.Style := Font.Style + [fsUnderline];
    if s[4] = '+' then
      Font.Style := Font.Style + [fsStrikeOut];
    Delete(s, 1, Pos('@', s));
    Font.Color := StrToInt(Copy(s, 1, Pos('@', s)-1));
  end;
end;

procedure TControlsFile.SaveComboBox(Control: TComboBox);
begin
  with Control do begin
    // write the properties
    WriteString(Name, 'Values', Items.CommaText);
    WriteString(Name, 'Selected', Items[ItemIndex]);
    WriteBool(Name, 'Sorted', Sorted);
    // add more properties as you like, by calling the correct WriteXXXX method
  end;
end;

procedure TControlsFile.SaveControls(Form: TForm);
var
  i: integer;
begin
  with Form do
    if ComponentCount > 0 then
      // loop through all the components on the form and
      for i := 0 to ComponentCount-1 do
        // and call the save routine for its class
        if Components[i] is TComboBox then
          SaveComboBox(Components[i] as TComboBox)
        else if Components[i] is TEdit then
          SaveEdit(Components[i] as TEdit);
        // Add more control types by adding more "else-if-then"s
end;

procedure TControlsFile.SaveEdit(Control: TEdit);
var
  s: string;
begin
  with Control do begin
    // write the text
    WriteString(Name, 'Text', Text);
    // copy the font properties into a single string
    s := '----'; // assume normal style
    if fsBold in Font.Style then // if style is bolded then
      s[1] := '+'; // change the value in the string
    if fsItalic in Font.Style then // same with italic
      s[2] := '+';
    if fsUnderline in Font.Style then
      s[3] := '+';
    if fsStrikeOut in Font.Style then
      s[4] := '+';
    // compile the complete font string
    s := Format('%s@%d@%s@%d@', [Font.Name, Font.Size, s, Integer(Font.Color)]); 
    // write that string
    WriteString(Name, 'Font', s);
  end;
end;

This example will save and load the properties of all the TComboBoxes and TEdits on a single form. The format of the resulting file will be something like this:
[Edit1]
Text=Here is the line typed into edit1
Font=Arial@8@+---@0@

[Edit2]
Text=Here is the line typed into edit2
Font=MS Sans Serif@8@----@0@

[ComboBox1]
Values="Value 1","Value 2","Value 4","Value 3"
Selected=Value 3
Sorted=false

This example file should fill the Edit1 with a bold Arial 8 pt. text, while Edit2 has a normal MS Sans Serif 8 pt. text.

This example should give you an idea, of how to save the properties of every control you wish. Sometimes you can take a small shortcut in the coding of the load/save routine. If you don't want to save the fonts of your editboxes, you could use the TCustomEdit for your load/save routines. This will then also be used to save all edits and memos.

Report
Re: How do I STORE Control values or properties in a file ? Posted by mydelphi on 20 Jul 2003 at 10:01 AM
Hi zibadian,

First, thank you for supplying me with such an elaborate piece of
code. I'm too busy picking up my eyes, cause they popped out of its
sockets

Lets see, I tried your first code block (all the procedures and stuff)
and copied it just after the tail end of my forms

  TForm1 = class(TForm)
.
.
  public
    { Public declarations }
  end;

type
  TControlsFile = class(TIniFile)
  private
    procedure LoadComboBox(Control: TComboBox);
    procedure SaveComboBox(Control: TComboBox);
    procedure LoadEdit(Control: TEdit);
    procedure SaveEdit(Control: TEdit);
  public
    procedure LoadControls(Form: TForm);
    procedure SaveControls(Form: TForm);
  end;

procedure TControlsFile.LoadComboBox(Control: TComboBox);
begin
  with Control do begin
    // first read if the list is sorted
    Sorted := ReadBool(Name, 'Sorted', true);
    // read all the items, which are automatically sorted (if sorted = true)
    Items.CommaText := ReadString(Name, 'Values', 'DefaultValues');
    // read the selected item
    ItemIndex := Items.IndexOf(ReadString(Name, 'Selected', ''));
    // add more properties as you like, by calling the correct ReadXXXX method
  end;
end;
.
.


I'm getting hung up with an error message:
*************
Identifier redeclared: 'TControlsFile.LoadComboBox
*************

I'm hoping that all I have to do, is basically, copy everything you
posted (which I did do) and from their on, study and use it into my
application. It's looks very interesting, and seems to be willing to
fit my projects purpose. I am greatful for this code you shared!
You don't know how hard I tried searching for it.

I think I will maintain it in a separate "include" file (I haven't
used them since Turbo Pascal 3, days) Maybe there is a better way.
I just don't want to have my project bombarded with so much source
code. It will confuse me. We'll see. In the mean time.

I want badly, to get it working. What do I have to do to get it
working ?

-mydelphi





: : Hi,
: :
: : I'm looking for some suggestions.
: :
: : I've pretty much exhausted myself of a theral search in all of my
: : libraries of books for any example or hints or whatever,
: :
: : Mastering Delphi 3
: : Secrets of Delphi 2
: : Delphi 3 Super Bible
: : And several other books
: : Plus, the Internet (doing actual web searches)
: :
: : So, here I am, asking some more questions.
: :
: : Ok then, I have, yet another problem. I'm trying to STORE my controls
: : values into a file. I've mimiced ONLY the controls values/property
: : that I will end up using.
: :
: : I've thought about using the TIniFle unit and save to and INI file,
: : and this would be simple, if all I was doing was saying ONE dementional
: : strings, Intergers, even checkbox values ie, .checked=boolean,
: : but that turned out to be a bad idea. If I have a combobox for instance,
: : with a list of items, say 15 of them, how am I going to store them in this
: : INI file structure, and then put them back into the combobox later, and
: : I go to retreave the complete file (control structure on my form)
: :
: : I would love to just store the ALL the controls AND their values from my
: : form, and just recall them as they were last set, but after trying that,
: : using a file structure below, either my system would crash or I did not
: : get back my values (they did not get stored?)
: :
: : Example:
: :
: :
: : Type
: :   TmyControls = record
: :                   txtTmpListLastStr: String[100]; // last highlisted string
: :                   cbTmpList: TCombobox;
: :                    chbYesNo: TCheckbox;
: :                     txtSize: Integer;                  
: :                 end;
: : 
: : var
: :   F : file of TmyControls;
: :   myControlsRecStruc: TmyControls;
: : .
: : .
: :   myControlsRecStruc.txtTmpListLastStr := form1.txtTmpListLastStr;
: :           myControlsRecStruc.cbTmpList := form1.cbTmpList.checked;
: :            myControlsRecStruc.chbYesNo := form1.chbYesNo;
: :             myControlsRecStruc.txtSize := form1.txtSize;
: : .
: : .
: :   // Write the data to our file structure
: :   assign(f,'h:\ctrldef.ini');
: :     rewrite(f);
: :       write(f,myControlsRecStruc);
: :   close(f);
: : 
: :   // Read the data back, from our file structure, into our Controls
: :   assign(f,'h:\def.ini');
: :     reset(f);
: :     read(f,myControlsRecStruc);
: :   close(f);
: : .
: : .
: :   // populate our Controls from file structure here.
: : .
: : .
: : 

: :
: : Not to forget that all the above are Controls on my form.
: :
: : but the above presents too many problems:
: : 1 - combobox: If I add a new item (which I will) how to they get inserted
: : and properly sorted. If I have a mechanism for storing the last position
: : or selection in the combobox, and I go and insert a new item into this
: : list, won't I throw off the order AND the new item ?
: : 2 - some of my controls are mixed values. Example, there are some
: : Intergers, Strings (only works if I say String[nnn], NOT String) and
: : checkboxes. I use/store the chbYesNo.checked values.
: :
: : I'm sorry for being so lenghthy, but it was difficult to explain.
: : I was sure I read somewhere's in one of my Books on how to do this, but I
: : could not find anything in my latest search.
: :
: : I really hope that it's something very simple that I over looked
: :
: : Thank you for any suggestions
: :
: : -mydelphi
: :
: Saving the controls into an inifile is a smart idea, since you can create a single section to hold all the values you want for a single control. The easiest way to code this is to create a descendant of a TIniFile or TMemIniFile, which adds several new methods to handle certain classes of controls. Here is an example in code.
:
: type
:   TControlsFile = class(TIniFile)
:   private
:     procedure LoadComboBox(Control: TComboBox);
:     procedure SaveComboBox(Control: TComboBox);
:     procedure LoadEdit(Control: TEdit);
:     procedure SaveEdit(Control: TEdit);
:   public
:     procedure LoadControls(Form: TForm);
:     procedure SaveControls(Form: TForm);
:   end;
: 
: procedure TControlsFile.LoadComboBox(Control: TComboBox);
: begin
:   with Control do begin
:     // first read if the list is sorted
:     Sorted := ReadBool(Name, 'Sorted', true);
:     // read all the items, which are automatically sorted (if sorted = true)
:     Items.CommaText := ReadString(Name, 'Values', 'DefaultValues');
:     // read the selected item
:     ItemIndex := Items.IndexOf(ReadString(Name, 'Selected', ''));
:     // add more properties as you like, by calling the correct ReadXXXX method
:   end;
: end;
: 
: procedure TControlsFile.LoadControls(Form: TForm);
: var
:   i: integer;
: begin
:   with Form do
:     if ComponentCount > 0 then
:       // loop through all the components on the form and
:       for i := 0 to ComponentCount-1 do
:         // and call the load routine for its class
:         if Components[i] is TComboBox then
:           LoadComboBox(Components[i] as TComboBox)
:         else if Components[i] is TEdit then
:           LoadEdit(Components[i] as TEdit);
:         // Add more control types by adding more "else-if-then"s
: end;
: 
: procedure TControlsFile.LoadEdit(Control: TEdit);
: var
:   s: string;
: begin
:   with Control do begin
:     // read the text
:     Text := ReadString(Name, 'Text', 'Some default text');
:     // read the font string
:     s := ReadString(Name, 'Font', 'MS Sans Serif@8@----@0@');
:     // set the font properties based on the font string
:     Font.Name := Copy(s, 1, Pos('@', s)-1);
:     Delete(s, 1, Pos('@', s));
:     Font.Size := StrToInt(Copy(s, 1, Pos('@', s)-1));
:     Delete(s, 1, Pos('@', s));
:     Font.Style := [];
:     if s[1] = '+' then
:       Font.Style := Font.Style + [fsBold];
:     if s[2] = '+' then
:       Font.Style := Font.Style + [fsItalic];
:     if s[3] = '+' then
:       Font.Style := Font.Style + [fsUnderline];
:     if s[4] = '+' then
:       Font.Style := Font.Style + [fsStrikeOut];
:     Delete(s, 1, Pos('@', s));
:     Font.Color := StrToInt(Copy(s, 1, Pos('@', s)-1));
:   end;
: end;
: 
: procedure TControlsFile.SaveComboBox(Control: TComboBox);
: begin
:   with Control do begin
:     // write the properties
:     WriteString(Name, 'Values', Items.CommaText);
:     WriteString(Name, 'Selected', Items[ItemIndex]);
:     WriteBool(Name, 'Sorted', Sorted);
:     // add more properties as you like, by calling the correct WriteXXXX method
:   end;
: end;
: 
: procedure TControlsFile.SaveControls(Form: TForm);
: var
:   i: integer;
: begin
:   with Form do
:     if ComponentCount > 0 then
:       // loop through all the components on the form and
:       for i := 0 to ComponentCount-1 do
:         // and call the save routine for its class
:         if Components[i] is TComboBox then
:           SaveComboBox(Components[i] as TComboBox)
:         else if Components[i] is TEdit then
:           SaveEdit(Components[i] as TEdit);
:         // Add more control types by adding more "else-if-then"s
: end;
: 
: procedure TControlsFile.SaveEdit(Control: TEdit);
: var
:   s: string;
: begin
:   with Control do begin
:     // write the text
:     WriteString(Name, 'Text', Text);
:     // copy the font properties into a single string
:     s := '----'; // assume normal style
:     if fsBold in Font.Style then // if style is bolded then
:       s[1] := '+'; // change the value in the string
:     if fsItalic in Font.Style then // same with italic
:       s[2] := '+';
:     if fsUnderline in Font.Style then
:       s[3] := '+';
:     if fsStrikeOut in Font.Style then
:       s[4] := '+';
:     // compile the complete font string
:     s := Format('%s@%d@%s@%d@', [Font.Name, Font.Size, s, Integer(Font.Color)]); 
:     // write that string
:     WriteString(Name, 'Font', s);
:   end;
: end;
: 

: This example will save and load the properties of all the TComboBoxes and TEdits on a single form. The format of the resulting file will be something like this:
:
: [Edit1]
: Text=Here is the line typed into edit1
: Font=Arial@8@+---@0@
: 
: [Edit2]
: Text=Here is the line typed into edit2
: Font=MS Sans Serif@8@----@0@
: 
: [ComboBox1]
: Values="Value 1","Value 2","Value 4","Value 3"
: Selected=Value 3
: Sorted=false
: 

: This example file should fill the Edit1 with a bold Arial 8 pt. text, while Edit2 has a normal MS Sans Serif 8 pt. text.
:
: This example should give you an idea, of how to save the properties of every control you wish. Sometimes you can take a small shortcut in the coding of the load/save routine. If you don't want to save the fonts of your editboxes, you could use the TCustomEdit for your load/save routines. This will then also be used to save all edits and memos.
:
:

Report
Re: How do I STORE Control values or properties in a file ? Posted by mydelphi on 20 Jul 2003 at 10:38 AM
Some good news.

The first block I copied previously is now working (compiling).
What I had done in error previously (I think) is copy everything into
the Interface area.
I experimented, and moved all the code just after the Implementation
part, and it compilied perfect.

My next question, how to I CALL or Save those controls on my form ?
In my previous "Save to file" button, I remarked out the code that
calls the save, and now I want to relace it with ?

Are there any "order" to the calling of statements ?
I'd like to keep them in an order, 1, 2, 3 etc.

Thanks again for your help
-mydelphi




: : Hi,
: :
: : I'm looking for some suggestions.
: :
: : I've pretty much exhausted myself of a theral search in all of my
: : libraries of books for any example or hints or whatever,
: :
: : Mastering Delphi 3
: : Secrets of Delphi 2
: : Delphi 3 Super Bible
: : And several other books
: : Plus, the Internet (doing actual web searches)
: :
: : So, here I am, asking some more questions.
: :
: : Ok then, I have, yet another problem. I'm trying to STORE my controls
: : values into a file. I've mimiced ONLY the controls values/property
: : that I will end up using.
: :
: : I've thought about using the TIniFle unit and save to and INI file,
: : and this would be simple, if all I was doing was saying ONE dementional
: : strings, Intergers, even checkbox values ie, .checked=boolean,
: : but that turned out to be a bad idea. If I have a combobox for instance,
: : with a list of items, say 15 of them, how am I going to store them in this
: : INI file structure, and then put them back into the combobox later, and
: : I go to retreave the complete file (control structure on my form)
: :
: : I would love to just store the ALL the controls AND their values from my
: : form, and just recall them as they were last set, but after trying that,
: : using a file structure below, either my system would crash or I did not
: : get back my values (they did not get stored?)
: :
: : Example:
: :
: :
: : Type
: :   TmyControls = record
: :                   txtTmpListLastStr: String[100]; // last highlisted string
: :                   cbTmpList: TCombobox;
: :                    chbYesNo: TCheckbox;
: :                     txtSize: Integer;                  
: :                 end;
: : 
: : var
: :   F : file of TmyControls;
: :   myControlsRecStruc: TmyControls;
: : .
: : .
: :   myControlsRecStruc.txtTmpListLastStr := form1.txtTmpListLastStr;
: :           myControlsRecStruc.cbTmpList := form1.cbTmpList.checked;
: :            myControlsRecStruc.chbYesNo := form1.chbYesNo;
: :             myControlsRecStruc.txtSize := form1.txtSize;
: : .
: : .
: :   // Write the data to our file structure
: :   assign(f,'h:\ctrldef.ini');
: :     rewrite(f);
: :       write(f,myControlsRecStruc);
: :   close(f);
: : 
: :   // Read the data back, from our file structure, into our Controls
: :   assign(f,'h:\def.ini');
: :     reset(f);
: :     read(f,myControlsRecStruc);
: :   close(f);
: : .
: : .
: :   // populate our Controls from file structure here.
: : .
: : .
: : 

: :
: : Not to forget that all the above are Controls on my form.
: :
: : but the above presents too many problems:
: : 1 - combobox: If I add a new item (which I will) how to they get inserted
: : and properly sorted. If I have a mechanism for storing the last position
: : or selection in the combobox, and I go and insert a new item into this
: : list, won't I throw off the order AND the new item ?
: : 2 - some of my controls are mixed values. Example, there are some
: : Intergers, Strings (only works if I say String[nnn], NOT String) and
: : checkboxes. I use/store the chbYesNo.checked values.
: :
: : I'm sorry for being so lenghthy, but it was difficult to explain.
: : I was sure I read somewhere's in one of my Books on how to do this, but I
: : could not find anything in my latest search.
: :
: : I really hope that it's something very simple that I over looked
: :
: : Thank you for any suggestions
: :
: : -mydelphi
: :
: Saving the controls into an inifile is a smart idea, since you can create a single section to hold all the values you want for a single control. The easiest way to code this is to create a descendant of a TIniFile or TMemIniFile, which adds several new methods to handle certain classes of controls. Here is an example in code.
:
: type
:   TControlsFile = class(TIniFile)
:   private
:     procedure LoadComboBox(Control: TComboBox);
:     procedure SaveComboBox(Control: TComboBox);
:     procedure LoadEdit(Control: TEdit);
:     procedure SaveEdit(Control: TEdit);
:   public
:     procedure LoadControls(Form: TForm);
:     procedure SaveControls(Form: TForm);
:   end;
: 
: procedure TControlsFile.LoadComboBox(Control: TComboBox);
: begin
:   with Control do begin
:     // first read if the list is sorted
:     Sorted := ReadBool(Name, 'Sorted', true);
:     // read all the items, which are automatically sorted (if sorted = true)
:     Items.CommaText := ReadString(Name, 'Values', 'DefaultValues');
:     // read the selected item
:     ItemIndex := Items.IndexOf(ReadString(Name, 'Selected', ''));
:     // add more properties as you like, by calling the correct ReadXXXX method
:   end;
: end;
: 
: procedure TControlsFile.LoadControls(Form: TForm);
: var
:   i: integer;
: begin
:   with Form do
:     if ComponentCount > 0 then
:       // loop through all the components on the form and
:       for i := 0 to ComponentCount-1 do
:         // and call the load routine for its class
:         if Components[i] is TComboBox then
:           LoadComboBox(Components[i] as TComboBox)
:         else if Components[i] is TEdit then
:           LoadEdit(Components[i] as TEdit);
:         // Add more control types by adding more "else-if-then"s
: end;
: 
: procedure TControlsFile.LoadEdit(Control: TEdit);
: var
:   s: string;
: begin
:   with Control do begin
:     // read the text
:     Text := ReadString(Name, 'Text', 'Some default text');
:     // read the font string
:     s := ReadString(Name, 'Font', 'MS Sans Serif@8@----@0@');
:     // set the font properties based on the font string
:     Font.Name := Copy(s, 1, Pos('@', s)-1);
:     Delete(s, 1, Pos('@', s));
:     Font.Size := StrToInt(Copy(s, 1, Pos('@', s)-1));
:     Delete(s, 1, Pos('@', s));
:     Font.Style := [];
:     if s[1] = '+' then
:       Font.Style := Font.Style + [fsBold];
:     if s[2] = '+' then
:       Font.Style := Font.Style + [fsItalic];
:     if s[3] = '+' then
:       Font.Style := Font.Style + [fsUnderline];
:     if s[4] = '+' then
:       Font.Style := Font.Style + [fsStrikeOut];
:     Delete(s, 1, Pos('@', s));
:     Font.Color := StrToInt(Copy(s, 1, Pos('@', s)-1));
:   end;
: end;
: 
: procedure TControlsFile.SaveComboBox(Control: TComboBox);
: begin
:   with Control do begin
:     // write the properties
:     WriteString(Name, 'Values', Items.CommaText);
:     WriteString(Name, 'Selected', Items[ItemIndex]);
:     WriteBool(Name, 'Sorted', Sorted);
:     // add more properties as you like, by calling the correct WriteXXXX method
:   end;
: end;
: 
: procedure TControlsFile.SaveControls(Form: TForm);
: var
:   i: integer;
: begin
:   with Form do
:     if ComponentCount > 0 then
:       // loop through all the components on the form and
:       for i := 0 to ComponentCount-1 do
:         // and call the save routine for its class
:         if Components[i] is TComboBox then
:           SaveComboBox(Components[i] as TComboBox)
:         else if Components[i] is TEdit then
:           SaveEdit(Components[i] as TEdit);
:         // Add more control types by adding more "else-if-then"s
: end;
: 
: procedure TControlsFile.SaveEdit(Control: TEdit);
: var
:   s: string;
: begin
:   with Control do begin
:     // write the text
:     WriteString(Name, 'Text', Text);
:     // copy the font properties into a single string
:     s := '----'; // assume normal style
:     if fsBold in Font.Style then // if style is bolded then
:       s[1] := '+'; // change the value in the string
:     if fsItalic in Font.Style then // same with italic
:       s[2] := '+';
:     if fsUnderline in Font.Style then
:       s[3] := '+';
:     if fsStrikeOut in Font.Style then
:       s[4] := '+';
:     // compile the complete font string
:     s := Format('%s@%d@%s@%d@', [Font.Name, Font.Size, s, Integer(Font.Color)]); 
:     // write that string
:     WriteString(Name, 'Font', s);
:   end;
: end;
: 

: This example will save and load the properties of all the TComboBoxes and TEdits on a single form. The format of the resulting file will be something like this:
:
: [Edit1]
: Text=Here is the line typed into edit1
: Font=Arial@8@+---@0@
: 
: [Edit2]
: Text=Here is the line typed into edit2
: Font=MS Sans Serif@8@----@0@
: 
: [ComboBox1]
: Values="Value 1","Value 2","Value 4","Value 3"
: Selected=Value 3
: Sorted=false
: 

: This example file should fill the Edit1 with a bold Arial 8 pt. text, while Edit2 has a normal MS Sans Serif 8 pt. text.
:
: This example should give you an idea, of how to save the properties of every control you wish. Sometimes you can take a small shortcut in the coding of the load/save routine. If you don't want to save the fonts of your editboxes, you could use the TCustomEdit for your load/save routines. This will then also be used to save all edits and memos.
:
:

Report
Re: How do I STORE Control values or properties in a file ? Posted by mydelphi on 20 Jul 2003 at 10:47 AM
Here is some mure progress news.

I did a little more experimenting, and found that:
   tINIFile: TControlsFile;


worked. I typed "tINIFile." and I got a zillion items pop up
I typed "S" and I saw the SaveComboBox an SaveEdit etc.
I guess I need to know how to SAVE my controls items and then
RETREAVE back into all my controls. Is that hard to do ?
I want to make sure I'm calling the right statetments/functions.

A little help would be nice
Thanks again,
-mydelphi



: : Hi,
: :
: : I'm looking for some suggestions.
: :
: : I've pretty much exhausted myself of a theral search in all of my
: : libraries of books for any example or hints or whatever,
: :
: : Mastering Delphi 3
: : Secrets of Delphi 2
: : Delphi 3 Super Bible
: : And several other books
: : Plus, the Internet (doing actual web searches)
: :
: : So, here I am, asking some more questions.
: :
: : Ok then, I have, yet another problem. I'm trying to STORE my controls
: : values into a file. I've mimiced ONLY the controls values/property
: : that I will end up using.
: :
: : I've thought about using the TIniFle unit and save to and INI file,
: : and this would be simple, if all I was doing was saying ONE dementional
: : strings, Intergers, even checkbox values ie, .checked=boolean,
: : but that turned out to be a bad idea. If I have a combobox for instance,
: : with a list of items, say 15 of them, how am I going to store them in this
: : INI file structure, and then put them back into the combobox later, and
: : I go to retreave the complete file (control structure on my form)
: :
: : I would love to just store the ALL the controls AND their values from my
: : form, and just recall them as they were last set, but after trying that,
: : using a file structure below, either my system would crash or I did not
: : get back my values (they did not get stored?)
: :
: : Example:
: :
: :
: : Type
: :   TmyControls = record
: :                   txtTmpListLastStr: String[100]; // last highlisted string
: :                   cbTmpList: TCombobox;
: :                    chbYesNo: TCheckbox;
: :                     txtSize: Integer;                  
: :                 end;
: : 
: : var
: :   F : file of TmyControls;
: :   myControlsRecStruc: TmyControls;
: : .
: : .
: :   myControlsRecStruc.txtTmpListLastStr := form1.txtTmpListLastStr;
: :           myControlsRecStruc.cbTmpList := form1.cbTmpList.checked;
: :            myControlsRecStruc.chbYesNo := form1.chbYesNo;
: :             myControlsRecStruc.txtSize := form1.txtSize;
: : .
: : .
: :   // Write the data to our file structure
: :   assign(f,'h:\ctrldef.ini');
: :     rewrite(f);
: :       write(f,myControlsRecStruc);
: :   close(f);
: : 
: :   // Read the data back, from our file structure, into our Controls
: :   assign(f,'h:\def.ini');
: :     reset(f);
: :     read(f,myControlsRecStruc);
: :   close(f);
: : .
: : .
: :   // populate our Controls from file structure here.
: : .
: : .
: : 

: :
: : Not to forget that all the above are Controls on my form.
: :
: : but the above presents too many problems:
: : 1 - combobox: If I add a new item (which I will) how to they get inserted
: : and properly sorted. If I have a mechanism for storing the last position
: : or selection in the combobox, and I go and insert a new item into this
: : list, won't I throw off the order AND the new item ?
: : 2 - some of my controls are mixed values. Example, there are some
: : Intergers, Strings (only works if I say String[nnn], NOT String) and
: : checkboxes. I use/store the chbYesNo.checked values.
: :
: : I'm sorry for being so lenghthy, but it was difficult to explain.
: : I was sure I read somewhere's in one of my Books on how to do this, but I
: : could not find anything in my latest search.
: :
: : I really hope that it's something very simple that I over looked
: :
: : Thank you for any suggestions
: :
: : -mydelphi
: :
: Saving the controls into an inifile is a smart idea, since you can create a single section to hold all the values you want for a single control. The easiest way to code this is to create a descendant of a TIniFile or TMemIniFile, which adds several new methods to handle certain classes of controls. Here is an example in code.
:
: type
:   TControlsFile = class(TIniFile)
:   private
:     procedure LoadComboBox(Control: TComboBox);
:     procedure SaveComboBox(Control: TComboBox);
:     procedure LoadEdit(Control: TEdit);
:     procedure SaveEdit(Control: TEdit);
:   public
:     procedure LoadControls(Form: TForm);
:     procedure SaveControls(Form: TForm);
:   end;
: 
: procedure TControlsFile.LoadComboBox(Control: TComboBox);
: begin
:   with Control do begin
:     // first read if the list is sorted
:     Sorted := ReadBool(Name, 'Sorted', true);
:     // read all the items, which are automatically sorted (if sorted = true)
:     Items.CommaText := ReadString(Name, 'Values', 'DefaultValues');
:     // read the selected item
:     ItemIndex := Items.IndexOf(ReadString(Name, 'Selected', ''));
:     // add more properties as you like, by calling the correct ReadXXXX method
:   end;
: end;
: 
: procedure TControlsFile.LoadControls(Form: TForm);
: var
:   i: integer;
: begin
:   with Form do
:     if ComponentCount > 0 then
:       // loop through all the components on the form and
:       for i := 0 to ComponentCount-1 do
:         // and call the load routine for its class
:         if Components[i] is TComboBox then
:           LoadComboBox(Components[i] as TComboBox)
:         else if Components[i] is TEdit then
:           LoadEdit(Components[i] as TEdit);
:         // Add more control types by adding more "else-if-then"s
: end;
: 
: procedure TControlsFile.LoadEdit(Control: TEdit);
: var
:   s: string;
: begin
:   with Control do begin
:     // read the text
:     Text := ReadString(Name, 'Text', 'Some default text');
:     // read the font string
:     s := ReadString(Name, 'Font', 'MS Sans Serif@8@----@0@');
:     // set the font properties based on the font string
:     Font.Name := Copy(s, 1, Pos('@', s)-1);
:     Delete(s, 1, Pos('@', s));
:     Font.Size := StrToInt(Copy(s, 1, Pos('@', s)-1));
:     Delete(s, 1, Pos('@', s));
:     Font.Style := [];
:     if s[1] = '+' then
:       Font.Style := Font.Style + [fsBold];
:     if s[2] = '+' then
:       Font.Style := Font.Style + [fsItalic];
:     if s[3] = '+' then
:       Font.Style := Font.Style + [fsUnderline];
:     if s[4] = '+' then
:       Font.Style := Font.Style + [fsStrikeOut];
:     Delete(s, 1, Pos('@', s));
:     Font.Color := StrToInt(Copy(s, 1, Pos('@', s)-1));
:   end;
: end;
: 
: procedure TControlsFile.SaveComboBox(Control: TComboBox);
: begin
:   with Control do begin
:     // write the properties
:     WriteString(Name, 'Values', Items.CommaText);
:     WriteString(Name, 'Selected', Items[ItemIndex]);
:     WriteBool(Name, 'Sorted', Sorted);
:     // add more properties as you like, by calling the correct WriteXXXX method
:   end;
: end;
: 
: procedure TControlsFile.SaveControls(Form: TForm);
: var
:   i: integer;
: begin
:   with Form do
:     if ComponentCount > 0 then
:       // loop through all the components on the form and
:       for i := 0 to ComponentCount-1 do
:         // and call the save routine for its class
:         if Components[i] is TComboBox then
:           SaveComboBox(Components[i] as TComboBox)
:         else if Components[i] is TEdit then
:           SaveEdit(Components[i] as TEdit);
:         // Add more control types by adding more "else-if-then"s
: end;
: 
: procedure TControlsFile.SaveEdit(Control: TEdit);
: var
:   s: string;
: begin
:   with Control do begin
:     // write the text
:     WriteString(Name, 'Text', Text);
:     // copy the font properties into a single string
:     s := '----'; // assume normal style
:     if fsBold in Font.Style then // if style is bolded then
:       s[1] := '+'; // change the value in the string
:     if fsItalic in Font.Style then // same with italic
:       s[2] := '+';
:     if fsUnderline in Font.Style then
:       s[3] := '+';
:     if fsStrikeOut in Font.Style then
:       s[4] := '+';
:     // compile the complete font string
:     s := Format('%s@%d@%s@%d@', [Font.Name, Font.Size, s, Integer(Font.Color)]); 
:     // write that string
:     WriteString(Name, 'Font', s);
:   end;
: end;
: 

: This example will save and load the properties of all the TComboBoxes and TEdits on a single form. The format of the resulting file will be something like this:
:
: [Edit1]
: Text=Here is the line typed into edit1
: Font=Arial@8@+---@0@
: 
: [Edit2]
: Text=Here is the line typed into edit2
: Font=MS Sans Serif@8@----@0@
: 
: [ComboBox1]
: Values="Value 1","Value 2","Value 4","Value 3"
: Selected=Value 3
: Sorted=false
: 

: This example file should fill the Edit1 with a bold Arial 8 pt. text, while Edit2 has a normal MS Sans Serif 8 pt. text.
:
: This example should give you an idea, of how to save the properties of every control you wish. Sometimes you can take a small shortcut in the coding of the load/save routine. If you don't want to save the fonts of your editboxes, you could use the TCustomEdit for your load/save routines. This will then also be used to save all edits and memos.
:
:

Report
Re: How do I STORE Control values or properties in a file ? Posted by mydelphi on 20 Jul 2003 at 12:37 PM
Not so good news here.

So far, I've gone through many searches, and found nothing relating to
the source code that was posted here. I'm still unable to get it to
work (create and save the data to an ini file) I'm pretty exhausted
with searching and reading on this today.

Right now, I'm going through my "Delphi 3 Super Bible" book, well over
1,300 pages worth of nothing, right ? and anyway, I'm reading up on
the tregistry and tinifile subjects. Very tireing. I'm starting to
loose interest in my app now. I've lost contact with the person who
sent me this (thanks anyways) and I'm just not making any more headway.

I'm going to continue trying harder to figuring this code out, cause I
really thing its perfect for what I want to do It's just hard to
get there without some additional guidance, thats all.

Well, let me get back to my project, before I really loose interest

If I find anything new out or make some headway, I'll post my updates
here.

Thanks everyone.
-mydelphi



: Here is some mure progress news.
:
: I did a little more experimenting, and found that:
:
:    tINIFile: TControlsFile;
: 

:
: worked. I typed "tINIFile." and I got a zillion items pop up
: I typed "S" and I saw the SaveComboBox an SaveEdit etc.
: I guess I need to know how to SAVE my controls items and then
: RETREAVE back into all my controls. Is that hard to do ?
: I want to make sure I'm calling the right statetments/functions.
:
: A little help would be nice
: Thanks again,
: -mydelphi
:
:
:
: : : Hi,
: : :
: : : I'm looking for some suggestions.
: : :
: : : I've pretty much exhausted myself of a theral search in all of my
: : : libraries of books for any example or hints or whatever,
: : :
: : : Mastering Delphi 3
: : : Secrets of Delphi 2
: : : Delphi 3 Super Bible
: : : And several other books
: : : Plus, the Internet (doing actual web searches)
: : :
: : : So, here I am, asking some more questions.
: : :
: : : Ok then, I have, yet another problem. I'm trying to STORE my controls
: : : values into a file. I've mimiced ONLY the controls values/property
: : : that I will end up using.
: : :
: : : I've thought about using the TIniFle unit and save to and INI file,
: : : and this would be simple, if all I was doing was saying ONE dementional
: : : strings, Intergers, even checkbox values ie, .checked=boolean,
: : : but that turned out to be a bad idea. If I have a combobox for instance,
: : : with a list of items, say 15 of them, how am I going to store them in this
: : : INI file structure, and then put them back into the combobox later, and
: : : I go to retreave the complete file (control structure on my form)
: : :
: : : I would love to just store the ALL the controls AND their values from my
: : : form, and just recall them as they were last set, but after trying that,
: : : using a file structure below, either my system would crash or I did not
: : : get back my values (they did not get stored?)
: : :
: : : Example:
: : :
: : :
: : : Type
: : :   TmyControls = record
: : :                   txtTmpListLastStr: String[100]; // last highlisted string
: : :                   cbTmpList: TCombobox;
: : :                    chbYesNo: TCheckbox;
: : :                     txtSize: Integer;                  
: : :                 end;
: : : 
: : : var
: : :   F : file of TmyControls;
: : :   myControlsRecStruc: TmyControls;
: : : .
: : : .
: : :   myControlsRecStruc.txtTmpListLastStr := form1.txtTmpListLastStr;
: : :           myControlsRecStruc.cbTmpList := form1.cbTmpList.checked;
: : :            myControlsRecStruc.chbYesNo := form1.chbYesNo;
: : :             myControlsRecStruc.txtSize := form1.txtSize;
: : : .
: : : .
: : :   // Write the data to our file structure
: : :   assign(f,'h:\ctrldef.ini');
: : :     rewrite(f);
: : :       write(f,myControlsRecStruc);
: : :   close(f);
: : : 
: : :   // Read the data back, from our file structure, into our Controls
: : :   assign(f,'h:\def.ini');
: : :     reset(f);
: : :     read(f,myControlsRecStruc);
: : :   close(f);
: : : .
: : : .
: : :   // populate our Controls from file structure here.
: : : .
: : : .
: : : 

: : :
: : : Not to forget that all the above are Controls on my form.
: : :
: : : but the above presents too many problems:
: : : 1 - combobox: If I add a new item (which I will) how to they get inserted
: : : and properly sorted. If I have a mechanism for storing the last position
: : : or selection in the combobox, and I go and insert a new item into this
: : : list, won't I throw off the order AND the new item ?
: : : 2 - some of my controls are mixed values. Example, there are some
: : : Intergers, Strings (only works if I say String[nnn], NOT String) and
: : : checkboxes. I use/store the chbYesNo.checked values.
: : :
: : : I'm sorry for being so lenghthy, but it was difficult to explain.
: : : I was sure I read somewhere's in one of my Books on how to do this, but I
: : : could not find anything in my latest search.
: : :
: : : I really hope that it's something very simple that I over looked
: : :
: : : Thank you for any suggestions
: : :
: : : -mydelphi
: : :
: : Saving the controls into an inifile is a smart idea, since you can create a single section to hold all the values you want for a single control. The easiest way to code this is to create a descendant of a TIniFile or TMemIniFile, which adds several new methods to handle certain classes of controls. Here is an example in code.
: :
: : type
: :   TControlsFile = class(TIniFile)
: :   private
: :     procedure LoadComboBox(Control: TComboBox);
: :     procedure SaveComboBox(Control: TComboBox);
: :     procedure LoadEdit(Control: TEdit);
: :     procedure SaveEdit(Control: TEdit);
: :   public
: :     procedure LoadControls(Form: TForm);
: :     procedure SaveControls(Form: TForm);
: :   end;
: : 
: : procedure TControlsFile.LoadComboBox(Control: TComboBox);
: : begin
: :   with Control do begin
: :     // first read if the list is sorted
: :     Sorted := ReadBool(Name, 'Sorted', true);
: :     // read all the items, which are automatically sorted (if sorted = true)
: :     Items.CommaText := ReadString(Name, 'Values', 'DefaultValues');
: :     // read the selected item
: :     ItemIndex := Items.IndexOf(ReadString(Name, 'Selected', ''));
: :     // add more properties as you like, by calling the correct ReadXXXX method
: :   end;
: : end;
: : 
: : procedure TControlsFile.LoadControls(Form: TForm);
: : var
: :   i: integer;
: : begin
: :   with Form do
: :     if ComponentCount > 0 then
: :       // loop through all the components on the form and
: :       for i := 0 to ComponentCount-1 do
: :         // and call the load routine for its class
: :         if Components[i] is TComboBox then
: :           LoadComboBox(Components[i] as TComboBox)
: :         else if Components[i] is TEdit then
: :           LoadEdit(Components[i] as TEdit);
: :         // Add more control types by adding more "else-if-then"s
: : end;
: : 
: : procedure TControlsFile.LoadEdit(Control: TEdit);
: : var
: :   s: string;
: : begin
: :   with Control do begin
: :     // read the text
: :     Text := ReadString(Name, 'Text', 'Some default text');
: :     // read the font string
: :     s := ReadString(Name, 'Font', 'MS Sans Serif@8@----@0@');
: :     // set the font properties based on the font string
: :     Font.Name := Copy(s, 1, Pos('@', s)-1);
: :     Delete(s, 1, Pos('@', s));
: :     Font.Size := StrToInt(Copy(s, 1, Pos('@', s)-1));
: :     Delete(s, 1, Pos('@', s));
: :     Font.Style := [];
: :     if s[1] = '+' then
: :       Font.Style := Font.Style + [fsBold];
: :     if s[2] = '+' then
: :       Font.Style := Font.Style + [fsItalic];
: :     if s[3] = '+' then
: :       Font.Style := Font.Style + [fsUnderline];
: :     if s[4] = '+' then
: :       Font.Style := Font.Style + [fsStrikeOut];
: :     Delete(s, 1, Pos('@', s));
: :     Font.Color := StrToInt(Copy(s, 1, Pos('@', s)-1));
: :   end;
: : end;
: : 
: : procedure TControlsFile.SaveComboBox(Control: TComboBox);
: : begin
: :   with Control do begin
: :     // write the properties
: :     WriteString(Name, 'Values', Items.CommaText);
: :     WriteString(Name, 'Selected', Items[ItemIndex]);
: :     WriteBool(Name, 'Sorted', Sorted);
: :     // add more properties as you like, by calling the correct WriteXXXX method
: :   end;
: : end;
: : 
: : procedure TControlsFile.SaveControls(Form: TForm);
: : var
: :   i: integer;
: : begin
: :   with Form do
: :     if ComponentCount > 0 then
: :       // loop through all the components on the form and
: :       for i := 0 to ComponentCount-1 do
: :         // and call the save routine for its class
: :         if Components[i] is TComboBox then
: :           SaveComboBox(Components[i] as TComboBox)
: :         else if Components[i] is TEdit then
: :           SaveEdit(Components[i] as TEdit);
: :         // Add more control types by adding more "else-if-then"s
: : end;
: : 
: : procedure TControlsFile.SaveEdit(Control: TEdit);
: : var
: :   s: string;
: : begin
: :   with Control do begin
: :     // write the text
: :     WriteString(Name, 'Text', Text);
: :     // copy the font properties into a single string
: :     s := '----'; // assume normal style
: :     if fsBold in Font.Style then // if style is bolded then
: :       s[1] := '+'; // change the value in the string
: :     if fsItalic in Font.Style then // same with italic
: :       s[2] := '+';
: :     if fsUnderline in Font.Style then
: :       s[3] := '+';
: :     if fsStrikeOut in Font.Style then
: :       s[4] := '+';
: :     // compile the complete font string
: :     s := Format('%s@%d@%s@%d@', [Font.Name, Font.Size, s, Integer(Font.Color)]); 
: :     // write that string
: :     WriteString(Name, 'Font', s);
: :   end;
: : end;
: : 

: : This example will save and load the properties of all the TComboBoxes and TEdits on a single form. The format of the resulting file will be something like this:
: :
: : [Edit1]
: : Text=Here is the line typed into edit1
: : Font=Arial@8@+---@0@
: : 
: : [Edit2]
: : Text=Here is the line typed into edit2
: : Font=MS Sans Serif@8@----@0@
: : 
: : [ComboBox1]
: : Values="Value 1","Value 2","Value 4","Value 3"
: : Selected=Value 3
: : Sorted=false
: : 

: : This example file should fill the Edit1 with a bold Arial 8 pt. text, while Edit2 has a normal MS Sans Serif 8 pt. text.
: :
: : This example should give you an idea, of how to save the properties of every control you wish. Sometimes you can take a small shortcut in the coding of the load/save routine. If you don't want to save the fonts of your editboxes, you could use the TCustomEdit for your load/save routines. This will then also be used to save all edits and memos.
: :
: :
:
:

Report
Re: How do I STORE Control values or properties in a file ? Posted by mydelphi on 20 Jul 2003 at 1:49 PM
I've got some news.

Finally. After countless what ifs, I got something working. But, I have
another issue. I can't get it to Load bACK into my controls. I need
help with this. anyway, here is what I got so far, and it works as far
as I can tell, when I, at the DOS prompt c:\type ctrldef.ini, it will
display a bunch of settings and things.
[i]I've simplyied it a little ie, t:=tonc... etc. less typing.[/i]

  // write out the data
  t:=tcontrolsFile.Create('h:\ctrldef.ini');
  t.SaveControls(form1);



So, I've made some more progress. great.

Now, I did notice something. my combobox has some funny char in it or
will mess up my original values when I try and load it with the Saved
ctrldef.ini contents:

  // read in the data
  t:=tcontrolsFile.Create('h:\ctrldef.ini');
  t.LoadControls(form1);


Now, as I was saying, the controls aren't properly loading back. I need
some help with this. If anyone else would like ta help, plase do so.
I'd really appreciate it

I am worried about one more thing though. How to I ADD to my combobox
list of items ?
I'm going to be doing this, using a separate Textbox (blank by default,
until user enters somerthing into it, and a NEW item should be added to
the list when user clicks on the Save button (or SaveTemplate button)

Thanks again.
-mydelphi



: : Hi,
: :
: : I'm looking for some suggestions.
: :
: : I've pretty much exhausted myself of a theral search in all of my
: : libraries of books for any example or hints or whatever,
: :
: : Mastering Delphi 3
: : Secrets of Delphi 2
: : Delphi 3 Super Bible
: : And several other books
: : Plus, the Internet (doing actual web searches)
: :
: : So, here I am, asking some more questions.
: :
: : Ok then, I have, yet another problem. I'm trying to STORE my controls
: : values into a file. I've mimiced ONLY the controls values/property
: : that I will end up using.
: :
: : I've thought about using the TIniFle unit and save to and INI file,
: : and this would be simple, if all I was doing was saying ONE dementional
: : strings, Intergers, even checkbox values ie, .checked=boolean,
: : but that turned out to be a bad idea. If I have a combobox for instance,
: : with a list of items, say 15 of them, how am I going to store them in this
: : INI file structure, and then put them back into the combobox later, and
: : I go to retreave the complete file (control structure on my form)
: :
: : I would love to just store the ALL the controls AND their values from my
: : form, and just recall them as they were last set, but after trying that,
: : using a file structure below, either my system would crash or I did not
: : get back my values (they did not get stored?)
: :
: : Example:
: :
: :
: : Type
: :   TmyControls = record
: :                   txtTmpListLastStr: String[100]; // last highlisted string
: :                   cbTmpList: TCombobox;
: :                    chbYesNo: TCheckbox;
: :                     txtSize: Integer;                  
: :                 end;
: : 
: : var
: :   F : file of TmyControls;
: :   myControlsRecStruc: TmyControls;
: : .
: : .
: :   myControlsRecStruc.txtTmpListLastStr := form1.txtTmpListLastStr;
: :           myControlsRecStruc.cbTmpList := form1.cbTmpList.checked;
: :            myControlsRecStruc.chbYesNo := form1.chbYesNo;
: :             myControlsRecStruc.txtSize := form1.txtSize;
: : .
: : .
: :   // Write the data to our file structure
: :   assign(f,'h:\ctrldef.ini');
: :     rewrite(f);
: :       write(f,myControlsRecStruc);
: :   close(f);
: : 
: :   // Read the data back, from our file structure, into our Controls
: :   assign(f,'h:\def.ini');
: :     reset(f);
: :     read(f,myControlsRecStruc);
: :   close(f);
: : .
: : .
: :   // populate our Controls from file structure here.
: : .
: : .
: : 

: :
: : Not to forget that all the above are Controls on my form.
: :
: : but the above presents too many problems:
: : 1 - combobox: If I add a new item (which I will) how to they get inserted
: : and properly sorted. If I have a mechanism for storing the last position
: : or selection in the combobox, and I go and insert a new item into this
: : list, won't I throw off the order AND the new item ?
: : 2 - some of my controls are mixed values. Example, there are some
: : Intergers, Strings (only works if I say String[nnn], NOT String) and
: : checkboxes. I use/store the chbYesNo.checked values.
: :
: : I'm sorry for being so lenghthy, but it was difficult to explain.
: : I was sure I read somewhere's in one of my Books on how to do this, but I
: : could not find anything in my latest search.
: :
: : I really hope that it's something very simple that I over looked
: :
: : Thank you for any suggestions
: :
: : -mydelphi
: :
: Saving the controls into an inifile is a smart idea, since you can create a single section to hold all the values you want for a single control. The easiest way to code this is to create a descendant of a TIniFile or TMemIniFile, which adds several new methods to handle certain classes of controls. Here is an example in code.
:
: type
:   TControlsFile = class(TIniFile)
:   private
:     procedure LoadComboBox(Control: TComboBox);
:     procedure SaveComboBox(Control: TComboBox);
:     procedure LoadEdit(Control: TEdit);
:     procedure SaveEdit(Control: TEdit);
:   public
:     procedure LoadControls(Form: TForm);
:     procedure SaveControls(Form: TForm);
:   end;
: 
: procedure TControlsFile.LoadComboBox(Control: TComboBox);
: begin
:   with Control do begin
:     // first read if the list is sorted
:     Sorted := ReadBool(Name, 'Sorted', true);
:     // read all the items, which are automatically sorted (if sorted = true)
:     Items.CommaText := ReadString(Name, 'Values', 'DefaultValues');
:     // read the selected item
:     ItemIndex := Items.IndexOf(ReadString(Name, 'Selected', ''));
:     // add more properties as you like, by calling the correct ReadXXXX method
:   end;
: end;
: 
: procedure TControlsFile.LoadControls(Form: TForm);
: var
:   i: integer;
: begin
:   with Form do
:     if ComponentCount > 0 then
:       // loop through all the components on the form and
:       for i := 0 to ComponentCount-1 do
:         // and call the load routine for its class
:         if Components[i] is TComboBox then
:           LoadComboBox(Components[i] as TComboBox)
:         else if Components[i] is TEdit then
:           LoadEdit(Components[i] as TEdit);
:         // Add more control types by adding more "else-if-then"s
: end;
: 
: procedure TControlsFile.LoadEdit(Control: TEdit);
: var
:   s: string;
: begin
:   with Control do begin
:     // read the text
:     Text := ReadString(Name, 'Text', 'Some default text');
:     // read the font string
:     s := ReadString(Name, 'Font', 'MS Sans Serif@8@----@0@');
:     // set the font properties based on the font string
:     Font.Name := Copy(s, 1, Pos('@', s)-1);
:     Delete(s, 1, Pos('@', s));
:     Font.Size := StrToInt(Copy(s, 1, Pos('@', s)-1));
:     Delete(s, 1, Pos('@', s));
:     Font.Style := [];
:     if s[1] = '+' then
:       Font.Style := Font.Style + [fsBold];
:     if s[2] = '+' then
:       Font.Style := Font.Style + [fsItalic];
:     if s[3] = '+' then
:       Font.Style := Font.Style + [fsUnderline];
:     if s[4] = '+' then
:       Font.Style := Font.Style + [fsStrikeOut];
:     Delete(s, 1, Pos('@', s));
:     Font.Color := StrToInt(Copy(s, 1, Pos('@', s)-1));
:   end;
: end;
: 
: procedure TControlsFile.SaveComboBox(Control: TComboBox);
: begin
:   with Control do begin
:     // write the properties
:     WriteString(Name, 'Values', Items.CommaText);
:     WriteString(Name, 'Selected', Items[ItemIndex]);
:     WriteBool(Name, 'Sorted', Sorted);
:     // add more properties as you like, by calling the correct WriteXXXX method
:   end;
: end;
: 
: procedure TControlsFile.SaveControls(Form: TForm);
: var
:   i: integer;
: begin
:   with Form do
:     if ComponentCount > 0 then
:       // loop through all the components on the form and
:       for i := 0 to ComponentCount-1 do
:         // and call the save routine for its class
:         if Components[i] is TComboBox then
:           SaveComboBox(Components[i] as TComboBox)
:         else if Components[i] is TEdit then
:           SaveEdit(Components[i] as TEdit);
:         // Add more control types by adding more "else-if-then"s
: end;
: 
: procedure TControlsFile.SaveEdit(Control: TEdit);
: var
:   s: string;
: begin
:   with Control do begin
:     // write the text
:     WriteString(Name, 'Text', Text);
:     // copy the font properties into a single string
:     s := '----'; // assume normal style
:     if fsBold in Font.Style then // if style is bolded then
:       s[1] := '+'; // change the value in the string
:     if fsItalic in Font.Style then // same with italic
:       s[2] := '+';
:     if fsUnderline in Font.Style then
:       s[3] := '+';
:     if fsStrikeOut in Font.Style then
:       s[4] := '+';
:     // compile the complete font string
:     s := Format('%s@%d@%s@%d@', [Font.Name, Font.Size, s, Integer(Font.Color)]); 
:     // write that string
:     WriteString(Name, 'Font', s);
:   end;
: end;
: 

: This example will save and load the properties of all the TComboBoxes and TEdits on a single form. The format of the resulting file will be something like this:
:
: [Edit1]
: Text=Here is the line typed into edit1
: Font=Arial@8@+---@0@
: 
: [Edit2]
: Text=Here is the line typed into edit2
: Font=MS Sans Serif@8@----@0@
: 
: [ComboBox1]
: Values="Value 1","Value 2","Value 4","Value 3"
: Selected=Value 3
: Sorted=false
: 

: This example file should fill the Edit1 with a bold Arial 8 pt. text, while Edit2 has a normal MS Sans Serif 8 pt. text.
:
: This example should give you an idea, of how to save the properties of every control you wish. Sometimes you can take a small shortcut in the coding of the load/save routine. If you don't want to save the fonts of your editboxes, you could use the TCustomEdit for your load/save routines. This will then also be used to save all edits and memos.
:
:

Report
Re: How do I STORE Control values or properties in a file ? Posted by zibadian on 20 Jul 2003 at 2:59 PM
: I've got some news.
:
: Finally. After countless what ifs, I got something working. But, I have
: another issue. I can't get it to Load bACK into my controls. I need
: help with this. anyway, here is what I got so far, and it works as far
: as I can tell, when I, at the DOS prompt c:\type ctrldef.ini, it will
: display a bunch of settings and things.
: [i]I've simplyied it a little ie, t:=tonc... etc. less typing.[/i]
:
:
:   // write out the data
:   t:=tcontrolsFile.Create('h:\ctrldef.ini');
:   t.SaveControls(form1);
: 

:
:
: So, I've made some more progress. great.
:
: Now, I did notice something. my combobox has some funny char in it or
: will mess up my original values when I try and load it with the Saved
: ctrldef.ini contents:
:
:
:   // read in the data
:   t:=tcontrolsFile.Create('h:\ctrldef.ini');
:   t.LoadControls(form1);
: 

:
: Now, as I was saying, the controls aren't properly loading back. I need
: some help with this. If anyone else would like ta help, plase do so.
: I'd really appreciate it
:
: I am worried about one more thing though. How to I ADD to my combobox
: list of items ?
: I'm going to be doing this, using a separate Textbox (blank by default,
: until user enters somerthing into it, and a NEW item should be added to
: the list when user clicks on the Save button (or SaveTemplate button)
:
: Thanks again.
: -mydelphi
:
I'm sorry for not getting back to you earlier. I was too busy with one of my own projects to check the board.

The IniFiles:
Could you post the inifile on this board, so I can see what the program saved? Note: you can double-click an inifile in the explorer and it will open in the notepad.
Also: NEVER call a variable TSomething, since delphi uses the "T" in front of a name to identify a variable type. So your tInifile is already been declared as an object type.
There is a good example of how to create an inifile in the TCustomIniFile.Create() help.
The order of the load and save procedures of the object declaration is unimportant, but the order in the if-then statements could be. Here are two code snippits (in the SaveControls), which save a TMemo differently:
   ...
    else if Components[i] is TCustomEdit then
      SaveCustomEdit(Components[i] as TCustomEdit)
    else if Components[i] is TMemo then
      SaveCustomEdit(Components[i] as TMemo)
   ...

Look into the heirarchy of the TMemo, and you'll see that it is a descendant of a TCustomEdit. This means that the condition "Components[i] is TCustomEdit" will return TRUE for a TMemo, thus saving the memo as an edit instead of a memo.
   ...
    else if Components[i] is TMemo then
      SaveCustomEdit(Components[i] as TMemo)
    else if Components[i] is TCustomEdit then
      SaveCustomEdit(Components[i] as TCustomEdit)
   ...

In this code the memo is saved as a TMemo, and all other edit boxes are saved as a TCustomEdit.
Also I think that the memo might not be saved correctly, because of the line endings in the Text property. A simple solution would be to use the StringReplace() function to replace the line endings (#13#10) with another characters (example: #2), when you save it. Upon loading it you need to replace all those characters back to line endings.
Question: after loading the ini file, do you free the TControlFile object again? You should otherwise, when creating a new one you might cause some memory conflict.

Adding items to a combobox:
The combobox has a property called Items. This is a key property when trying to change the items. According to the help files it is a TStrings object, and thus has several interesting methods including an Add(), Delete(), Insert(), etc...
Hint: when faced with such a problem in the future, just look in the help files if you can find amethod which does what you want (in this case Add()) in the method list of the object (ie. TComboBox). If that fails look at the most likely property (ie. Items). These are often objects themselves, and might have such a method. It will almost always give you a result within a few minutes.
Report
Re: How do I STORE Control values or properties in a file ? Posted by mydelphi on 20 Jul 2003 at 4:55 PM
This message was edited by mydelphi at 2003-7-20 17:2:34

Hi zibadian

Thanks for your response. I'm still at it over here. Driving me crazy
with this last hangup with comboboxs.

I have a great idea. Why don't I POST the whole source code. Easy. I
re-created an NEW app, but striping out all other code not related.
It's a very small app when you right down to it now.

I think that once you see the code below in action, you'll understand
what problems I'm having. I just can't figure it out. I"m hopping
please, that you know how to fix.

Thank you for any help you can share.
-mydelphi

First, perform the following, ok ?
------------------------------------------------------


1, Start an new application (and shrink the Form window)
2, add in the following controls - easy.

A - ONE textbox --- edit1 -------- no events needed here
B - ONE combobox -- combobox1 ---- no events needed here
C - ONE Button ---- [SaveIni] ---- onClick event -- to call SaveIni
D - ONE Button ---- [RetreaveIni]- onClick event -- to call ReteaveIni

You should have 4 items all together on your Form

A = any text you feel like typing in. ie, "TEST #1"

B = go into the ComboBox1's Object Inspector and double-click the
Items: (TStrings) [...] and enter in these two items:

100 - movies
200 - trips

C = procedure TForm1.SaveIniClick(Sender: TObject);

D = procedure TForm1.ReteaveIniClick(Sender: TObject);

3, Now, copy the source code below, into your project. However way
you know how, without problems. Basically, you should replace all
your code with this one below. I figure you're a guru, and won't
have any problems

Note, when you click on the [SaveIni] button, all will look fine, and
save etc. When you then, click on the [RetreaveIni] button, that's
when things look a bit funny. Edit1 retreaves fine, but ComboBox1,
when you go into it, looks all messed up.

Anybody should be able to duplicate this project very easy.

Good luck finding this out. If I'm missing anything in my source,
please don't hesitage to let me now. Also, I'm missing the code for
the CheckBox controls. I need to store those as well.

Thanking you in advance for everything
----------------------------------------------------------

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, iniFiles;

type
  TForm1 = class(TForm)
    SaveIni: TButton;
    Edit1: TEdit;
    ComboBox1: TComboBox;
    ReteaveIni: TButton;
    procedure SaveIniClick(Sender: TObject);
    procedure ReteaveIniClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

type
  TControlsFile = class(TIniFile)
  private
    procedure LoadComboBox(Control: TComboBox);
    procedure SaveComboBox(Control: TComboBox);
    procedure LoadEdit(Control: TEdit);
    procedure SaveEdit(Control: TEdit);
  public
    procedure LoadControls(Form: TForm);
    procedure SaveControls(Form: TForm);
  end;

procedure TControlsFile.LoadComboBox(Control: TComboBox);
begin
  with Control do begin
    // first read if the list is sorted
    Sorted := ReadBool(Name, 'Sorted', true);
    // read all the items, which are automatically sorted (if sorted = true)
    Items.commaText := (ReadString(Name, 'Values', 'DefaultValues')); // <--- cause of prob
    // read the selected item
    ItemIndex := Items.IndexOf(ReadString(Name, 'Selected', ''));
    // add more properties as you like, by calling the correct ReadXXXX method
  end;
end;
procedure TControlsFile.LoadControls(Form: TForm);
var
  i: integer;
begin
  with Form do
    if ComponentCount > 0 then
      // loop through all the components on the form and
      for i := 0 to ComponentCount-1 do
        // and call the load routine for its class
        if Components[i] is TComboBox then
          LoadComboBox(Components[i] as TComboBox)
        else if Components[i] is TEdit then
          LoadEdit(Components[i] as TEdit);
        // Add more control types by adding more "else-if-then"s
end;
procedure TControlsFile.LoadEdit(Control: TEdit);
var
  s: string;
begin
  with Control do begin
    // read the text
    Text := ReadString(Name, 'Text', 'Some default text');
    // read the font string
    s := ReadString(Name, 'Font', 'MS Sans Serif@8@----@0@');
    // set the font properties based on the font string
    Font.Name := Copy(s, 1, Pos('@', s)-1);
    Delete(s, 1, Pos('@', s));
    Font.Size := StrToInt(Copy(s, 1, Pos('@', s)-1));
    Delete(s, 1, Pos('@', s));
    Font.Style := [];
    if s[1] = '+' then
      Font.Style := Font.Style + [fsBold];
    if s[2] = '+' then
      Font.Style := Font.Style + [fsItalic];
    if s[3] = '+' then
      Font.Style := Font.Style + [fsUnderline];
    if s[4] = '+' then
      Font.Style := Font.Style + [fsStrikeOut];
    Delete(s, 1, Pos('@', s));
    Font.Color := StrToInt(Copy(s, 1, Pos('@', s)-1));
  end;
end;
procedure TControlsFile.SaveComboBox(Control: TComboBox);
begin
  with Control do begin
    // write the properties
    WriteString(Name, 'Values', Items.CommaText);
    WriteString(Name, 'Selected', Items[ItemIndex]);
    WriteBool(Name, 'Sorted', Sorted);
    // add more properties as you like, by calling the correct WriteXXXX method
  end;
end;
procedure TControlsFile.SaveControls(Form: TForm);
var
  i: integer;
begin
  with Form do
    if ComponentCount > 0 then
      // loop through all the components on the form and
      for i := 0 to ComponentCount-1 do
        // and call the save routine for its class
        if Components[i] is TComboBox then
          SaveComboBox(Components[i] as TComboBox)
        else if Components[i] is TEdit then
          SaveEdit(Components[i] as TEdit);
        // Add more control types by adding more "else-if-then"s
end;
procedure TControlsFile.SaveEdit(Control: TEdit);
var
  s: string;
begin
  with Control do begin
    // write the text
    WriteString(Name, 'Text', Text);
    // copy the font properties into a single string
    s := '----'; // assume normal style
    if fsBold in Font.Style then // if style is bolded then
      s[1] := '+'; // change the value in the string
    if fsItalic in Font.Style then // same with italic
      s[2] := '+';
    if fsUnderline in Font.Style then
      s[3] := '+';
    if fsStrikeOut in Font.Style then
      s[4] := '+';
    // compile the complete font string
    s := Format('%s@%d@%s@%d@', [Font.Name, Font.Size, s, Integer(Font.Color)]);
    // write that string
    WriteString(Name, 'Font', s);
  end;
end;

var
  myT : TControlsFile;

procedure TForm1.SaveIniClick(Sender: TObject);
begin
  myT:=tcontrolsFile.Create('c:\d.ini');
  myT.SaveControls(form1);
end;

procedure TForm1.ReteaveIniClick(Sender: TObject);
begin
  myT:=tcontrolsFile.Create('c:\d.ini');
  myT.LoadControls(form1);
end;

end.

This is what the DOS ini file looks like:
C:\>type d.ini
[Edit1]
Text=Test #1
Font=MS Sans Serif@8@----@-2147483640@

[ComboBox1]
Values="100 - movies","200 - trips"
Selected=100 - movies
Sorted=0
C:\>


-------------------------------------


: : I've got some news.
: :
: : Finally. After countless what ifs, I got something working. But, I have
: : another issue. I can't get it to Load bACK into my controls. I need
: : help with this. anyway, here is what I got so far, and it works as far
: : as I can tell, when I, at the DOS prompt c:\type ctrldef.ini, it will
: : display a bunch of settings and things.
: : [i]I've simplyied it a little ie, t:=tonc... etc. less typing.[/i]
: :
: :
: :   // write out the data
: :   t:=tcontrolsFile.Create('h:\ctrldef.ini');
: :   t.SaveControls(form1);
: : 

: :
: :
: : So, I've made some more progress. great.
: :
: : Now, I did notice something. my combobox has some funny char in it or
: : will mess up my original values when I try and load it with the Saved
: : ctrldef.ini contents:
: :
: :
: :   // read in the data
: :   t:=tcontrolsFile.Create('h:\ctrldef.ini');
: :   t.LoadControls(form1);
: : 

: :
: : Now, as I was saying, the controls aren't properly loading back. I need
: : some help with this. If anyone else would like ta help, plase do so.
: : I'd really appreciate it
: :
: : I am worried about one more thing though. How to I ADD to my combobox
: : list of items ?
: : I'm going to be doing this, using a separate Textbox (blank by default,
: : until user enters somerthing into it, and a NEW item should be added to
: : the list when user clicks on the Save button (or SaveTemplate button)
: :
: : Thanks again.
: : -mydelphi
: :
: I'm sorry for not getting back to you earlier. I was too busy with one of my own projects to check the board.
:
: The IniFiles:
: Could you post the inifile on this board, so I can see what the program saved? Note: you can double-click an inifile in the explorer and it will open in the notepad.
: Also: NEVER call a variable TSomething, since delphi uses the "T" in front of a name to identify a variable type. So your tInifile is already been declared as an object type.
: There is a good example of how to create an inifile in the TCustomIniFile.Create() help.
: The order of the load and save procedures of the object declaration is unimportant, but the order in the if-then statements could be. Here are two code snippits (in the SaveControls), which save a TMemo differently:
:
:    ...
:     else if Components[i] is TCustomEdit then
:       SaveCustomEdit(Components[i] as TCustomEdit)
:     else if Components[i] is TMemo then
:       SaveCustomEdit(Components[i] as TMemo)
:    ...
: 

: Look into the heirarchy of the TMemo, and you'll see that it is a descendant of a TCustomEdit. This means that the condition "Components[i] is TCustomEdit" will return TRUE for a TMemo, thus saving the memo as an edit instead of a memo.
:
:    ...
:     else if Components[i] is TMemo then
:       SaveCustomEdit(Components[i] as TMemo)
:     else if Components[i] is TCustomEdit then
:       SaveCustomEdit(Components[i] as TCustomEdit)
:    ...
: 

: In this code the memo is saved as a TMemo, and all other edit boxes are saved as a TCustomEdit.
: Also I think that the memo might not be saved correctly, because of the line endings in the Text property. A simple solution would be to use the StringReplace() function to replace the line endings (#13#10) with another characters (example: #2), when you save it. Upon loading it you need to replace all those characters back to line endings.
: Question: after loading the ini file, do you free the TControlFile object again? You should otherwise, when creating a new one you might cause some memory conflict.
:
: Adding items to a combobox:
: The combobox has a property called Items. This is a key property when trying to change the items. According to the help files it is a TStrings object, and thus has several interesting methods including an Add(), Delete(), Insert(), etc...
: Hint: when faced with such a problem in the future, just look in the help files if you can find amethod which does what you want (in this case Add()) in the method list of the object (ie. TComboBox). If that fails look at the most likely property (ie. Items). These are often objects themselves, and might have such a method. It will almost always give you a result within a few minutes.
:



Report
Re: How do I STORE Control values or properties in a file ? Posted by mydelphi on 20 Jul 2003 at 3:28 PM
Here's an update to what I got so far

Textboxes save and retreave perfectly. Success there.

Comboboxes is something else.

Everything seems to save ok. The ini file generated looks pretty perfect
and clean and well layed out. Here is how it's stored in the INI:

: [ComboBox1]
: Values="100 - movies","200 - trips"
: Selected=200 - trips
: Sorted=0

: [ComboBox2]
: Values="1 - Value","2 - Value"
: Selected=1 - Value
: Sorted=0



Here's the messy part.
After the data is retreaved, the dropdown is suppose to show the
following, inside the combobox1:

100 - movies
200 - trips



But, instead, when I retreave this data back, the comboboxs are garbled.
When I click on the dropdown, (and as an example) this is what ends up
showing in the combobox1 list:

100
-
movies"
200 - trip


Note, that Combobox2 is the same problem. And, notice how the last
item in the list (ie, 200 - trip) is missing it's final chars.
The "S" got cut off.

Other than that, the code works perfect! Thank you again.

So far, I need to do some things:
1, fix the combobox problems,
2, create an INI for checkbox controls (not implemented yet), and
3, add in a feature to ADD new items to the list in comboboxs
and SAVE them.

Much help is appreciated. Thanks again.
-mydelphi



The last item to add to the ini file is the checkboxes controls.

: I've got some news.
:
: Finally. After countless what ifs, I got something working. But, I have
: another issue. I can't get it to Load bACK into my controls. I need
: help with this. anyway, here is what I got so far, and it works as far
: as I can tell, when I, at the DOS prompt c:\type ctrldef.ini, it will
: display a bunch of settings and things.
: [i]I've simplyied it a little ie, t:=tonc... etc. less typing.[/i]
:
:
:   // write out the data
:   t:=tcontrolsFile.Create('h:\ctrldef.ini');
:   t.SaveControls(form1);
: 

:
:
: So, I've made some more progress. great.
:
: Now, I did notice something. my combobox has some funny char in it or
: will mess up my original values when I try and load it with the Saved
: ctrldef.ini contents:
:
:
:   // read in the data
:   t:=tcontrolsFile.Create('h:\ctrldef.ini');
:   t.LoadControls(form1);
: 

:
: Now, as I was saying, the controls aren't properly loading back. I need
: some help with this. If anyone else would like ta help, plase do so.
: I'd really appreciate it
:
: I am worried about one more thing though. How to I ADD to my combobox
: list of items ?
: I'm going to be doing this, using a separate Textbox (blank by default,
: until user enters somerthing into it, and a NEW item should be added to
: the list when user clicks on the Save button (or SaveTemplate button)
:
: Thanks again.
: -mydelphi
:
:
:
: : : Hi,
: : :
: : : I'm looking for some suggestions.
: : :
: : : I've pretty much exhausted myself of a theral search in all of my
: : : libraries of books for any example or hints or whatever,
: : :
: : : Mastering Delphi 3
: : : Secrets of Delphi 2
: : : Delphi 3 Super Bible
: : : And several other books
: : : Plus, the Internet (doing actual web searches)
: : :
: : : So, here I am, asking some more questions.
: : :
: : : Ok then, I have, yet another problem. I'm trying to STORE my controls
: : : values into a file. I've mimiced ONLY the controls values/property
: : : that I will end up using.
: : :
: : : I've thought about using the TIniFle unit and save to and INI file,
: : : and this would be simple, if all I was doing was saying ONE dementional
: : : strings, Intergers, even checkbox values ie, .checked=boolean,
: : : but that turned out to be a bad idea. If I have a combobox for instance,
: : : with a list of items, say 15 of them, how am I going to store them in this
: : : INI file structure, and then put them back into the combobox later, and
: : : I go to retreave the complete file (control structure on my form)
: : :
: : : I would love to just store the ALL the controls AND their values from my
: : : form, and just recall them as they were last set, but after trying that,
: : : using a file structure below, either my system would crash or I did not
: : : get back my values (they did not get stored?)
: : :
: : : Example:
: : :
: : :
: : : Type
: : :   TmyControls = record
: : :                   txtTmpListLastStr: String[100]; // last highlisted string
: : :                   cbTmpList: TCombobox;
: : :                    chbYesNo: TCheckbox;
: : :                     txtSize: Integer;                  
: : :                 end;
: : : 
: : : var
: : :   F : file of TmyControls;
: : :   myControlsRecStruc: TmyControls;
: : : .
: : : .
: : :   myControlsRecStruc.txtTmpListLastStr := form1.txtTmpListLastStr;
: : :           myControlsRecStruc.cbTmpList := form1.cbTmpList.checked;
: : :            myControlsRecStruc.chbYesNo := form1.chbYesNo;
: : :             myControlsRecStruc.txtSize := form1.txtSize;
: : : .
: : : .
: : :   // Write the data to our file structure
: : :   assign(f,'h:\ctrldef.ini');
: : :     rewrite(f);
: : :       write(f,myControlsRecStruc);
: : :   close(f);
: : : 
: : :   // Read the data back, from our file structure, into our Controls
: : :   assign(f,'h:\def.ini');
: : :     reset(f);
: : :     read(f,myControlsRecStruc);
: : :   close(f);
: : : .
: : : .
: : :   // populate our Controls from file structure here.
: : : .
: : : .
: : : 

: : :
: : : Not to forget that all the above are Controls on my form.
: : :
: : : but the above presents too many problems:
: : : 1 - combobox: If I add a new item (which I will) how to they get inserted
: : : and properly sorted. If I have a mechanism for storing the last position
: : : or selection in the combobox, and I go and insert a new item into this
: : : list, won't I throw off the order AND the new item ?
: : : 2 - some of my controls are mixed values. Example, there are some
: : : Intergers, Strings (only works if I say String[nnn], NOT String) and
: : : checkboxes. I use/store the chbYesNo.checked values.
: : :
: : : I'm sorry for being so lenghthy, but it was difficult to explain.
: : : I was sure I read somewhere's in one of my Books on how to do this, but I
: : : could not find anything in my latest search.
: : :
: : : I really hope that it's something very simple that I over looked
: : :
: : : Thank you for any suggestions
: : :
: : : -mydelphi
: : :
: : Saving the controls into an inifile is a smart idea, since you can create a single section to hold all the values you want for a single control. The easiest way to code this is to create a descendant of a TIniFile or TMemIniFile, which adds several new methods to handle certain classes of controls. Here is an example in code.
: :
: : type
: :   TControlsFile = class(TIniFile)
: :   private
: :     procedure LoadComboBox(Control: TComboBox);
: :     procedure SaveComboBox(Control: TComboBox);
: :     procedure LoadEdit(Control: TEdit);
: :     procedure SaveEdit(Control: TEdit);
: :   public
: :     procedure LoadControls(Form: TForm);
: :     procedure SaveControls(Form: TForm);
: :   end;
: : 
: : procedure TControlsFile.LoadComboBox(Control: TComboBox);
: : begin
: :   with Control do begin
: :     // first read if the list is sorted
: :     Sorted := ReadBool(Name, 'Sorted', true);
: :     // read all the items, which are automatically sorted (if sorted = true)
: :     Items.CommaText := ReadString(Name, 'Values', 'DefaultValues');
: :     // read the selected item
: :     ItemIndex := Items.IndexOf(ReadString(Name, 'Selected', ''));
: :     // add more properties as you like, by calling the correct ReadXXXX method
: :   end;
: : end;
: : 
: : procedure TControlsFile.LoadControls(Form: TForm);
: : var
: :   i: integer;
: : begin
: :   with Form do
: :     if ComponentCount > 0 then
: :       // loop through all the components on the form and
: :       for i := 0 to ComponentCount-1 do
: :         // and call the load routine for its class
: :         if Components[i] is TComboBox then
: :           LoadComboBox(Components[i] as TComboBox)
: :         else if Components[i] is TEdit then
: :           LoadEdit(Components[i] as TEdit);
: :         // Add more control types by adding more "else-if-then"s
: : end;
: : 
: : procedure TControlsFile.LoadEdit(Control: TEdit);
: : var
: :   s: string;
: : begin
: :   with Control do begin
: :     // read the text
: :     Text := ReadString(Name, 'Text', 'Some default text');
: :     // read the font string
: :     s := ReadString(Name, 'Font', 'MS Sans Serif@8@----@0@');
: :     // set the font properties based on the font string
: :     Font.Name := Copy(s, 1, Pos('@', s)-1);
: :     Delete(s, 1, Pos('@', s));
: :     Font.Size := StrToInt(Copy(s, 1, Pos('@', s)-1));
: :     Delete(s, 1, Pos('@', s));
: :     Font.Style := [];
: :     if s[1] = '+' then
: :       Font.Style := Font.Style + [fsBold];
: :     if s[2] = '+' then
: :       Font.Style := Font.Style + [fsItalic];
: :     if s[3] = '+' then
: :       Font.Style := Font.Style + [fsUnderline];
: :     if s[4] = '+' then
: :       Font.Style := Font.Style + [fsStrikeOut];
: :     Delete(s, 1, Pos('@', s));
: :     Font.Color := StrToInt(Copy(s, 1, Pos('@', s)-1));
: :   end;
: : end;
: : 
: : procedure TControlsFile.SaveComboBox(Control: TComboBox);
: : begin
: :   with Control do begin
: :     // write the properties
: :     WriteString(Name, 'Values', Items.CommaText);
: :     WriteString(Name, 'Selected', Items[ItemIndex]);
: :     WriteBool(Name, 'Sorted', Sorted);
: :     // add more properties as you like, by calling the correct WriteXXXX method
: :   end;
: : end;
: : 
: : procedure TControlsFile.SaveControls(Form: TForm);
: : var
: :   i: integer;
: : begin
: :   with Form do
: :     if ComponentCount > 0 then
: :       // loop through all the components on the form and
: :       for i := 0 to ComponentCount-1 do
: :         // and call the save routine for its class
: :         if Components[i] is TComboBox then
: :           SaveComboBox(Components[i] as TComboBox)
: :         else if Components[i] is TEdit then
: :           SaveEdit(Components[i] as TEdit);
: :         // Add more control types by adding more "else-if-then"s
: : end;
: : 
: : procedure TControlsFile.SaveEdit(Control: TEdit);
: : var
: :   s: string;
: : begin
: :   with Control do begin
: :     // write the text
: :     WriteString(Name, 'Text', Text);
: :     // copy the font properties into a single string
: :     s := '----'; // assume normal style
: :     if fsBold in Font.Style then // if style is bolded then
: :       s[1] := '+'; // change the value in the string
: :     if fsItalic in Font.Style then // same with italic
: :       s[2] := '+';
: :     if fsUnderline in Font.Style then
: :       s[3] := '+';
: :     if fsStrikeOut in Font.Style then
: :       s[4] := '+';
: :     // compile the complete font string
: :     s := Format('%s@%d@%s@%d@', [Font.Name, Font.Size, s, Integer(Font.Color)]); 
: :     // write that string
: :     WriteString(Name, 'Font', s);
: :   end;
: : end;
: : 

: : This example will save and load the properties of all the TComboBoxes and TEdits on a single form. The format of the resulting file will be something like this:
: :
: : [Edit1]
: : Text=Here is the line typed into edit1
: : Font=Arial@8@+---@0@
: : 
: : [Edit2]
: : Text=Here is the line typed into edit2
: : Font=MS Sans Serif@8@----@0@
: : 
: : [ComboBox1]
: : Values="Value 1","Value 2","Value 4","Value 3"
: : Selected=Value 3
: : Sorted=false
: : 

: : This example file should fill the Edit1 with a bold Arial 8 pt. text, while Edit2 has a normal MS Sans Serif 8 pt. text.
: :
: : This example should give you an idea, of how to save the properties of every control you wish. Sometimes you can take a small shortcut in the coding of the load/save routine. If you don't want to save the fonts of your editboxes, you could use the TCustomEdit for your load/save routines. This will then also be used to save all edits and memos.
: :
: :
:
:

Report
Re: How do I STORE Control values or properties in a file ? Posted by mydelphi on 20 Jul 2003 at 8:18 PM
This message was edited by mydelphi at 2003-7-20 20:21:20

Great news.

I've done it!

After hours and hours and hours and hours and.., well, you get the
picture. I've finally figured it all out - lots of reading, and thanks
to zibadian and "Delphi 3 Super Bible" (and few other books) I've
got my answers. My eyes are crossed, but worth it.
I think it's a bug, why I hit the snag, but it's finally working

Hay, I even managed to figure out setting up and adding in another
items for TCheckBox too. But, that took about 4 hours to figure too.
I had lots of snags with that one. But, anyways, it's fixed and working
rather nicely. I'd like to turn it into a unit or something, so that
I can maintain it for other projects, but I'm not that much of a guru
to figure it all out quickly enough.

Thanks to MY "complete object hierarchy" I was finally able to figure
out the proper order (insertion point) to place TCheckBox at in the
Loading of things etc. Turns out, it way up at the top of the tree
of TWinControl, and TForm is WAY down the bottom. I don't know why.

I'm still confused with this method of INI stuff, because I want to
create or implement a savings to "templates", and I just can't seem
to grasp how the TIniFile is going to do it for me. I must be missing
something here.

zibadian, any comments on this ?
Thanks everyone for baring with me (and those, that were coming for
the ride) much appreciated
-mydelphi


Report
Re: How do I STORE Control values or properties in a file ? Posted by zibadian on 20 Jul 2003 at 10:08 PM
: This message was edited by mydelphi at 2003-7-20 20:21:20

: Great news.
:
: I've done it!
:
: After hours and hours and hours and hours and.., well, you get the
: picture. I've finally figured it all out - lots of reading, and thanks
: to zibadian and "Delphi 3 Super Bible" (and few other books) I've
: got my answers. My eyes are crossed, but worth it.
: I think it's a bug, why I hit the snag, but it's finally working
:
: Hay, I even managed to figure out setting up and adding in another
: items for TCheckBox too. But, that took about 4 hours to figure too.
: I had lots of snags with that one. But, anyways, it's fixed and working
: rather nicely. I'd like to turn it into a unit or something, so that
: I can maintain it for other projects, but I'm not that much of a guru
: to figure it all out quickly enough.
:
: Thanks to MY "complete object hierarchy" I was finally able to figure
: out the proper order (insertion point) to place TCheckBox at in the
: Loading of things etc. Turns out, it way up at the top of the tree
: of TWinControl, and TForm is WAY down the bottom. I don't know why.
:
: I'm still confused with this method of INI stuff, because I want to
: create or implement a savings to "templates", and I just can't seem
: to grasp how the TIniFile is going to do it for me. I must be missing
: something here.
:
: zibadian, any comments on this ?
: Thanks everyone for baring with me (and those, that were coming for
: the ride) much appreciated
: -mydelphi
:
:
:
I've found the problem. The Inifile object itself removed the leading and trailing comment characters '"', and these are necessary for the CommaText property to be around strings with spaces and commas in it. Here is a small addition in the SaveComboBox() method to solve it:
    WriteString(Name, 'Values', '"'+Items.CommaText+'"');

Also you need to free your TControlFile objects after using them. Otherwise if you call it often enough you'll run out of memory. Here is a small updates RetrieveIniClick():
procedure TForm1.retrieveiniClick(Sender: TObject);
var
  ControlFile: TControlFile;
begin
  ControlFile := TControlsFile.Create('c:\d.ini');
  ControlFile.LoadControls(form1);
  ControlFile.Free;
end;

The same goes for the SaveIniClick(). Note: that I changed the object variable to a local variable. This is somewhat more in line with the Delphi convension about temporary variables, since this variable will only be necessary in 1 procedure only and will reduce errors.
Report
Re: How do I STORE Control values or properties in a file ? Posted by mydelphi on 20 Jul 2003 at 11:24 PM
bravo zibadian

Well, done. I figure out that I had to eliminate the " " spaced in my
strings, but I knew I had to find another way to use them. Now, I can
ues them again. Thank you for your researching and all.

Well, here's what I've ben using to FREE my resources or whatever you
call it:
  myT.Free;


But, since you recommend I use your method of local variables, I'm only
happy to comply

Hay, I've got finished incorporating your code suggestion, and it worked
like a charm yippy.

We've got success.
-mydelphi



: : This message was edited by mydelphi at 2003-7-20 20:21:20

: : Great news.
: :
: : I've done it!
: :
: : After hours and hours and hours and hours and.., well, you get the
: : picture. I've finally figured it all out - lots of reading, and thanks
: : to zibadian and "Delphi 3 Super Bible" (and few other books) I've
: : got my answers. My eyes are crossed, but worth it.
: : I think it's a bug, why I hit the snag, but it's finally working
: :
: : Hay, I even managed to figure out setting up and adding in another
: : items for TCheckBox too. But, that took about 4 hours to figure too.
: : I had lots of snags with that one. But, anyways, it's fixed and working
: : rather nicely. I'd like to turn it into a unit or something, so that
: : I can maintain it for other projects, but I'm not that much of a guru
: : to figure it all out quickly enough.
: :
: : Thanks to MY "complete object hierarchy" I was finally able to figure
: : out the proper order (insertion point) to place TCheckBox at in the
: : Loading of things etc. Turns out, it way up at the top of the tree
: : of TWinControl, and TForm is WAY down the bottom. I don't know why.
: :
: : I'm still confused with this method of INI stuff, because I want to
: : create or implement a savings to "templates", and I just can't seem
: : to grasp how the TIniFile is going to do it for me. I must be missing
: : something here.
: :
: : zibadian, any comments on this ?
: : Thanks everyone for baring with me (and those, that were coming for
: : the ride) much appreciated
: : -mydelphi
: :
: :
: :
: I've found the problem. The Inifile object itself removed the leading and trailing comment characters '"', and these are necessary for the CommaText property to be around strings with spaces and commas in it. Here is a small addition in the SaveComboBox() method to solve it:
:
:     WriteString(Name, 'Values', '"'+Items.CommaText+'"');
: 

: Also you need to free your TControlFile objects after using them. Otherwise if you call it often enough you'll run out of memory. Here is a small updates RetrieveIniClick():
:
: procedure TForm1.retrieveiniClick(Sender: TObject);
: var
:   ControlFile: TControlFile;
: begin
:   ControlFile := TControlsFile.Create('c:\d.ini');
:   ControlFile.LoadControls(form1);
:   ControlFile.Free;
: end;
: 

: The same goes for the SaveIniClick(). Note: that I changed the object variable to a local variable. This is somewhat more in line with the Delphi convension about temporary variables, since this variable will only be necessary in 1 procedure only and will reduce errors.
:




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.