Msgbox in VB

1. How to close msgbox when user clicks "cancel" button?
2. How to convert text entered in textbox automatically converted to uppercase when the user leaves the textbox?
Suppose types "john" in the textbox (text1.text)
the display must be "JOHN"
3. I want to have a pop-up a msgbox when user enters less or more than 8 characters in the textbox, asking him to enter text 8 character long?
Thanks very much

Vasi04Asked:
Who is Participating?
 
Gruff82Connect With a Mentor Commented:
Hi Vasi04

What I would do is add the lost focus event handler for the textbox, so it get's called every time the user moves away from the text box.

I would then cause the text to become upercase and validate the length. If it isn't 8 charachters return the user to the text box to re-enter.

Here is the code:

' Event Handler gets called as user moves away
Private Sub Text1_LostFocus()
   
    ' make the text uppercase
    Text1.Text = UCase$(Text1.Text)

    ' Test the length to see if it is 8 charachters (trim off any trailing spaces)
    If Len(trim$(Text1.Text)) <> 8 Then
       
        ' Fails validation so select from the start to the end of text in text box and return focus
        MsgBox "You must enter 8 charachters", vbInformation + vbOKOnly
        Text1.SelStart = 0
        Text1.SelLength = Len(Text1.Text)
        Text1.SetFocus
    End If
   
End Sub

Hope this helps
0
 
mladenoviczConnect With a Mentor Commented:
1. it will be closed it self
2.3.
Private Sub Text1_Validate(Cancel As Boolean)
    Text1.Text = UCase(Text1.Text)
    If Len(Text1.Text) <> 8 Then
        MsgBox "invalid length. must be 8"
        Cancel = True
    End If
End Sub

0
 
sgayatriConnect With a Mentor Commented:
What do yu mean by saying 'close msgbox when click cancel'
Clicking any button whether ok or cancel (or whatever way you display a msgbox ) will close yur msgbox
You can use the button information after a click.
ex:
vbinfo = MsgBox("Click any button", vbYesNoCancel, "Message")
If vbinfo = vbYes Then
  MsgBox "Yes"
'  do something
Else
  If vbinfo = vbNo Then
     MsgBox "No"
    'do something more
  Else
    MsgBox "Cancel"
   'do something else
  End If
End If

 For the remaining questions, the above two answers will suffice.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
fds_fatboyConnect With a Mentor Commented:
Gruff82:
Validation on LostFocus gives problems. The Lostfocus event in general gives problems because it goes off after the next control has got focus and in certain circumstances, doesn't go off at all should only be used in moderation: For an example, try this:

Create a new standard VB project with a single form. Add the following 2 textboxes, Text1 and Text2; add a command button Command1.
Add the following code and run it. The When ti runs immediatey tab away from Text1 making it fail validation and see the results...

Option Explicit

Private Sub Command1_Click()
    Unload Me
End Sub

Private Sub Form_Load()
    Text1.Text = "Arry"
    Text2.Text = "Bert"
End Sub

Private Sub Text1_LostFocus()
    If Len(Text1.Text) < 8 Then
        MsgBox ("Text1 less than 8 characters")
    End If
    Text1.SetFocus
End Sub
Private Sub Text2_LostFocus()
    If Len(Text2.Text) < 8 Then
        MsgBox ("Text2 less than 8 characters")
    End If
    Text2.SetFocus
End Sub

0
 
fds_fatboyCommented:
Also, another point about not using LostFocus event for validation is - your user can avoid validating a field simply by not visiting it. Using my previous example, run the program. Change the contents of Text1 to Arry1234 (valid - 8 characters) and click on the button. Hooray, passed validation - but the contents of Text2 are invalid.
0
 
Gruff82Commented:
Your right about the lost focus event, probably best to use the validate event handler with cancel = true to force the field to be valid before moving on to the next one.

My mistake - thankfully its friday
0
 
JR2003Connect With a Mentor Commented:
Use these functions in a module and call them with the textbox as a paramewter on the form load.

e.g to call it in the form load
TextBoxNumericOnly Text1

Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

Private Const ES_NUMBER = &H2000&
Private Const ES_UPPERCASE = &H8&
Private Const ES_LOWERCASE = &H10&
Private Const GWL_STYLE = (-16)


'Makes TextBox only receive numerics
Public Function TextBoxNumericOnly(TextBox As TextBox) As Boolean

    Dim iTmp As Long
    iTmp = SetWindowLong(TextBox.hwnd, GWL_STYLE, GetWindowLong(TextBox.hwnd, GWL_STYLE) Or ES_NUMBER)

End Function

'Makes TextBox only receive uppercase letters
Public Function TextBoxUpperCaseOnly(TextBox As TextBox) As Boolean

    Dim iTmp As Long
    iTmp = SetWindowLong(TextBox.hwnd, GWL_STYLE, GetWindowLong(TextBox.hwnd, GWL_STYLE) Or ES_UPPERCASE)

End Function

'Makes TextBox only receive Lowercase letters
Public Function TextBoxLowerCaseOnly(TextBox As TextBox) As Boolean

    Dim iTmp As Long
    iTmp = SetWindowLong(TextBox.hwnd, GWL_STYLE, GetWindowLong(TextBox.hwnd, GWL_STYLE) Or ES_LOWERCASE)

End Function
0
 
Vasi04Author Commented:
I have used codes from gruff82 and mladenovicz
Thanks all for your support.

I tried to use code from JR2003 for UPPERCASE. I could not get it compiled.
If you could explain how to incorporate this, it would be great!

Thanks all
0
 
mladenoviczCommented:
I think you need this line too for JR2003 code

Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
0
 
JR2003Commented:
Vasi04
Looks like you need the GetWindowLong as mladenovicz pointed out (thanks mladenovicz)
JR
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.