Results 1 to 3 of 3

Thread: VB input boxes

  1. #1
    Flak Monkey! Dorza's Avatar
    Join Date
    Jul 2003
    UK - South Wales
    17 times in 15 posts
    • Dorza's system
      • Motherboard:
      • Asus P5B Deluxe - WiFi
      • CPU:
      • Q6600 @ 3.06Ghz
      • Memory:
      • 2GB Crucial
      • Storage:
      • 500GB Samsung SpinPoint
      • Graphics card(s):
      • Geforce 9600GT
      • PSU:
      • Cosair HX520W
      • Case:
      • LianLi something something or other
      • Monitor(s):
      • Eizo FlexScan S1910 (1280*1024)
      • Internet:
      • 2mb Virgin (when they want to give me that: else 1mb)

    VB input boxes

    Ok just a few questions here

    1, Is there a way you can prefent the user from clicking on "ok" or "Cancel" if they do not enter any data into the box? Either by blanking the buttons out or by displaying a message box when they click a button, to inform them they need to enter data first.

    2, is there a way you can get rid of the "Cancel" button showing only the "ok" button

    3,Can you change the colour and font of the input box, and possibly have a background on it.

    thanks for any help given it would be much appreciated. I cant find any infromation on this in the crappy vb book the college gave

  2. #2
    I am totally new to visual basic myself, and in fact I dunno y im replying bcoz i dont know a lot about it! I just digged out an old copy of visual basic out of my drawer but for some reason the installer only install the actual Visual Basic, anything else is corrupted, so i perssume this was downloaded from kazaa as a faulty ISO or summit... anyways im in the same boat as u... clueless to VB (well maybe you are not clueless) but if you would have some time, perhaps we can chat on msn sometime about it ?

    I hope to find some kind of guide book and make an internet browser designed specifically for a website, but of course ill start easy, ive managed to make a main windows with a new button, give each window a title, make an About Box with an ok button, return button and System Information button, the program also displays the time at the bottom of the screen. Prob is, now im stuck i am completley lost in the world of visual basic!

  3. #3
    Join Date
    Mar 2004
    0 times in 0 posts
    For on-line information about Visual Basic a good place to start is Microsoft's MSDN (Microsoft Developer Network). For VB specific, start here:

    To try to answer your questions as simply as possible, I think the inputbox might be a bit too limited, and if so you're best off making your own form with the look and layout you want. You can show a MODAL form with a line something like:

    MyPasswordForm.Show vbModal
    ' Any code here won't run until the MyPasswordForm is unloaded

    In my examples here, assume you have a form called MyPasswordForm with a field (a textbox) the user is entering into, which is called txtField.
    An OK button, called cmdOK.
    A Cancel button called cmdCancel.
    VB gives controls default names, like Text1, Command1 and so on, but to make your code clear you can change them by clicking once on the control in question, pressing F4 to bring up the properties window for that control, find the "Name" property, and change the name to something more appropriate, eg. txtField.

    There are various ways to validate what a user enters into a field. You could, for example, write validation code in the txtField CHANGE event, which only enables the OK button if the correct value is entered. For example, double-click on the textbox control on your form and this will bring up a code window with the following code:

    Private Sub txtField_Change()

    End Sub

    This subroutine is called an EVENT, and every time the value within txtField is changed (eg. when the user presses a key to enter a character into the box), this routine will be fired.

    Add the following code:

    Private Sub txtField_Change()
    If txtField = "MYPASSWORD" Then
    cmdOK.Enabled = True
    cmdOK.Enabled = False
    End If
    End Sub

    Also, click once on the OK button on your form, press F4 for properties and locate the "Enabled" property, and set this to False. That way when you first run the program the OK button is disabled, and only becomes enabled when your user enters the correct word (in this case MYPASSWORD) into the textbox.

    Run this, and give it a go. The OK button won't get enabled until you enter MYPASSWORD (in uppercase) into the textbox.

    An alternative approach is to validate the contents of the field when the user clicks the OK button.

    To do this, first set the OK button's Enabled property back to True.
    Now, remove the code from the txtField_Change() routine.
    Double-click on the OK button. You should see this event routine:

    Private Sub cmdOK_Click()

    End Sub

    Add the following code here:

    Private Sub cmdOK_Click()
    If txtField = "MYPASSWORD" Then
    MsgBox "Password is valid!", vbOKOnly + vbInformation, "Hurray"
    Unload Me ' Unload this form
    If MsgBox("Password is invalid, would you like to try again?", vbYesNo + vbQuestion, "Oops") = vbNo Then
    Unload Me ' Unload this form
    End If
    End If
    End Sub

    You can see a number of things here, including the two ways in which you can call MSGBOX. You can call it just to pop up a message, but MSGBOX also returns a value to tell you which button the user clicked on. Also, see the second parameter on MSGBOX which describes the buttons and icon to show. For example, vbOKOnly + vbInformation will show just the OK button, with a QUESTION MARK in the msgbox window. If you are in the Visual Basic code editing window, you should see a list of these pop up if you type something like: msgbox "Hello", (you'll see the list as soon as you press the last comma). By adding one or more of these pre-defined constants you control the way the msgbox is displayed.

    A few other things to help you along.

    First, in the first example I could have replaced this code:

    If txtField = "MYPASSWORD" Then
    cmdOK.Enabled = True
    cmdOK.Enabled = False
    End If

    with a single line:

    cmdOK.Enabled = (txtField = "MYPASSWORD")

    either will work.

    On the OK button, have a look for the property "Default". If you set this to true, then the OK button will be activated if the user presses the ENTER while in the textbox (or anywhere on the form).

    On the Cancel button, set the property "Cancel" to true. The Cancel button's _CLICK event will fire if the user presses the ESCape button while in the textbox.

    You can set properties in code if you like, such as: cmdOK.Default = True

    Hope this helps and doesn't confuse.
    Last edited by spindle; 23-03-2004 at 10:08 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. database and VB help
    By s4ch117 in forum Software
    Replies: 8
    Last Post: 09-02-2004, 01:41 PM
  2. Does VB support animated Gifs?
    By Dorza in forum Software
    Replies: 3
    Last Post: 25-01-2004, 05:15 PM
  3. How the hell do I do this in VB?
    By Steve in forum Software
    Replies: 7
    Last Post: 22-12-2003, 12:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts