-
Jan 8th, 2012, 06:21 PM
#1
Thread Starter
New Member
Visual Basic 2005 - Sorting Files by Names
Hello, I'm new to these forums and I'm looking for some help.
For my programming class we must make a word guessing game with scoring for a project. We aren't required to log top scores or anything like that, but I would like to learn how to for future references. I've looked around and all I've found was ways to log in Excel and SQL servers, which I don't have the ability to do at school.
I had the idea to create a text file with the user's score as the title each time a game is won. Then I'd like to write to the file (which I know how to do) with the word that was used in the game and the user's name.
What I don't know how to do is find the file name with the highest integer value. I've looked around and couldn't find any way how to do this.
Another way I thought of doing this was to just use one text file, but insert a line of text per each game played. Then I'd have to find where to insert each line to make sure they're in order, so I could just display the text file in a textbox, which would be easy enough.
Example:
#1 310 - Josh with the word "Cat"
#2 265 - Josh with the word "Dog"
I know a lot of people here don't like to hand out code to people, and have them learn for themselves. I support this and would like to learn for myself, but I don't know what commands to use to get a list of file names or insert lines and certain places in a text file.
Thanks in advance.
-
Jan 8th, 2012, 08:17 PM
#2
Re: Visual Basic 5 - Sorting Files by Names
Tip: Display the high scores in a listbox with the Sorted property set to True. Now, listboxes don't sort numbers as numbers, they sort them as text. If your format your scores with leading zeroes, then it resolves the sorting issue. Example:
Code:
' in a loop that reads your scores
... loop start
... read a score & format like so: sText is a line read from the file
List1.AddItem Format$(sText, "000000") ' add more zeroes if scores can be over 1 million
... loop end
In my signature below is a link to the VB FAQs section. There you can find examples of reading/writing text files
Good luck with your homework
Edited. You are taking a programming class and Visual Basic version 5 is being used? Wow!!!
Last edited by LaVolpe; Jan 8th, 2012 at 08:21 PM.
-
Jan 8th, 2012, 09:35 PM
#3
Re: Visual Basic 5 - Sorting Files by Names
Are you sure you are using VB5 (aka 1997) and not VB 2005?
-
Jan 9th, 2012, 12:41 PM
#4
Thread Starter
New Member
Re: Visual Basic 5 - Sorting Files by Names
Originally Posted by LaVolpe
Tip: Display the high scores in a listbox with the Sorted property set to True. Now, listboxes don't sort numbers as numbers, they sort them as text. If your format your scores with leading zeroes, then it resolves the sorting issue. Example:
Code:
' in a loop that reads your scores
... loop start
... read a score & format like so: sText is a line read from the file
List1.AddItem Format$(sText, "000000") ' add more zeroes if scores can be over 1 million
... loop end
In my signature below is a link to the VB FAQs section. There you can find examples of reading/writing text files
Good luck with your homework
Edited. You are taking a programming class and Visual Basic version 5 is being used? Wow!!!
Sorry, I actually just started VB this year. I know how to write to a text file, but how would I sort it by the value of the score?
Originally Posted by RhinoBull
Are you sure you are using VB5 (aka 1997) and not VB 2005?
Sorry, yeah, VB 2005!
-
Jan 9th, 2012, 01:00 PM
#5
Re: Visual Basic 5 - Sorting Files by Names
That's what I thougth - thanks for confirming.
You need to post your question in VB.Net forum instead.
-
Jan 9th, 2012, 11:59 PM
#6
Re: VB 2005 - Sorting Files by Name
The easiest way without linq (i think) would be:
Just append your lines to your text file unsorted - without the #No in the front
Then when you read your textfile put it in a SortedDictionary(of integer, string) object with the key being the score and the data being the rest...
Then loop through the sorted dictionary to display it.
PS i say without linq since you are just starting and your teacher may think it wasn't your's if u used it
Kris
-
Jan 10th, 2012, 12:48 PM
#7
Thread Starter
New Member
Re: VB 2005 - Sorting Files by Name
Originally Posted by i00
The easiest way without linq (i think) would be:
Just append your lines to your text file unsorted - without the #No in the front
Then when you read your textfile put it in a SortedDictionary(of integer, string) object with the key being the score and the data being the rest...
Then loop through the sorted dictionary to display it.
PS i say without linq since you are just starting and your teacher may think it wasn't your's if u used it
Kris
How do I use a sorted dictionary? Sorry for asking these questions... :P
It sounds pretty simplistic enough for me to use.
-
Jan 10th, 2012, 08:26 PM
#8
PowerPoster
Re: Visual Basic 5 - Sorting Files by Names
Code:
Dim i as Integer
frmMain.File1.Sorted = True
For i = 1 to frmMain.File1..........
Blah
Blah
Next i
Then loop through all of the FileListBox entries then add then to each a 3D Array, or something else to that matter...
I have a huge free products range, of computer software in which you can download using any kind of 64-Bit Web Browser. Also there is coming a Social Networking section that I am making on my Website...
|Ambra Productions Inc. | The Black Sun Society | The Black Shield | Ambra College | Church of the Black Sun | Ambra Productions Inc's Homepage | Boomtick Event's Venues: Ambar Nightclub, Jack Rabbit Slim's, Villa Nightclub and Lucy's Bar | Pasta Ambra | Fish Feast Company | Wallet Wizard | Ambrose Liquor | Ambar Tavern | Ambra University |
Do you wish to do unpaid work for me??? If so, the PM me on this Forum, and then we can get to work, programming for the future of computers go by the name of ThEiMp. This is my ghost writers name. Also my nickname, means that I am: The Imperial of the Technology Industry, so then to make it really short, I just then wrote: The Imp, which is where I get the nickname from...
-
Jan 10th, 2012, 08:54 PM
#9
Re: Visual Basic 5 - Sorting Files by Names
Imp,
did you not see last line in post #4?
-
Jan 11th, 2012, 01:45 AM
#10
Re: Visual Basic 5 - Sorting Files by Names
Moved to VB.Net Forum.
Thanks for the reports guys!
Gary
-
Jan 11th, 2012, 03:48 AM
#11
Re: Visual Basic 2005 - Sorting Files by Names
-
Jan 12th, 2012, 02:16 AM
#12
Re: Visual Basic 2005 - Sorting Files by Names
Duplicate threads have been merged.
j0sh77, please remember to only have one thread per question.
Gary
-
Jan 12th, 2012, 11:29 AM
#13
PowerPoster
Re: Visual Basic 2005 - Sorting Files by Names
-- Totally confused. Was this thread, a total waste of time in the VB6 COM Section??? But .NET works the same, in broad spectrum details. But it's the fine details, that make up COM and .NET different...
PS: Also note that I cannot keep helping the OP, cause I don't know anything about .NET, only Visual Basic COM and also Visual Basic DOS. (Please don't ask me anymore about Visual Basic v1.00 & v2.00 for DOS. Cause I have stopped doing that, many years ago!!)
I have a huge free products range, of computer software in which you can download using any kind of 64-Bit Web Browser. Also there is coming a Social Networking section that I am making on my Website...
|Ambra Productions Inc. | The Black Sun Society | The Black Shield | Ambra College | Church of the Black Sun | Ambra Productions Inc's Homepage | Boomtick Event's Venues: Ambar Nightclub, Jack Rabbit Slim's, Villa Nightclub and Lucy's Bar | Pasta Ambra | Fish Feast Company | Wallet Wizard | Ambrose Liquor | Ambar Tavern | Ambra University |
Do you wish to do unpaid work for me??? If so, the PM me on this Forum, and then we can get to work, programming for the future of computers go by the name of ThEiMp. This is my ghost writers name. Also my nickname, means that I am: The Imperial of the Technology Industry, so then to make it really short, I just then wrote: The Imp, which is where I get the nickname from...
-
Jan 12th, 2012, 12:13 PM
#14
Thread Starter
New Member
Re: Visual Basic 2005 - Sorting Files by Names
Originally Posted by gep13
Duplicate threads have been merged.
j0sh77, please remember to only have one thread per question.
Gary
Sorry for the double post, I hadn't realized you were going to move it!
Originally Posted by ThEiMp
Code:
Dim i as Integer
frmMain.File1.Sorted = True
For i = 1 to frmMain.File1..........
Blah
Blah
Next i
Then loop through all of the FileListBox entries then add then to each a 3D Array, or something else to that matter...
Looking at the responses I'm getting, I'm assuming I won't be able to do this so easily at my level of programming. :P
Is there a way to get a list of all the file names in a folder? I could name a txt file with the score the player received and then inside the file the name and the word the guessed... Then I could take the value of the name of the txt and sort it that way? I don't know if that would work, but its just an idea!
-
Jan 13th, 2012, 12:42 AM
#15
PowerPoster
Re: Visual Basic 2005 - Sorting Files by Names
FileListBox, it comes as a Unbound Control inside VB6 COM. But I guess that you can also use that control inside VB .NET, as well...
I have a huge free products range, of computer software in which you can download using any kind of 64-Bit Web Browser. Also there is coming a Social Networking section that I am making on my Website...
|Ambra Productions Inc. | The Black Sun Society | The Black Shield | Ambra College | Church of the Black Sun | Ambra Productions Inc's Homepage | Boomtick Event's Venues: Ambar Nightclub, Jack Rabbit Slim's, Villa Nightclub and Lucy's Bar | Pasta Ambra | Fish Feast Company | Wallet Wizard | Ambrose Liquor | Ambar Tavern | Ambra University |
Do you wish to do unpaid work for me??? If so, the PM me on this Forum, and then we can get to work, programming for the future of computers go by the name of ThEiMp. This is my ghost writers name. Also my nickname, means that I am: The Imperial of the Technology Industry, so then to make it really short, I just then wrote: The Imp, which is where I get the nickname from...
-
Jan 13th, 2012, 03:08 AM
#16
Re: Visual Basic 2005 - Sorting Files by Names
Originally Posted by ThEiMp
-- Totally confused. Was this thread, a total waste of time in the VB6 COM Section???
Although originally posted in the Visual Basic Forum, the OP is actually using VB.Net, so I merged the threads, and left the question in VB.Net.
Gary
-
Jan 13th, 2012, 12:24 PM
#17
Thread Starter
New Member
Re: Visual Basic 2005 - Sorting Files by Names
Or would there be a way to insert the scores played in one session into an array, and then write the array to a txt file? Then on form load it would recall the data from the txt and store it to an array again? Then I could sort them by their scores.
-
Jan 13th, 2012, 05:51 PM
#18
PowerPoster
Re: Visual Basic 2005 - Sorting Files by Names
-- That could be one of the ways of doing, it. But then I could mention that there could be alot of processor usage and memory consumption, as well. Seeing that you will need to update the txt file, to about something like an .Interval of something like a second, or so at a time or even everytime the ListBox is then Changed, then that could also be one of the very good ideas, to work with...
PS: Visual Basic, any version is very memory hungry, and also it's kernal for the Run-Time Support, is very slow and it uses alot of processor usage from Windows 32/64Bit, as well. But then I can vouch that the 64-Bit Windows, is able to work out the kinks in the Support, from the apps, that is written in VB.NET, but not VB6 COM...
I have a huge free products range, of computer software in which you can download using any kind of 64-Bit Web Browser. Also there is coming a Social Networking section that I am making on my Website...
|Ambra Productions Inc. | The Black Sun Society | The Black Shield | Ambra College | Church of the Black Sun | Ambra Productions Inc's Homepage | Boomtick Event's Venues: Ambar Nightclub, Jack Rabbit Slim's, Villa Nightclub and Lucy's Bar | Pasta Ambra | Fish Feast Company | Wallet Wizard | Ambrose Liquor | Ambar Tavern | Ambra University |
Do you wish to do unpaid work for me??? If so, the PM me on this Forum, and then we can get to work, programming for the future of computers go by the name of ThEiMp. This is my ghost writers name. Also my nickname, means that I am: The Imperial of the Technology Industry, so then to make it really short, I just then wrote: The Imp, which is where I get the nickname from...
-
Jan 13th, 2012, 07:23 PM
#19
Thread Starter
New Member
Re: Visual Basic 2005 - Sorting Files by Names
Originally Posted by ThEiMp
-- That could be one of the ways of doing, it. But then I could mention that there could be alot of processor usage and memory consumption, as well. Seeing that you will need to update the txt file, to about something like an .Interval of something like a second, or so at a time or even everytime the ListBox is then Changed, then that could also be one of the very good ideas, to work with...
Memory usage is fine, but Im also lookin to learn how to do this for other programs!
-
Jan 14th, 2012, 12:10 AM
#20
PowerPoster
Re: Visual Basic 2005 - Sorting Files by Names
Question: How many do you have, right now???
I have a huge free products range, of computer software in which you can download using any kind of 64-Bit Web Browser. Also there is coming a Social Networking section that I am making on my Website...
|Ambra Productions Inc. | The Black Sun Society | The Black Shield | Ambra College | Church of the Black Sun | Ambra Productions Inc's Homepage | Boomtick Event's Venues: Ambar Nightclub, Jack Rabbit Slim's, Villa Nightclub and Lucy's Bar | Pasta Ambra | Fish Feast Company | Wallet Wizard | Ambrose Liquor | Ambar Tavern | Ambra University |
Do you wish to do unpaid work for me??? If so, the PM me on this Forum, and then we can get to work, programming for the future of computers go by the name of ThEiMp. This is my ghost writers name. Also my nickname, means that I am: The Imperial of the Technology Industry, so then to make it really short, I just then wrote: The Imp, which is where I get the nickname from...
-
Jan 14th, 2012, 01:15 AM
#21
Re: Visual Basic 2005 - Sorting Files by Names
Originally Posted by j0sh77
I had the idea to create a text file with the user's score as the title each time a game is won. Then I'd like to write to the file (which I know how to do) with the word that was used in the game and the user's name.
What I don't know how to do is find the file name with the highest integer value. I've looked around and couldn't find any way how to do this.
Another way I thought of doing this was to just use one text file, but insert a line of text per each game played. Then I'd have to find where to insert each line to make sure they're in order, so I could just display the text file in a textbox, which would be easy enough.
Example:
#1 310 - Josh with the word "Cat"
#2 265 - Josh with the word "Dog"
I know a lot of people here don't like to hand out code to people, and have them learn for themselves. I support this and would like to learn for myself, but I don't know what commands to use to get a list of file names or insert lines and certain places in a text file.
Thanks in advance.
You could store the data as follows
Document.txt save in the Bin\Debug folder of your app.
Code:
1,310,Josh with the word,Cat
2,500,Josh with the word,Dog
3,265,Josh with the word,Mouse
Then use the TextFieldParser class to read in the file (see code below). For working with files in general look at the IO.File namespace. The code below uses the TextFileParser with the file structure above where the file is stored in the app folder as Document.txt. A function is used to get the max value for the second column of data, in this case 500.
So this gives you a good starting point, some answers some direction. If something does not work as expected more likely than not I used something from VS2008 as I do not have VS2005 on my home machine, only VS2008.
Code:
Private Sub ReadFileDemo()
Dim Values As Integer() = {}
If Not IO.File.Exists("Document.txt") Then
MessageBox.Show("Document.txt was not located")
Exit Sub
End If
Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser("Document.txt")
MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
MyReader.Delimiters = New String() {","}
Dim currentRow As String()
While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
Console.WriteLine("[{0}] [{1}] [{2}] [{3}]", _
currentRow(0), _
currentRow(1), _
currentRow(2), _
currentRow(3))
Array.Resize(Values, Values.Length + 1)
Values(Values.Length - 1) = CInt(currentRow(1))
Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
Console.WriteLine(ex.Message)
End Try
End While
End Using
For Each item In Values
Console.WriteLine(item)
Next
Console.WriteLine("Max value {0}", MaxValue(Values))
End Sub
Private Function MaxValue(ByVal sender() As Integer) As Integer
Dim Result As Integer = sender(0)
For i As Integer = 1 To sender.Length - 1
If sender(i) > Result Then
Result = sender(i)
End If
Next
Return Result
End Function
-
Jan 14th, 2012, 06:09 AM
#22
PowerPoster
Re: Visual Basic 2005 - Sorting Files by Names
-- I thought that .NET removed the Return command, cause that was connected to the GoSub, command???
I have a huge free products range, of computer software in which you can download using any kind of 64-Bit Web Browser. Also there is coming a Social Networking section that I am making on my Website...
|Ambra Productions Inc. | The Black Sun Society | The Black Shield | Ambra College | Church of the Black Sun | Ambra Productions Inc's Homepage | Boomtick Event's Venues: Ambar Nightclub, Jack Rabbit Slim's, Villa Nightclub and Lucy's Bar | Pasta Ambra | Fish Feast Company | Wallet Wizard | Ambrose Liquor | Ambar Tavern | Ambra University |
Do you wish to do unpaid work for me??? If so, the PM me on this Forum, and then we can get to work, programming for the future of computers go by the name of ThEiMp. This is my ghost writers name. Also my nickname, means that I am: The Imperial of the Technology Industry, so then to make it really short, I just then wrote: The Imp, which is where I get the nickname from...
-
Jan 14th, 2012, 08:35 AM
#23
Re: Visual Basic 2005 - Sorting Files by Names
Originally Posted by ThEiMp
-- I thought that .NET removed the Return command, cause that was connected to the GoSub, command???
Second link in a Google search for VB.NET Return
http://msdn.microsoft.com/en-us/libr...v=VS.100).aspx
-
Jan 14th, 2012, 06:13 PM
#24
PowerPoster
Re: Visual Basic 2005 - Sorting Files by Names
!! YIKES !!
!! CRIKEY !!
!! Thanks for the Input$ !!
I have a huge free products range, of computer software in which you can download using any kind of 64-Bit Web Browser. Also there is coming a Social Networking section that I am making on my Website...
|Ambra Productions Inc. | The Black Sun Society | The Black Shield | Ambra College | Church of the Black Sun | Ambra Productions Inc's Homepage | Boomtick Event's Venues: Ambar Nightclub, Jack Rabbit Slim's, Villa Nightclub and Lucy's Bar | Pasta Ambra | Fish Feast Company | Wallet Wizard | Ambrose Liquor | Ambar Tavern | Ambra University |
Do you wish to do unpaid work for me??? If so, the PM me on this Forum, and then we can get to work, programming for the future of computers go by the name of ThEiMp. This is my ghost writers name. Also my nickname, means that I am: The Imperial of the Technology Industry, so then to make it really short, I just then wrote: The Imp, which is where I get the nickname from...
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
|