[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

disabling buttons based on ini file contents

Posted on 2011-02-26
9
Medium Priority
?
231 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
[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
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 38

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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

650 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