Results 1 to 4 of 4

Thread: [RESOLVED] Closing A Form

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2007
    Location
    Somewhere else today
    Posts
    355

    Resolved [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.

  2. #2
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    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
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2007
    Location
    Somewhere else today
    Posts
    355

    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
  •  



Click Here to Expand Forum to Full Width