Solved

disabling buttons based on ini file contents

Posted on 2011-02-26
9
220 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 36

Expert Comment

by:Geert Gruwez
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:TG-Steve
Comment Utility
just trying this jimyx thankyou i will get back
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 24

Expert Comment

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

Expert Comment

by:ewangoya
Comment Utility

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
Comment Utility
thanks guys

jimyx your solution worked perfect thank you
0
 

Author Comment

by:TG-Steve
Comment Utility
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
Comment Utility

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

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video discusses moving either the default database or any database to a new volume.

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now