|
-
Aug 17th, 2009, 09:36 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Closing A Form
The code below checks to see if there are any spaces in the Username. The problem I have is when there is text in this field with a space in it, this procedure is run and I can not close the form.
Code:
Private Sub txtUserName_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtUserName.LostFocus
For i = 0 To txtUserName.TextLength - 1
If txtUserName.Text.Substring(i, 1) = Chr(32) Then
MsgBox("Please Do Not Leave Spaces In User Name! " & vbCr & vbCr & "Please Enter Different Username. ", MsgBoxStyle.Exclamation, "New User")
Exit For
End If
Next
txtUserName.Text = ""
txtUserName.Focus()
End Sub
What I want to happen is when the form is being closed, it ignores this procedure altogether. I could delete the text in the field first and then close the form, or I could not allow a space to be entered in this field.
I have tried "FormClosing" without success.
Any Ideas.
-
Aug 17th, 2009, 10:09 AM
#2
Re: Closing A Form
Don't use the LostFocus event for this. Use the Validate event instead.
Also instead of the loop you can do this:
Code:
If TxtUserName.Text.Contains(" ") Then
Messagebox.Show("Please Do Not Leave Spaces In User Name!" & Environment.NewLine & "Please Enter Different Username.", "User Name")
e.Cancel = True
End If
It'll probably help if you read this too: HowTo: Use validation events in VB .NET
-
Aug 17th, 2009, 10:11 AM
#3
Re: Closing A Form
This seems like it might be part of a login form. Could the code be moved elsewhere? I prefer to do cleanup in a button click event. Alternatively, you could move it into Form Closing (unless that would be too late), and cancel the closing as needed. In that case, you'd actually have to ask a question (Do you really want to close?) rather than just show a messagebox. That might be enough of a solution, anyways.
My usual boring signature: Nothing
 
-
Aug 17th, 2009, 10:19 AM
#4
Thread Starter
Hyperactive Member
Re: Closing A Form
Many thanks for that Joggalo. That has tided my code up alot.
Computerman
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
|