| | |
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
![]() |
•
•
Join Date: May 2009
Posts: 40
Reputation:
Solved Threads: 0
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.
The only problem is to validate user input as a valid file name. AFAIK the following should be 100% bullet-proof method
Instead of flagging bad characters in the for loop you could also simply remove them
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.
VB.NET Syntax (Toggle Plain Text)
Dim FileName As String Dim bIsValidFileName As Boolean Dim i As Integer ' Get user input FileName = TextBox1.Text ' Validate bIsValidFileName = True ' Can't be empty If String.IsNullOrEmpty(FileName) Then bIsValidFileName = False End If ' Check invalid characters For i = 0 To Path.GetInvalidFileNameChars.GetUpperBound(0) If FileName.IndexOf(Path.GetInvalidFileNameChars(i)) >= 0 Then ' Illegal character bIsValidFileName = False Exit For End If Next i ' Save or reprompt input If bIsValidFileName Then ' Save the file Else MessageBox.Show("'" & FileName & "' is not a valid filename", "Filename", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) End If
VB.NET Syntax (Toggle Plain Text)
' Check invalid characters For i = 0 To Path.GetInvalidFileNameChars.GetUpperBound(0) If FileName.IndexOf(Path.GetInvalidFileNameChars(i)) >= 0 Then ' Remove illegal character FileName.Replace(Path.GetInvalidFileNameChars(i), "") End If Next i ' Now you have to check that you don't end up with an empty string after replacing characters If String.IsNullOrEmpty(FileName) Then bIsValidFileName = False End If
Teme64 @ Windows Developer Blog
![]() |
Similar Threads
- How to Place text from a textbox into an array? (C++)
- How to change color of text in textbox? (Visual Basic 4 / 5 / 6)
- Comparison of text in textbox (VB.NET)
- Writing text from textbox to file... (C++)
Other Threads in the VB.NET Forum
- Previous Thread: Address arrays via a for next loop
- Next Thread: DataGrid to Excel Export as a ZIP file
| Thread Tools | Search this Thread |
"crystal .net .net2005 30minutes 2008 access add application arithmetic array assignment basic beginner box button buttons click code combo component connectionstring convert cpu cuesent data database databasesearch datagrid datagridview datetimepicker design designer dissertation dissertations dissertationthesis dosconsolevb.net editvb.net exists firewall folder forms html image images isnumericfuntioncall listview math mobile module mssqlbackend mysql navigate number opacity open peertopeervideostreaming picturebox1 picturebox2 port print printpreview problemwithinstallation project record regex reports" reuse right-to-left savedialog serial settings socket sqldatbase sqlserver storedprocedure string temp textbox timer txttoxmlconverter updown useraccounts usercontol usercontrol vb vb.net vb.netcode vb.nettoolboxvisualbasic2008sidebar vbnet view vista visual visualbasic visualbasic.net visualstudio visualstudio.net web wpf wrapingcode xml





