Solved

disabling buttons based on ini file contents

Posted on 2011-02-26
9
223 Views
Last Modified: 2012-05-11
Hi Guys

i have an application with several buttons and i want to somehow use a .ini file to control which buttons are enabled on a specific pc.

so when i drop the app on a computer i would of put the ini file in the windows directory with contents like below

button1=yes
button2=yes
button3=no
button4=yes

is this possible
0
Comment
Question by:TG-Steve
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 34988459
yes, off course

you'll need to periodically monitor the directory for that file
like with a timer

const IniFileName = 'c:\settings\program.ini';

procedure TForm1.Timer1Timer(Sender: TObject);
var Ini: TIniFile;
begin
  if FileExists(IniFileName) then
  begin
    Ini := TIniFile.Create(IniFileName);
    try
      // read your settings here
    finally
      Ini.Free;
    end;
  end;
end;
0
 
LVL 24

Accepted Solution

by:
jimyX earned 500 total points
ID: 34988484
You can use two ways either as String (Yes/No) or as Integer/Boolean (0/1):

As String:
Button1=YES
Button2=NO
Button3=NO
Button4=YES
procedure TForm1.FormCreate(Sender: TObject);
var
  iniF : TIniFile;
begin
  try
    iniF := TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini'));
    if UpperCase(iniF.ReadString('Button', Button1.Name, 'YES')) = 'YES' then
      Button1.Enabled := True
    else
      Button1.Enabled := False;
    if UpperCase(iniF.ReadString('Button', Button2.Name, 'YES')) = 'YES' then
      Button2.Enabled := True
    else
      Button2.Enabled := False;
    if UpperCase(iniF.ReadString('Button', Button3.Name, 'YES')) = 'YES' then
      Button3.Enabled := True
    else
      Button3.Enabled := False;
    if UpperCase(iniF.ReadString('Button', Button4.Name, 'YES')) = 'YES' then
      Button4.Enabled := True
    else
      Button4.Enabled := False;
  finally
    iniF.Free;
  end;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var
  iniF : TIniFile;
begin
  try
    iniF := TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini')) ;
    if Button1.Enabled then
      iniF.WriteString('Button',Button1.Name, 'YES')
    else
      iniF.WriteString('Button',Button1.Name, 'NO');
    if Button2.Enabled then
      iniF.WriteString('Button',Button2.Name, 'YES')
    else
      iniF.WriteString('Button',Button2.Name, 'NO');
    if Button3.Enabled then
      iniF.WriteString('Button',Button3.Name, 'YES')
    else
      iniF.WriteString('Button',Button3.Name, 'NO');
    if Button4.Enabled then
      iniF.WriteString('Button',Button4.Name, 'YES')
    else
      iniF.WriteString('Button',Button4.Name, 'NO');
  finally
    iniF.Free;
  end;
end;

Open in new window


As Bool:
Button1=1
Button2=0
Button3=0
Button4=1
procedure TForm1.FormCreate(Sender: TObject);
var
  iniF : TIniFile;
begin
  try
    iniF := TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini'));
    Button1.Enabled := iniF.ReadBool('Button', Button1.Name, True);
    Button2.Enabled := iniF.ReadBool('Button', Button2.Name, True);
    Button3.Enabled := iniF.ReadBool('Button', Button3.Name, True);
    Button4.Enabled := iniF.ReadBool('Button', Button4.Name, True);
  finally
    iniF.Free;
  end;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var
  iniF : TIniFile;
begin
  try
    iniF := TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini')) ;
    iniF.WriteBool('Button',Button1.Name, Button1.Enabled);
    iniF.WriteBool('Button',Button2.Name, Button2.Enabled);
    iniF.WriteBool('Button',Button3.Name, Button3.Enabled);
    iniF.WriteBool('Button',Button4.Name, Button4.Enabled);
  finally
    iniF.Free;
  end;
end;

Open in new window

0
 

Author Comment

by:TG-Steve
ID: 34988547
if you dont mind me asking. why would i need the timer? once i put the ini file on the specific pc it should never change so i presume i would only need to open it once during the program startup. i am interested in this way due to locking down in certain offices so that they dont have access to certain parts of the app in the future

it is an error on my part not explaining this and giving the idea of an app that looks for changes in the future is probably what you concluded from looking at my first post sorry
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:TG-Steve
ID: 34988552
just trying this jimyx thankyou i will get back
0
 
LVL 24

Expert Comment

by:jimyX
ID: 34988596
please do not forget to add "inifiles" to the uses clause.
0
 
LVL 32

Expert Comment

by:ewangoya
ID: 34988912

Don't you think it would be better to store this information in the database in a UserSettigns table. This way even if a person wants to access the application in a different office, the buttons are enabled and disabled properly.

You could even do this in a GroupSettings table where each person belongs to a certain group which represents the target office
0
 

Author Closing Comment

by:TG-Steve
ID: 34988988
thanks guys

jimyx your solution worked perfect thank you
0
 

Author Comment

by:TG-Steve
ID: 34988998
ewangoya sorry i didnt catch your responce (due to me not refreshing the screen whilst i was trying the previous fix) so please dont think i point blank ignored you sorry

fortunatly in the building where i work people are assigned a personal station to work from so do not use other desks etc so in this case the solution was great although i would of questioned more in the direction you suggested if people hot swapped desks etc

this is off topic but why when i accept the answers on any threat and give the max marks it only shows a score of 9.1 ?? is this a bug in the EE website?
0
 
LVL 32

Expert Comment

by:ewangoya
ID: 34989027

No problem, so long as you get a good satisfactory solution, we are always here to help
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

776 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