Link to home
Start Free TrialLog in
Avatar of zachvaldez
zachvaldezFlag for United States of America

asked on

Adding the selected item from a combo box to a textfile.

I'd like to know how to add a seledted item in a combo box to a textfile.For example to an .ini file. and a txtfile like notepad.
Avatar of DarkoLord
DarkoLord
Flag of Slovenia image

Hi... use this:

Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long

Public Sub WriteToINI(ByVal strSection As String, ByVal strkey As String, ByVal strkeyvalue As String, ByVal strfullpath As String)
   'sub to write a key and its value inside an ini section.
   Call WritePrivateProfileString(strSection$, UCase$(strkey$), strkeyvalue$, strfullpath$)
End Sub

Private Sub Command1_Click()
   Dim strTxt As String, ff As String
   ff = FreeFile
   strTxt = Combo1.List(Combo1.ListIndex)

   'write to normal text file
   Open "c:\textfile.txt" For Output As #ff
       Print #ff, strTxt  
   Close #ff  

   'write to ini file
   WriteToINI "section", "key", strTxt, "c:\inifile.ini"
End Sub


Darko
Avatar of zachvaldez

ASKER

Thanks,

How about loading it to the combobox from the textfile?
Ho,
oops! in addition also to load it from the textfile using the .ini aproach!

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of DarkoLord
DarkoLord
Flag of Slovenia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for sharing your knowledge.

I tried the eg. and it is inputting only 1 item from the combo box and that is good as in the

code: WriteToINI "section", "key", strTxt, "c:\inifile.ini"

The value here is the strTxt that I passed so that now the ini file looked like:
"C:\inifile.ini" - Path
[BooksRead] - Section
BooKid - Key
TDG23G1 -Value

Is it possible to append  Value(strStr) in succession and keep on adding to it as I select it from the combo box and it is save so that I will
have the following:

BookId -TDG23G1
BookId - KKDFHLF4
BookId - L089HHI
 and so forth....

Right now it saves one value and when I select another item form the combo box, it just replaced it and not
Add to the list in the inifile. I guess to make the question clear - How can I add/append value(bookids)
without replacing the bookids already in the ini file....? it should save the bookids there and append bookids depending on the combobox selection.
Avatar of GrahamSkan
That's the way .ini files work. They are for storing key values categorised by sections. If you want to store more key values you should give them different names. You could name them sequentially. It's also best to keep a count, so you know how many to retrieve.

[BooksRead]
Count=3
BookId_1=TDG23G1
BookId_2=KKDFHLF4
BookId_3=L089HHI
Well you can retrieve all subkeys at once... here is the function that adds them to a listbox:

Private Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long

Public Sub LoadIniSectionKeysLB(ByVal strSection As String, ByVal lstB As ListBox, ByVal strfullpath As String)
   'sub to load all keys from an ini section into a listbox.
   Dim KeyAndVal() As String, Key_Val() As String, strBuffer As String
   Dim intx As Integer
   Let strBuffer$ = String$(750, Chr$(0&))
     Call GetPrivateProfileSection(strSection, strBuffer, Len(strBuffer), strfullpath)
       KeyAndVal = Split(strBuffer, vbNullChar)
         For intx = LBound(KeyAndVal) To UBound(KeyAndVal)
           If KeyAndVal(intx) = vbNullString Then Exit For
             Key_Val = Split(KeyAndVal(intx), "=")
               If UBound(Key_Val) = -1 Then Exit For
             lstB.AddItem Key_Val(0) '<--to get the keys prior to "=" delimiter only
          'lstB.additem inikey(1) '<--to get the key values past the "=" delimiter only
         Next
       'If lstB.ListCount > 0 Then lst.Selected(0) = True '<<--if you want first list item in listbox selected
     Erase KeyAndVal: Erase Key_Val
End Sub

Darko
The idea of differentiating the 'KEY" for new bookids written to the inifile is enterprising.
How is that done in code? There must be somekind of integer variable named to count each combobox selected item.
To count selected item? I don't understand what you mean by that....

Darko
As I select an item in th combobox
it would add a counter in the "KEY' say  BookID1 = TGSDLL,Bookid 2 = ILOOOS
in the ini file under "KEY"
and then the abiltity to save the last entry...and keep on adding th bookids. It should check the last entry and integer in the KEY and add 1 to it.

In ini file there is no "last" entry so you would have to create a key to store the last ID value that was written...

Darko
Thanks, that was exactly what I am trying to get at.How to create a uniquie key for every item selected in teh combo box. Thanks for your help.
GrahamSkin,
 Hi,
Can you please show me an example in code how  to keep track on the number of items
as in the 'KEY" Count=3 you mentioned aboved?

Thanks!
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks DarkoLord for getting back on this quickly. I did know that you can manipulate the reserved word "key".
The reason why I am asking that is I would like to know the frequency each item was selected and based on that I can count it. But this is superved!
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Graham,what if I load the items programmatically from the ini file..
This loads all keys from a section to combobox:


Private Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long

Private Sub LoadIniSectionKeys(ByVal strSection As String, ByVal cmB As ComboBox, ByVal strfullpath As String)
   Dim KeyAndVal() As String, Key_Val() As String, strBuffer As String
   Dim intx As Integer
   Let strBuffer$ = String$(750, Chr$(0&))
     Call GetPrivateProfileSection(strSection, strBuffer, Len(strBuffer), strfullpath)
       KeyAndVal = Split(strBuffer, vbNullChar)
         For intx = LBound(KeyAndVal) To UBound(KeyAndVal)
           If KeyAndVal(intx) = vbNullString Then Exit For
             Key_Val = Split(KeyAndVal(intx), "=")
               If UBound(Key_Val) = -1 Then Exit For
             'cmB.AddItem Key_Val(0) '<--to get the keys prior to "=" delimiter only
          cmB.additem inikey(1) '<--to get the key values past the "=" delimiter only
         Next
     Erase KeyAndVal: Erase Key_Val
End Sub

Private Sub LoadFromINI()
   LoadIniSectionKeys "Books Read", Combo1, "C:\file.txt"
End Sub

Darko
Thanks Darko- for stopping by.
I created a new section ,keycalled
[proposal_count]
Count=5

How will I load these default values?

z
Is this what you want?

Private Sub LoadKeys()
   Dim i As Long
   For i = 1 to Val(ReadFromINI("proposal_count", "Count", "c:\file.ini"))
        Combo1.AddItem ReadFromINI("Books Read", "BookID_" & i, "c:\file.ini")
   Next i
End Sub

Darko
Not exactly, but that would work in the other one..
I just want to load and read these 2 lines.

[proposal_count]
Count=5

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is where Iam right now
[MRUProposals]
ActNum0=TDG123
ActNum1=TDG22S
ActNum5=VPL4OS
[Proposal_Count]
Count=3


I like to see these values when I open the ini file..
what are those "0", "1" and "5"? Indexes of combobox items?
listindex property

also count is going back to 1 when I close and open the app
Darko,

Im getting error here this line:

  cmB.additem inikey(1) '<--to get the key values past the "=" delimiter only

But disregard my last comments.. about thie "also count is going back to 1 when I close and open the app"-My error this part
I could'nt ask for more. Sorry for me asking too many questions. The answer is somwhere there I just have to figure out from all the excellent examples you mentioned.
Sorry that was my error...

cmB.additem Key_Val(1)

should be the correct line...
This is what I am trying to load now-TDG123,TDG22S...
[MRUProposals]
ActNum0=TDG123
ActNum1=TDG22S
ActNum2=TDG24S

I created a checkbox that if it is true load these values....from ini file(I have problem here) using the LoadIniSectionKey function

otherwise use the default load from the Database.. whickh loads all bookIDs (I have no problem with this part)--

Private Sub LoadIniSectionKeys(ByVal strSection As String, ByVal cmB As ComboBox, ByVal strfullpath As String)
   Dim KeyAndVal() As String, Key_Val() As String, strBuffer As String
   Dim intx As Integer
   Let strBuffer$ = String$(750, Chr$(0&))
     Call GetPrivateProfileSection(strSection, strBuffer, Len(strBuffer), strfullpath)
       KeyAndVal = Split(strBuffer, vbNullChar)
         For intx = LBound(KeyAndVal) To UBound(KeyAndVal)
           If KeyAndVal(intx) = vbNullString Then Exit For
             Key_Val = Split(KeyAndVal(intx), "=")
               If UBound(Key_Val) = -1 Then Exit For
          cmB.additem Key_Val(1)
         Next
     Erase KeyAndVal: Erase Key_Val
End Sub

Private Sub LoadFromINI()
   LoadIniSectionKeys "MRUProposals", Combo1, "C:\file.ini"
End Sub
It is not loading the combo...

 For intx = LBound(KeyAndVal) To UBound(KeyAndVal)
           If KeyAndVal(intx) = vbNullString Then Exit For      ---------- when it hits this line
             Key_Val = Split(KeyAndVal(intx), "=")
               If UBound(Key_Val) = -1 Then Exit For
          cmB.additem Key_Val(1)
         Next
     Erase KeyAndVal: Erase Key_Val
End Sub
Do you have this in your ini file?

[MRUProposals]
ActNum0=TDG123
ActNum1=TDG22S
ActNum2=TDG24S
YES
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It is now working! Thanks for the time you spent helping me understand inifiles.. I surely understand now how inifiles work.
For that an EXTRA BONUS POINTS!!
You're welcome! Thanks for the points!

Darko