Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

*SOLVED* Form caption read from INI

Hello all and thank you for your help.

I am very new to delphi (in fact I am very new to programming)

I have been playing around with delphi for about 3 weeks now.
I had a brainfart and attempted to creat a control panel to play around with functions.
I have searched many pages, read many help files and tutorials and for the life of me can not find an answer to my current situation.
I hope someone here can find a solution.


I have 1 form, with many group boxes stacked on top of each other with a group of buttons to show each group box when clicked.
I use an external ini file to read and save settings (such as username)

at:
[code]procedure Tcontrol_panel.FormCreate(Sender: TObject);
begin
if fileexists('./my.ini') then
begin
inifile:=Tinifile.Create('./my.ini');
lblusername.Caption:=inifile.ReadString('UserInfo','AccountName','')+ lblusername.caption ;
ediaccname.Text:=inifile.ReadString('UserInfo','Accountname','');
IniFile.Free;
end;
end;[/code]

now this works very well, but in another group box I can change the username and click save button will save it to the ini file.

[code]procedure Tcontrol_panel.But_save_trdClick(Sender: TObject);
begin
inifile:=Tinifile.Create('./my.ini');
inifile.WriteString('UserInfo','AccountName',ediaccname.Text);
inifile.WriteString('Trade','MaxTime', edimaxtime.text);
inifile.WriteString('Trade','MaxCardsPerTrade',edimaxtrd.text);
if ChkPmss.Checked=True then
begin
inifile.WriteString('ScreenShot','PMs','YES');
end
else if ChkPmss.Checked=False then
inifile.WriteString('ScreenShot','PMs','NO');
if ChkTrdss.Checked=True then
begin
inifile.WriteString('ScreenShot','Trade','YES');
end
else if ChkTrdss.Checked=False then
inifile.WriteString('ScreenShot','Trade','NO');
IF optblkbuy.Checked=True then
begin
inifile.WriteString('Trade','Mode','BulkBuying');
end
else if optprcsell.Checked=True then
begin
inifile.WriteString('Trade','Mode','PriceSheetSelling');
end;
IniFile.Free;
end;
[/code]this too works (as I can save and look at the ini file it has saved.

now for my problem...

When I select a button to show one of the group boxes I change the start button from "go" to "start_utility" clicking this button returns me to the main group box which is where the whole thing starts.
Now if I select the button "start_utlity" it re reads the ini file and rather than replace the string that was there with the new string it ADDS it...ie:
I start with the name test, change the name to hello, select save, the ini file shows hello but the main group now lists testhello rather than just hello.

How can I make the program change the name to the new string?

Thank you

Comments

  • zibadianzibadian Posts: 6,349Member
    : Hello all and thank you for your help.
    :
    : I am very new to delphi (in fact I am very new to programming)
    :
    : I have been playing around with delphi for about 3 weeks now.
    : I had a brainfart and attempted to creat a control panel to play
    : around with functions.
    : I have searched many pages, read many help files and tutorials and
    : for the life of me can not find an answer to my current situation.
    : I hope someone here can find a solution.
    :
    :
    : I have 1 form, with many group boxes stacked on top of each other
    : with a group of buttons to show each group box when clicked.
    : I use an external ini file to read and save settings (such as
    : username)
    :
    : at:
    : [code]: procedure Tcontrol_panel.FormCreate(Sender: TObject);
    : begin
    : if fileexists('./my.ini') then
    : begin
    : inifile:=Tinifile.Create('./my.ini');
    : lblusername.Caption:=inifile.ReadString('UserInfo','AccountName','')+ lblusername.caption ;
    : ediaccname.Text:=inifile.ReadString('UserInfo','Accountname','');
    : IniFile.Free;
    : end;
    : end;[/code]:
    :
    : now this works very well, but in another group box I can change the
    : username and click save button will save it to the ini file.
    :
    : [code]: procedure Tcontrol_panel.But_save_trdClick(Sender: TObject);
    : begin
    : inifile:=Tinifile.Create('./my.ini');
    : inifile.WriteString('UserInfo','AccountName',ediaccname.Text);
    : inifile.WriteString('Trade','MaxTime', edimaxtime.text);
    : inifile.WriteString('Trade','MaxCardsPerTrade',edimaxtrd.text);
    : if ChkPmss.Checked=True then
    : begin
    : inifile.WriteString('ScreenShot','PMs','YES');
    : end
    : else if ChkPmss.Checked=False then
    : inifile.WriteString('ScreenShot','PMs','NO');
    : if ChkTrdss.Checked=True then
    : begin
    : inifile.WriteString('ScreenShot','Trade','YES');
    : end
    : else if ChkTrdss.Checked=False then
    : inifile.WriteString('ScreenShot','Trade','NO');
    : IF optblkbuy.Checked=True then
    : begin
    : inifile.WriteString('Trade','Mode','BulkBuying');
    : end
    : else if optprcsell.Checked=True then
    : begin
    : inifile.WriteString('Trade','Mode','PriceSheetSelling');
    : end;
    : IniFile.Free;
    : end;
    : [/code]: this too works (as I can save and look at the ini file it has saved.
    :
    : now for my problem...
    :
    : When I select a button to show one of the group boxes I change the
    : start button from "go" to "start_utility" clicking this button
    : returns me to the main group box which is where the whole thing
    : starts.
    : Now if I select the button "start_utlity" it re reads the ini file
    : and rather than replace the string that was there with the new
    : string it ADDS it...ie:
    : I start with the name test, change the name to hello, select save,
    : the ini file shows hello but the main group now lists testhello
    : rather than just hello.
    :
    : How can I make the program change the name to the new string?
    :
    : Thank you
    :
    You error is in this line:
    [code]
    lblusername.Caption:=inifile.ReadString('UserInfo'
    ,'AccountName','')+ lblusername.caption ;
    [/code]
    The +-operator in this case concatenates 2 strings. Thus the string returned from ReadString() gets added to the Caption property.
  • caspert_ghostcaspert_ghost Posts: 53Member
    : You error is in this line:
    : [code]:
    : lblusername.Caption:=inifile.ReadString('UserInfo'
    : ,'AccountName','')+ lblusername.caption ;
    : [/code]:
    : The +-operator in this case concatenates 2 strings. Thus the string
    : returned from ReadString() gets added to the Caption property.

    the form group box has static text "Please enter" then it calls the ini file and adds the username from the ini file + edit text "'s password"

    I need it to replace the old username with the new user name and still use the + edit text ("'s password")

    when the program starts it works, but when I make a change and click the (return to start group) button, it ADDS username (it dont add another +edittext)

    ie: I start the program with user name test the page will read:
    "Please enter test's password"
    Now if I go to the other group page and change the user name to casper and save it to the ini file and return to the start group it reads:
    "Please enter caspertest's password"
    as you can see its adding the username not the +code so not sire how thats the error and if I remove that line then it reads:
    "Please enter casper"
    thank you for your response and I am sure that will help others but in my situation I need the + code.

    should I make the +code a seperate static line?? (was hoping to avoid this as it creates a space between the last letter of username and 's.

  • zibadianzibadian Posts: 6,349Member
    : : You error is in this line:
    : : [code]: :
    : : lblusername.Caption:=inifile.ReadString('UserInfo'
    : : ,'AccountName','')+ lblusername.caption ;
    : : [/code]: :
    : : The +-operator in this case concatenates 2 strings. Thus the string
    : : returned from ReadString() gets added to the Caption property.
    :
    : the form group box has static text "Please enter" then it calls the
    : ini file and adds the username from the ini file + edit text "'s
    : password"
    :
    : I need it to replace the old username with the new user name and
    : still use the + edit text ("'s password")
    :
    : when the program starts it works, but when I make a change and click
    : the (return to start group) button, it ADDS username (it dont add
    : another +edittext)
    :
    : ie: I start the program with user name test the page will read:
    : "Please enter test's password"
    : Now if I go to the other group page and change the user name to
    : casper and save it to the ini file and return to the start group it
    : reads:
    : "Please enter caspertest's password"
    : as you can see its adding the username not the +code so not sire how
    : thats the error and if I remove that line then it reads:
    : "Please enter casper"
    : thank you for your response and I am sure that will help others but
    : in my situation I need the + code.
    :
    : should I make the +code a seperate static line?? (was hoping to
    : avoid this as it creates a space between the last letter of username
    : and 's.
    :
    :
    If the lblUserName.Caption property contains a non-empty string, then that string is appended to the UserInfo.AccountName value using your code. If you want to replace that value with the one in the ini-file, then you should use this:
    [code]
    lblusername.Caption:=inifile.ReadString('UserInfo', 'AccountName',
    lblusername.caption);
    [/code]
    That's what the default parameter of the ReadString() is for.
  • caspert_ghostcaspert_ghost Posts: 53Member
    : :
    : If the lblUserName.Caption property contains a non-empty string,
    : then that string is appended to the UserInfo.AccountName value using
    : your code. If you want to replace that value with the one in the
    : ini-file, then you should use this:
    : [code]:
    : lblusername.Caption:=inifile.ReadString('UserInfo', 'AccountName',
    : lblusername.caption);
    : [/code]:
    : That's what the default parameter of the ReadString() is for.

    I tried the above code out of morbid couriosity and as I suspected:
    the 'default' part of the line code is there IF there is NO information in the search string. if there is information then the 'default' is ignored.

    with that code it removes "'s password" which is the caption code.
  • zibadianzibadian Posts: 6,349Member
    : : :
    : : If the lblUserName.Caption property contains a non-empty string,
    : : then that string is appended to the UserInfo.AccountName value using
    : : your code. If you want to replace that value with the one in the
    : : ini-file, then you should use this:
    : : [code]: :
    : : lblusername.Caption:=inifile.ReadString('UserInfo', 'AccountName',
    : : lblusername.caption);
    : : [/code]: :
    : : That's what the default parameter of the ReadString() is for.
    :
    : I tried the above code out of morbid couriosity and as I suspected:
    : the 'default' part of the line code is there IF there is NO
    : information in the search string. if there is information then the
    : 'default' is ignored.
    :
    : with that code it removes "'s password" which is the caption code.
    :
    How about this:
    [code]
    lblusername.Caption:=inifile.ReadString('UserInfo', 'AccountName',
    lblusername.caption)+'''s password';
    [/code]
  • caspert_ghostcaspert_ghost Posts: 53Member
    : How about this:
    : [code]:
    : lblusername.Caption:=inifile.ReadString('UserInfo', 'AccountName',
    : lblusername.caption)+'''s password';
    : [/code]:


    Thank you thank you thank you... I actually thought of that but when I typed in the code it gave me the dbl brackets for ''' thought it would cause an error so didnt try it.. gues it didnt matter.

    Works great thank you very much.
  • caspert_ghostcaspert_ghost Posts: 53Member
    posted in wrong area
Sign In or Register to comment.