Results 1 to 11 of 11

Thread: Add text to text file depending on what in list box [solved]

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Utah
    Posts
    397

    Add text to text file depending on what in list box [solved]

    Im tring to design a very easy to use interface for one of my programs, but ive ran into one slight problem. ill try to explain:

    I have 1 drop down list and a number only text box next to each other, when the user selects a item in the drop down list and types in a number in the text box and presses next, it creates the string "selected_item (number_typed)" but without quotes in a list box. Now when the user pressed create, i want it to create the following code in a text file (one of these below for each item in the listbox):

    class selected_item_from_dropdown_list
    {
    Count = number_in_textbox;
    };

    I know how to write to text files, i just need to know how to write to a text file depending what is in a listbox.

    please tell me if i need to explain any further.
    Last edited by Narfy; Jan 18th, 2004 at 12:34 AM.

  2. #2
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Okinawa, Japan
    Posts
    271
    VB Code:
    1. Dim classString as String
    2. Dim TempStr as String
    3. Dim OutString as String
    4. 'create a generic string
    5. classString = "class ^^SELECTED^^" & vbcrlf & "{" & vbclrf & "Count = ^^NUMBER^^;" & vbcrlf &"};"
    6.  
    7. For i = 1 To List1.ListCount    
    8.     TempStr=classString
    9.     TempStr=Replace(TempStr,"^^SELECTED^^",List1.List(List1.ListIndex))
    10.     TempStr=Replace(TempStr,"^^NUMBER^^",Text1.text)
    11.      Print #1,TempStr
    12. next i

    Something like that???
    Last edited by packetVB; Jan 16th, 2004 at 06:58 PM.

  3. #3
    Hyperactive Member
    Join Date
    Apr 2002
    Location
    UK
    Posts
    506
    Loop through each of the listbox items using:
    VB Code:
    1. Dim l As Long
    2.  
    3.   For l = 0 To List1.ListCount - 1
    4.     'Don't forget to open the file for output etc..
    5.     Print #FileNum, List1.List(l)
    6.   Next l
    If you're happy with the above then so be it, otherwise clear up a few points for me and I'll try and inform you further.
    1. When you say "Count = number_in_textbox;" do you mean the number that's in the textbox when the user presses create or the number in the textbox when the user presses next?
    2. Will "selected_item" contain spaces?

    -adehh

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Utah
    Posts
    397
    thanks packetvb, thats really close to what i want.
    but, i should have explained a little better.
    when the user pressed Next, it puts info into a list box, say I selected blubber from the drop down list and typed in 10 in the text box, then i press next it creates blubber (10) in the list box, and when i press create i want it to take it and create the code below in a text file using the info thats in the listbox.

    class blubber
    {
    Count = 10;
    };


    thanks

  5. #5
    Frenzied Member
    Join Date
    May 2003
    Location
    So Cal
    Posts
    1,564
    Are you appending a certain .txt file or creating a new one everytime?

  6. #6
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Okinawa, Japan
    Posts
    271
    np.
    OPh yea
    You probably should edit you post and put [Resolved] at end.

    packetvb

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Utah
    Posts
    397
    im creating a new one

    My post isn't solved, read my last post.

  8. #8
    Frenzied Member
    Join Date
    May 2003
    Location
    So Cal
    Posts
    1,564
    Do the have to be selected in the List Box or do you want them all saved to a txt file regardless if they are selected in the List box or not?

    If they need to be selected, then write the list1.text to the file after you parse it to your format. If you want to write them all, regardless if they are selected or not, use the list1.listcount to get the number of entries then do a for loop and write the list1.list(x) value, x being the number -1 (-1 because the (x) value starts at 0 and the listcount will start at 1.

    Parse the string, and then write the file as needed.

    If this doesnt make sense, let me know I'll code it out for you.
    Last edited by BrianS; Jan 17th, 2004 at 09:59 PM.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Utah
    Posts
    397
    they dont have to be selected.

    i dont really understand what your saying.

  10. #10
    Frenzied Member
    Join Date
    May 2003
    Location
    So Cal
    Posts
    1,564
    There might be an easier way, but this is what I did and it seems to work.

    VB Code:
    1. Dim fso, txtfile
    2. Dim x As Integer
    3. Dim Class As String
    4. Dim remaining As Integer
    5. Dim yournumber As String
    6.  
    7.  
    8.  
    9. Private Sub cmdCreate_Click()
    10. Dim MyArray() As String
    11.  
    12.  
    13. x = List1.ListCount
    14. x = x - 1
    15. Set fso = CreateObject("Scripting.FileSystemObject")
    16. Set txtfile = fso.CreateTextFile("c:\temp\myfile.txt", True)
    17.  
    18. For remaining = 0 To x
    19.     Class = List1.List(remaining)
    20.    
    21.     MsgBox Class 'Just for you to see it before we split it
    22.     MyArray() = Split(Class, "(") 'Split it at the (
    23.     Class = MyArray(0)
    24.     yournumber = MyArray(1)
    25.     yournumber = Replace(yournumber, ")", "") 'Get rid of trailing )
    26.  
    27.     MsgBox Class 'Just for you to see before you write
    28.     MsgBox yournumber 'Just for you to see before you write
    29.    
    30.     txtfile.WriteBlankLines (1)
    31.     txtfile.WriteLine ("Class " & Class)
    32.     txtfile.WriteLine ("{")
    33.     txtfile.WriteLine ("Count = " & yournumber & ";")
    34.     txtfile.WriteLine ("};")
    35.    
    36. Next
    37. txtfile.Close
    38. End Sub

    I use FSO for writing, you may have to write it different for your style/code. Here is the result of my Text file with 2 entries in the List Box with Values of Blubber (10) and Sandy (11).


    Class Blubber
    {
    Count = 10;
    };

    Class Sandy
    {
    Count = 11;
    };

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Utah
    Posts
    397
    thanks, worked perfect

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width