Solved

Load ini section into ComboBox?

Posted on 1998-09-18
19
582 Views
Last Modified: 2010-04-06
Hello!

I am new to Delphi, and am trying to write a simple
program which takes an INIfile section, strips out
everything from the "=" right (for each entry) and then
load it all into a combo box. So far, I have been able
to load the section into the combo box:

procedure TForm1.Button1Click(Sender: TObject);
  var
  DelphiIni: TIniFile;
  begin
  DelphiIni := TIniFile.Create('c:\windows\desktop\test.ini');
  DelphiIni.ReadSectionValues('colors', ComboBox1.Items);
  ComboBox1.ItemIndex := 0;
  DelphiIni.Free;
end;

The test.ini looks like this:
[colors]
color1=blue
color2=red
color3=green

But...I want to strip out everything from the "=" to
the right so that all I see in the combo box is:

color1
color2
color3

I have been able to do it in visual basic, but am
having a devil of a time doing it in Delphi. any
help or samples would be greatly appreciated.

Thanks!

-S
0
Comment
Question by:summerset
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 12
  • 6
19 Comments
 
LVL 1

Expert Comment

by:nrobin
ID: 1340134
procedure TForm1.Button1Click(Sender: TObject);
      var
            DelphiIni: TIniFile;
            tempItems: TStringList;
            nIndex: Integer;

      begin
            tempItems := TStringList.Create;
            DelphiIni := TIniFile.Create('c:\windows\win.ini');
            DelphiIni.ReadSectionValues ('windows', tempItems);

            for nIndex := 0 to tempItems.Count - 1 do
                  Combobox1.Items.Add (Copy (tempItems [nIndex], 1, Pos ('=', tempItems [nIndex]) - 1));
            ComboBox1.ItemIndex := 0;
            DelphiIni.Free;
      end;

That should do it, there are better ways but its fast and easy.

Nicholas.
0
 
LVL 10

Accepted Solution

by:
viktornet earned 120 total points
ID: 1340135
procedure TForm1.Button1Click(Sender: TObject);
     var
     DelphiIni: TIniFile;
i, nPos : integer;
str : string;
     begin
     DelphiIni := TIniFile.Create('c:\windows\desktop\test.ini');
     DelphiIni.ReadSectionValues('colors', ComboBox1.Items);
    for i := 0 to ComboBox1.Items.Count - 1 do
begin
      nPos := Pos('=', ComboBox1.Strings[i]);
      str := ComboBox1.Items.Strings[i];
      ComboBox1.Items.Strings[i] := Copy(ComboBox1.Items.Strings[i],
                nPos + 1, Length(str) - nPos + 1;
end;
     ComboBox1.ItemIndex := 0;
     DelphiIni.Free;
   end;

Haven't tried this but is spupose to be working.....

Regards,
Viktor Ivanov
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1340136
Hello

The example nRobin gave you will copy the first part, not the color....

e.g Color1=Blue

His example will enter the Color1 string, and my example will enter Blue in the ComboBox...

Regards,
Viktor Ivanov
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:viktornet
ID: 1340137
OPppppps........sorry didn't read your questions to the end....You want to copy everything to the right....Sorry, but nRobin's example was correct...sorry for the misunderstanding....

Here it is again....
---------
procedure TForm1.Button1Click(Sender: TObject);
        var
        DelphiIni: TIniFile;
   i: integer;
        begin
        DelphiIni := TIniFile.Create('c:\windows\desktop\test.ini');
        DelphiIni.ReadSectionValues('colors', ComboBox1.Items);
       for i := 0 to ComboBox1.Items.Count - 1 do
   begin  
         ComboBox1.Items.Strings[i] := Copy(ComboBox1.Items.Strings[i], 1,
                   Pos('=', ComboBox1.Strings[i]) - 1);
   end;
        ComboBox1.ItemIndex := 0;
        DelphiIni.Free;
      end;

Regards,
Viktor Ivanov
0
 
LVL 4

Expert Comment

by:erajoj
ID: 1340138
Hi,
it's even easier:

  Uses ... IniFiles;

  Ini: TIniFile;
  ...
  Ini := TIniFile.Create( 'whatever.ini' );
  Ini.ReadSection( 'misc', ComboBox1.Items );
  Ini.Free;

Please don't intricate everything! ;-)

/// John
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1340139
Sorry, didn't have much time to work on it, and still haven't tried your way, but it seems to be the easiest ;-)

(@Viktor Ivanov)^.Data;
0
 
LVL 4

Expert Comment

by:erajoj
ID: 1340140
Shouldn't that be "(@Viktor_Ivanov)^.Data;" ?
Whitespaces aren't allowed in identifiers, you know? ;-)))

pt := Point( $202F2F2F, $6E686F4A );
SetString( sSig, PChar( @pt ), SizeOf( TPoint ) );
WriteLn( Output, sSig );

0
 
LVL 10

Expert Comment

by:viktornet
ID: 1340141
Who actually cares??? Just kidding... I seems to have forgotten it :-(

Regards,
(@Viktor_Ivanov)^.Name;
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1340142
Hello John,

Just tried the code you post up there and I'm amazed of how cool you think ;->

What would print my name in the caption of the form by using the following code???

var
  pt : TPoint;
begin
  pt := Point( $202F2F2F, $6E686F4A );//Just change the values of the x,y of point
 Caption := StrPas(PChar(@pt)));
end;

Regards,
Viktor Ivanov
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1340143
John Hello again!

I tried something but it displays my names reversed... Any idea why is that..Here is my coordinates for the point....

pt := Point( $202F2F2F, $56696B);//It is supose to be Vik and it prints kiV instead . o O

//Vik
0
 
LVL 4

Expert Comment

by:erajoj
ID: 1340144
Hi Viktor,
That code only works with my sig. Your sig is too long (23 chars).
Besides, your "codeoptimizing" will not work, unless the stack incidentally provides a null terminator. NEVER trust that!
If I find out a really esoteric way of writing your sig, I'll mail you.

/// John
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1340145
Oh, oK.....Here is my name.....try it.....At least the first two letters.. ;->

pt := Point( $202F2F2F, $6B6956);
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1340146
pt := Point( $206B6956, $726F74);

This one displays Vik tor
do you know how can I remove the space in between???

Regards,
Viktor Ivanov
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1340147
This one could be my signature... What do you think???

pt := Point( $6B695620, $726F74);

 Viktor
0
 
LVL 4

Expert Comment

by:erajoj
ID: 1340148
I see you've got it, but you mean first three, not two, right?
I personally like the little-endian architecture of the Intel processors and find big-endian of, for instance, Motorola processors a bit messy.

/// John
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1340149
Yeah, sorry for misspeling {:-o)

Viktor

Can you think of an intersting signatue so it can fit in the coordinates of the point???

0
 
LVL 4

Expert Comment

by:erajoj
ID: 1340150
Yes, that's correct.
"/Viktor" => pt := Point( $6B695627, $726F74);
"Viktor"  => pt := Point( $746B6956, $726F);

/// John
0
 
LVL 4

Expert Comment

by:erajoj
ID: 1340151
Yes, but it'll take some work, and I really have to work now.
L8R

/// John
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1340152
Ok, Tell me when you finish work!

Tok 2 U L8R ;->

Viktor
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question