Solved

disabling buttons based on ini file contents

Posted on 2011-02-26
9
225 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
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!

 

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:Ephraim Wangoya
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:Ephraim Wangoya
ID: 34989027

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

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
select query - oracle 16 101
Tidtcpserver listening on multiports? 1 46
Twebbrowser add css to the header 3 39
How to add columns to a RAD Studio FMX grid at design time 1 34
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
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…

730 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