| | |
Using a text in a textbox for a file name?
Please support our VB.NET advertiser: Intel Parallel Studio Home
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 |
.net .net2008 2005 2008 access account arithmetic array basic beginner bing browser button buttons center check code crystalreport cuesent data database datagrid datagridview date datetimepicker designer dissertation dissertations dissertationtopic dropdownlist excel fade file-dialog filter forms ftp generatetags google hardcopy html images input insert intel internet mobile monitor net networking objects open output panel passingparameters pdf picturebox picturebox1 port position print printing problem save searchbox searchvb.net select serial settings shutdown soap sqlserver survey table tcp temperature text textbox timer timespan toolbox transparency trim update user vb vb.net vb.netformclosing()eventpictureboxmessagebox vb2008 vba vbnet visual visualbasic visualbasic.net visualstudio.net visualstudio2008 web winforms wpf wrapingcode year





