Using a text in a textbox for a file name?

Please support our VB.NET advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
Thread Solved
Reply

Join Date: May 2009
Posts: 40
Reputation: lolwtf is an unknown quantity at this point 
Solved Threads: 0
lolwtf lolwtf is offline Offline
Light Poster

Using a text in a textbox for a file name?

 
0
  #1
May 20th, 2009
I working on a personal project of mine and got stuck. The program is a simple quiz in which the user answers some questions, and their results are saved into a text file. Reading and writing to text files is easy so I don't need help with that. On my form I have a textbox and two buttons. The user will input his/her name into the textbox and hit a "take quiz" button. That button will create a text file, but i want the text files name to be what the user just entered in the textbox. The idea is that for my second button "Load File" will pull up the text file under their name. Any help would be appreciated.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 114
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: Using a text in a textbox for a file name?

 
0
  #2
May 20th, 2009
The only problem is to validate user input as a valid file name. AFAIK the following should be 100% bullet-proof method
  1. Dim FileName As String
  2. Dim bIsValidFileName As Boolean
  3. Dim i As Integer
  4.  
  5. ' Get user input
  6. FileName = TextBox1.Text
  7. ' Validate
  8. bIsValidFileName = True
  9. ' Can't be empty
  10. If String.IsNullOrEmpty(FileName) Then
  11. bIsValidFileName = False
  12. End If
  13. ' Check invalid characters
  14. For i = 0 To Path.GetInvalidFileNameChars.GetUpperBound(0)
  15. If FileName.IndexOf(Path.GetInvalidFileNameChars(i)) >= 0 Then
  16. ' Illegal character
  17. bIsValidFileName = False
  18. Exit For
  19. End If
  20. Next i
  21. ' Save or reprompt input
  22. If bIsValidFileName Then
  23. ' Save the file
  24. Else
  25. MessageBox.Show("'" & FileName & "' is not a valid filename", "Filename", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  26. End If
Instead of flagging bad characters in the for loop you could also simply remove them
  1. ' Check invalid characters
  2. For i = 0 To Path.GetInvalidFileNameChars.GetUpperBound(0)
  3. If FileName.IndexOf(Path.GetInvalidFileNameChars(i)) >= 0 Then
  4. ' Remove illegal character
  5. FileName.Replace(Path.GetInvalidFileNameChars(i), "")
  6. End If
  7. Next i
  8. ' Now you have to check that you don't end up with an empty string after replacing characters
  9. If String.IsNullOrEmpty(FileName) Then
  10. bIsValidFileName = False
  11. End If
I suggest putting the code above in a function which returns the file name so you can call it both before saving and loading the file.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 40
Reputation: lolwtf is an unknown quantity at this point 
Solved Threads: 0
lolwtf lolwtf is offline Offline
Light Poster

Re: Using a text in a textbox for a file name?

 
0
  #3
May 20th, 2009
Thank you so much for you help. I have been stuck on this for days! thanks again!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC