|
-
Jan 16th, 2004, 06:36 PM
#1
Thread Starter
Hyperactive Member
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.
-
Jan 16th, 2004, 06:54 PM
#2
Hyperactive Member
VB Code:
Dim classString as String
Dim TempStr as String
Dim OutString as String
'create a generic string
classString = "class ^^SELECTED^^" & vbcrlf & "{" & vbclrf & "Count = ^^NUMBER^^;" & vbcrlf &"};"
For i = 1 To List1.ListCount
TempStr=classString
TempStr=Replace(TempStr,"^^SELECTED^^",List1.List(List1.ListIndex))
TempStr=Replace(TempStr,"^^NUMBER^^",Text1.text)
Print #1,TempStr
next i
Something like that???
Last edited by packetVB; Jan 16th, 2004 at 06:58 PM.
-
Jan 16th, 2004, 06:57 PM
#3
Hyperactive Member
Loop through each of the listbox items using:
VB Code:
Dim l As Long
For l = 0 To List1.ListCount - 1
'Don't forget to open the file for output etc..
Print #FileNum, List1.List(l)
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
-
Jan 17th, 2004, 07:33 PM
#4
Thread Starter
Hyperactive Member
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
-
Jan 17th, 2004, 08:18 PM
#5
Frenzied Member
Are you appending a certain .txt file or creating a new one everytime?
-
Jan 17th, 2004, 08:34 PM
#6
Hyperactive Member
np.
OPh yea
You probably should edit you post and put [Resolved] at end.
packetvb
-
Jan 17th, 2004, 09:10 PM
#7
Thread Starter
Hyperactive Member
im creating a new one
My post isn't solved, read my last post.
-
Jan 17th, 2004, 09:33 PM
#8
Frenzied Member
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.
-
Jan 17th, 2004, 10:26 PM
#9
Thread Starter
Hyperactive Member
they dont have to be selected.
i dont really understand what your saying.
-
Jan 17th, 2004, 11:29 PM
#10
Frenzied Member
There might be an easier way, but this is what I did and it seems to work.
VB Code:
Dim fso, txtfile
Dim x As Integer
Dim Class As String
Dim remaining As Integer
Dim yournumber As String
Private Sub cmdCreate_Click()
Dim MyArray() As String
x = List1.ListCount
x = x - 1
Set fso = CreateObject("Scripting.FileSystemObject")
Set txtfile = fso.CreateTextFile("c:\temp\myfile.txt", True)
For remaining = 0 To x
Class = List1.List(remaining)
MsgBox Class 'Just for you to see it before we split it
MyArray() = Split(Class, "(") 'Split it at the (
Class = MyArray(0)
yournumber = MyArray(1)
yournumber = Replace(yournumber, ")", "") 'Get rid of trailing )
MsgBox Class 'Just for you to see before you write
MsgBox yournumber 'Just for you to see before you write
txtfile.WriteBlankLines (1)
txtfile.WriteLine ("Class " & Class)
txtfile.WriteLine ("{")
txtfile.WriteLine ("Count = " & yournumber & ";")
txtfile.WriteLine ("};")
Next
txtfile.Close
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;
};
-
Jan 18th, 2004, 12:33 AM
#11
Thread Starter
Hyperactive Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|