Results 1 to 16 of 16

Thread: Yet Another SCAR Question (combobox's and INI's this time)

  1. #1
    Join Date
    Jun 2011
    Posts
    13
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default Yet Another SCAR Question (combobox's and INI's this time)

    Well, how would i go about populating a combo box with the values inside a INI?

    I have it currently working, but it is hardcoded for a certain amount of items in the ini, i want to make it so that if sum1 goes and edits the INI file, those changes also show up in the combobox.

    What i am thinking can work, but have no idea if its possible or how to implement it, is

    Create a array with a undefined amount of spaces
    let the array be populated until the end of section in INI is reached,
    then populate the combobox with the array

    is this possible, and how?
    or can it be done any way else?

    Well, thanks again for all the help villavu

  2. #2
    Join Date
    Mar 2006
    Location
    Behind you
    Posts
    3,193
    Mentioned
    61 Post(s)
    Quoted
    63 Post(s)

    Default

    Try something like this. You might need to convert to Scar it's wrote for Simba.

    Code:
    Procedure GetComboFromINI;
    var
      z, i :integer;
    begin
      Combo := TComboBox.Create(Form);
      with PCombo do
      begin
        Parent := Form;
        Top := 30;
        Left := 80;
        Width := 100;
        Height := 20;
        z := StrToInt(ReadINI('Name', 'Length', 'Data')); // Must be the total amount of entries
        For i := 0 to (z - 1) do
          ITEMS.Add(ReadINI('Name', IntToStr(i), 'Data'));
      end;
    end;
    ~BraK

    E: This way you have to edit the List amount in the INI along with the List of Items.
    Last edited by BraK; 06-21-2011 at 07:04 PM.

    "Sometimes User's don't need the Answer spelled out with Code. Sometimes all they need is guidance and explanation of the logic to get where they are going."

  3. #3
    Join Date
    May 2008
    Location
    Here :p
    Posts
    194
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Code:
    procedure INIToComboBox;
    var
      I: Integer;
      Str: String;
    begin
      repeat
        inc(i);//increase I
        Str := ReadINI(section, keyname + inttostr(I), FileName);//get option I note: will start at 1
        if str = '' then break;//if nothing there then break the loop
        Form.ComboBox.Items.Add(str);//add option to the combo box
      until I>100; //fail safe i should never hit 100
    end;
    somthing like that?

    :O someone beet me to it

  4. #4
    Join Date
    Mar 2006
    Location
    Behind you
    Posts
    3,193
    Mentioned
    61 Post(s)
    Quoted
    63 Post(s)

    Default

    I R Ninja

    Bazz will it return '' for a none existent ini Line?

    ~BraK

    E: I don't mess with INI's too much.
    Last edited by BraK; 06-21-2011 at 07:22 PM.

    "Sometimes User's don't need the Answer spelled out with Code. Sometimes all they need is guidance and explanation of the logic to get where they are going."

  5. #5
    Join Date
    Jan 2008
    Location
    10° north of Hell
    Posts
    2,035
    Mentioned
    65 Post(s)
    Quoted
    164 Post(s)

    Default

    Code:
    program _;
    
    function ArrayFromINI(const Section, Keyname, Filename: string; const Max: integer): TVariantArray;
    var
      I: integer;
      T: string;
    begin
      I := 0;
      while True do
      begin
        T := ReadINI(Section, KeyName + IntToStr(I), FileName);
    
        if (T <> '') then
        begin
          SetLength(Result, I + 1);
          Result[I] := T;
        end else
          Break;
    
        Inc(I);
        if ((I > Max) and (Max > 0)) then
          Break;
      end;
    end;
    
    procedure ArrayToINI(const Arr: TVariantArray; const Section, KeyName, Filename: string);
    var
      I, H: integer;
    begin
      H := High(Arr);
      for I := 0 to H do
        WriteINI(Section, KeyName + IntToStr(I), ToStr(Arr[I]), Filename);
    end;
    
    begin
      ArrayToINI(['test'], 'Test', 'Wha', ScriptPath + 'data.ini');
      WriteLn(ArrayFromINI('Test', 'Wha', ScriptPath + 'data.ini', -1));
    end.

    Dg's Small Procedures | IRC Quotes
    Thank Wishlah for my nice new avatar!
    Quote Originally Posted by IRC
    [22:12:05] <Dgby714> Im agnostic
    [22:12:36] <Blumblebee> :O ...you can read minds

  6. #6
    Join Date
    Mar 2006
    Location
    Behind you
    Posts
    3,193
    Mentioned
    61 Post(s)
    Quoted
    63 Post(s)

    Default

    Pssh I knew Dgby714 would come in with a whole list of functions

    ~BraK

    "Sometimes User's don't need the Answer spelled out with Code. Sometimes all they need is guidance and explanation of the logic to get where they are going."

  7. #7
    Join Date
    Jan 2008
    Location
    10° north of Hell
    Posts
    2,035
    Mentioned
    65 Post(s)
    Quoted
    164 Post(s)

    Default

    Quote Originally Posted by BraK View Post
    Pssh I knew Dgby714 would come in with a whole list of functions

    ~BraK
    =P I lost my priv in #SRL-School btw

    Dg's Small Procedures | IRC Quotes
    Thank Wishlah for my nice new avatar!
    Quote Originally Posted by IRC
    [22:12:05] <Dgby714> Im agnostic
    [22:12:36] <Blumblebee> :O ...you can read minds

  8. #8
    Join Date
    Jun 2011
    Posts
    13
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    will test it now.

    im quite new here, is there a way to add rep to you guys? because i must say this is THE best forums when it comes to speedy responses, its almost instant. You guys surprise me every time xD

    Thanks again

  9. #9
    Join Date
    Jan 2008
    Location
    10° north of Hell
    Posts
    2,035
    Mentioned
    65 Post(s)
    Quoted
    164 Post(s)

    Default

    Quote Originally Posted by endlesslyonline View Post
    will test it now.

    im quite new here, is there a way to add rep to you guys? because i must say this is THE best forums when it comes to speedy responses, its almost instant. You guys surprise me every time xD

    Thanks again
    There is a little blue check box to the upper-right of the post.

    =)

    Dg's Small Procedures | IRC Quotes
    Thank Wishlah for my nice new avatar!
    Quote Originally Posted by IRC
    [22:12:05] <Dgby714> Im agnostic
    [22:12:36] <Blumblebee> :O ...you can read minds

  10. #10
    Join Date
    Mar 2006
    Location
    Behind you
    Posts
    3,193
    Mentioned
    61 Post(s)
    Quoted
    63 Post(s)

    Default

    This tutorial will teach you about Reputation. Here.

    ~BraK

    E: ninja'd by Dgby714 add me on MSN you

    "Sometimes User's don't need the Answer spelled out with Code. Sometimes all they need is guidance and explanation of the logic to get where they are going."

  11. #11
    Join Date
    Jun 2011
    Posts
    13
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by Dgby714 View Post
    Code:
    program _;
    
    function ArrayFromINI(const Section, Keyname, Filename: string; const Max: integer): TVariantArray;
    var
      I: integer;
      T: string;
    begin
      I := 0;
      while True do
      begin
        T := ReadINI(Section, KeyName + IntToStr(I), FileName);
    
        if (T <> '') then
        begin
          SetLength(Result, I + 1);
          Result[I] := T;
        end else
          Break;
    
        Inc(I);
        if ((I > Max) and (Max > 0)) then
          Break;
      end;
    end;
    
    procedure ArrayToINI(const Arr: TVariantArray; const Section, KeyName, Filename: string);
    var
      I, H: integer;
    begin
      H := High(Arr);
      for I := 0 to H do
        WriteINI(Section, KeyName + IntToStr(I), ToStr(Arr[I]), Filename);
    end;
    
    begin
      ArrayToINI(['test'], 'Test', 'Wha', ScriptPath + 'data.ini');
      WriteLn(ArrayFromINI('Test', 'Wha', ScriptPath + 'data.ini', -1));
    end.
    while running this i get an error "unknown identifier ToStr(Arr[I]) is that a type? shoud it be intToStr?
    *edit : if i change it to intToStr, i get a type mismatch at the last line

  12. #12
    Join Date
    Jan 2008
    Location
    10° north of Hell
    Posts
    2,035
    Mentioned
    65 Post(s)
    Quoted
    164 Post(s)

    Default

    Quote Originally Posted by endlesslyonline View Post
    while running this i get an error "unknown identifier ToStr(Arr[I]) is that a type? shoud it be intToStr?
    *edit : if i change it to intToStr, i get a type mismatch at the last line
    Dang... um I forgot this is in the SCAR section =O, make it a TStringArray and take away the ToStr

    ToStr is a magic function in Simba, hmm i might try to make one in SCAR using format =)

    Code:
    program _;
    
    function ArrayFromINI(const Section, Keyname, Filename: string; const Max: integer): TStringArray;
    var
      I: integer;
      T: string;
    begin
      I := 0;
      while True do
      begin
        T := ReadINI(Section, KeyName + IntToStr(I), FileName);
    
        if (T <> '') then
        begin
          SetLength(Result, I + 1);
          Result[I] := T;
        end else
          Break;
    
        Inc(I);
        if ((I > Max) and (Max > 0)) then
          Break;
      end;
    end;
    
    procedure ArrayToINI(const Arr: TStringArray; const Section, KeyName, Filename: string);
    var
      I, H: integer;
    begin
      H := High(Arr);
      for I := 0 to H do
        WriteINI(Section, KeyName + IntToStr(I), Arr[I], Filename);
    end;
    
    begin
      ArrayToINI(['test'], 'Test', 'Wha', ScriptPath + 'data.ini');
      WriteLn(ArrayFromINI('Test', 'Wha', ScriptPath + 'data.ini', -1));
    end.
    Last edited by Dgby714; 06-21-2011 at 09:05 PM.

    Dg's Small Procedures | IRC Quotes
    Thank Wishlah for my nice new avatar!
    Quote Originally Posted by IRC
    [22:12:05] <Dgby714> Im agnostic
    [22:12:36] <Blumblebee> :O ...you can read minds

  13. #13
    Join Date
    Jun 2011
    Posts
    13
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    um, okay, i can see where this is going, maybe i forgot to mention something, or i am reading it wrong.

    this is what my INI looks like:

    Code:
    [monster]
    goat=2324
    sheep=45463
    pig=5442
    wrath=674
    i want to populate the combo with the names of the monsters, but by the looks of these functions, it looks like its assuming the keyname is in number format? correct me if i am wrong

  14. #14
    Join Date
    Jan 2008
    Location
    10° north of Hell
    Posts
    2,035
    Mentioned
    65 Post(s)
    Quoted
    164 Post(s)

    Default

    Quote Originally Posted by endlesslyonline View Post
    um, okay, i can see where this is going, maybe i forgot to mention something, or i am reading it wrong.

    this is what my INI looks like:

    Code:
    [monster]
    goat=2324
    sheep=45463
    pig=5442
    wrath=674
    i want to populate the combo with the names of the monsters, but by the looks of these functions, it looks like its assuming the keyname is in number format? correct me if i am wrong
    Why not use SaveToFile? (Sorry if this is not in SCAR) Like say ComboBox.Lines.SaveToFile and LoadFromFile =)
    Actually that will look weird with the numbers


    Code:
    program _;
    
    var
      Data: TStringList;
    
    procedure Change(Sender: TObject);
    begin
      TForm(TComboBox(Sender).Parent).Caption := 'Test: ' + Data.Values[TComboBox(Sender).Text];
    end;
    
    procedure FormInit;
    var
      Form: TForm;
      Combo: TComboBox;
      I: integer;
    begin
      Form := TForm.Create(nil);
      with Form do
      begin
        Caption := 'Test';
        Width := 200;
        Height := 22;
      end;
    
      Data := TStringList.Create;
      //Data.LoadFromFile('...');
      Data.Values['Sheep'] := '1';
      Data.Values['Cat'] := '2';
      Data.Values['Dgby714'] := '3';
      Data.Values['BraK'] := '4';
      Data.Values['Dog'] := '5';
    
      Combo := TComboBox.Create(Form);
      with Combo do
      begin
        Parent := Form;
        Align := alClient;
        Style := csDropDownList;
    
        for I := 0 to Data.Count - 1 do
          Items.Add(Data.Names[I]);
    
        OnChange := @Change;
      end;
    
      Form.ShowModal;
    
      Data.Free;
      Form.Free;
    end;
    
    var
      V: TVariantArray;
    
    begin
      ThreadSafeCall('FormInit', V);
    end.
    OffTopic: BraK, Your not responding =P
    Last edited by Dgby714; 06-21-2011 at 09:54 PM.

    Dg's Small Procedures | IRC Quotes
    Thank Wishlah for my nice new avatar!
    Quote Originally Posted by IRC
    [22:12:05] <Dgby714> Im agnostic
    [22:12:36] <Blumblebee> :O ...you can read minds

  15. #15
    Join Date
    Jun 2011
    Posts
    13
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    i think i have seen the saveToFile and loadFromFile in SCAR, will check.

    Thanks again for all the responses.

  16. #16
    Join Date
    Feb 2006
    Location
    Belgium
    Posts
    3,137
    Mentioned
    3 Post(s)
    Quoted
    5 Post(s)

    Default

    On a side-note, make sure you call the method to fill up the combobow with ThreadSafeCall or you risk getting access violations.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •