Solved

disabling buttons based on ini file contents

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

Author Comment

by:TG-Steve
ID: 34988552
just trying this jimyx thankyou i will get back
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

910 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

20 Experts available now in Live!

Get 1:1 Help Now