I'm using VB 6, and my form has a TextBox control. I want the user to be able to enter the time in the textbox, but I have to validate whether the input time is correct or not.
Example:
Textbox1.Text = 236161 '(User Input)
236161 = HHMMSS
The above entered time is wrong because the minutes and seconds are greater than 60.
What code can I use to check the format of the time entered in the textbox?
I suggest that you use a DTPicker control, instead. This is the date/time picker that is available for VB 6 applications, and it essentially forces the user (but much more gently than that makes it sound) to enter a valid time in the proper format. Trying to do this validation yourself is a giant pain in the rear, and not worth the effort since using a built-in control makes it so much simpler.
You can start using this in your project by following these steps:
In the VB 6 IDE, open the "Project" menu, and then click "Components" (or press Ctrl+T).
Scroll down nearly to the bottom of the list and select "Microsoft Windows Common Controls-2" (preferably version 6.0, if available, rather than version 5.0).
Once you click OK, you will find a handful of new controls added to your toolbox. Find and click on the one called DTPicker. It will look something like this:
Drag the DTPicker control to your form.
Make sure that the control you just added is selected, then in the "Properties" window, scroll down to the "Format" property and set it to "2 - dtpTime". This specifies that you want to accept a time value in the DTPicker control, rather than a date.
With .Net you have regex included. With VB 6 you have to use COM or ActiveX libraries and add them as references to your project (http://www.regexlib.com/DisplayPatterns.aspx?cattabindex=4&categoryId=5 or http://support.microsoft.com/kb/818802)
For samples of regex to check time see: http://www.regexlib.com/DisplayPatterns.aspx?cattabindex=4&categoryId=5
Actually, you can use the format function like this:
TestStr = Format(TestDateTime, "HH:mm:ss")
Related
It seems that since Fixpack 6 our edit boxes für setting a time value have changed a bit. If I use the time picker pop-up the selection always starts with 0:00. How can I set the first time to e.g. 8:00?
Could not see any option to set the start time in the properties list.
My best guess (without having FP6 currently at hand) is that it may have moved to a newer version of the TimeTextBox?
Looking at this ticket it seems that the previously used 'visibleRange' property of the constraints has been made obsolete by a new combination of 'pickerMin' and 'pickerMax' properties.
https://bugs.dojotoolkit.org/ticket/18220
The Extension library's 'constraints' complex property (xe:djDateTimeConstraints) has not been upgraded to support these new properties.
Although the Extension Library provides the 'XPages Control' for the Dojo Time Picker, it uses the dijit javascript files from the core domino dojo plugin, thus explaining why one might be upgraded without the other knowing about it.
While you wait for the ExtLib to be updated, perhaps there is a way to get a hold of the TimeTextBox 'dijit' and add the constraints programmatically after the dijit has been initialised.
something like this (note: I have not tested) in some sort of after page load event
dijit.byId('yourdijitsid').constraints.pickerMin = "T08:00:00";
dijit.byId('yourdijitsid').constraints.pickerMax = "T18:00:00";
I am building a GUI in Netbeans - it is for a simple little application - a converter program. Basically, user types whatever it is they want to convert into a text field, selects the conversion from a number of radio buttons (say lbs to kg) and then clicks "Convert".
The thing is, I want the "Convert" button and the radio buttons to behave like this:
Radio buttons and "Convert" button are disabled when program loads.
Radio buttons and "Convert" button will become enabled if user types a number (and only a number) into the text field.
If used deletes what they have typed, everything will be disabled again until they type in another number.
I have managed to set the Radio buttons and "Convert" button up so they are disabled, by unchecking the "enabled" box in the properties for each component. I have also been able to use a simple if statement and the keyTyped event to enable/disable as follows:
private void txtUsrInputKeyTyped(java.awt.event.KeyEvent evt)
{
if (!txtUsrInput.getText().equals(""))
{
btnCalculate.setEnabled(true);
}
else
{
btnCalculate.setEnabled(false);
}
}
I want to extend my code so that if the user accidentally types a letter or symbol into the text field (don't ask me why they'd do that, when they know they must only type a number) then the program will either ignore what they typed, or display an error. The exception to this is, of course, typing a period (.) because they might want to indicate a decimal number.
Any thoughts on how I might do this? Hope what I wrote makes sense!
Your GUI
If you do not already know swing, then learn it. Knowing how all of the components work is always a huge advantage for any developer, and if anything goes wrong, you know exactly how to fix it (or at least have a much better chance). Take a look at this stuff, for some help on getting started.
Checking for a decimal
This looks like a job for regular expressions. What you can do is specify a number [0-9], and a period, \.. Then ensure that either a number or a decimal is typed a certain amount of times. The important thing to note here though, is that the user can only type one decimal, which winds up being:
([0-9]+(\.[0-9]+))
This will allow values like 9, 0.9, 1.9302. It will not allow values like .901. If you wish to allow that, simple swap the first + for a *.:
([0-9]*(\.[0-9]+))
Using it
Because a text box contains a string, you can compare it with your regex. Simply:
if(textBox1.getText().matches("([0-9]+(\.[0-9]+))")) {
// Run some code in here.
}
Well, if GUI is simple, DO NOT use GUI Builder, but code from scratch. Following harder path, you will learn about layouts, actionlisteners and so on ...
ALL,
I've been searching MSDN for the last couple of days and even asked on codeguru but to no availability.
So here goes...
Let's say I have a Windows XP computer whose current locale set to US-en. The calendar at the bottom right corner displays the date as 2/14/2014.
Now, let's say I am developing a program and I need to call the DatePicker control to show the date. And let's say that the user of the program will be around the world. For the sake of simplicity I will pick my own country - Russia. ;-)
Now how do I change the locale to the Russian? I thought I could just use ::SetThreadLocale() and be done, but this function will affect only some resources of my program, namely the dialog titles will be one. But it does not affect the calendar nor the DatePicker control.
Searching MSDN I found 2 more functions: SetLocaleInfo() which can't be called as MSDN says it will affect every single application on the system - not what I want - my only, and SetCalendarInfo() which looks OK, but I'm not sure if this is the one.
So, did I find the right function to call and if not is there a way to do what I want?
Thank you.
I am making a program called "BasicSys". It is a BASIC System simulator that uses a textbox for the console. So far I have everything working great but I need to have the text box act like a command prompt window. It needs to be able to ask for input and retreive the value without allowing the user to modify anything outside of the prompt space (the space where the user should only be able to type is after a ":" or a ">"). Some feilds are password feilds that require either no echoing or having the chartacters replaced by *'s. Is it possible to make a console out of a textbox?
P.S. I also want to know if there are any small BASIC v2 compilers for Win32 so BasicSys can compile and run BASIC programs.
Depending on how realistic you want it to be you can use the API to open a real console window and interact with it. There are many examples available that you can find by searching such as this one. My suggestion though would be to fake it with a multi-line textbox. It would not be very tricky. Set an index every time you draw the prompt, then as long as the cursor is positioned after the index the textbox is read / write. If the user scrolls backwards make the textbox read only. It should be fairly simple using the KeyDown event and setting the ReadOnly property True / False to get a passable "command" window.
In VB6, I have a DTPicker control on a form. (The DTPicker is the calendar date/time selector, included in Microsoft Windows Common Controls-2 6.0, available from the Components dialog.)
While there are many properties to affect the colors of the calendar when it's dropped down, there is no property that allows changing the color of the date that's displayed in the textbox. I'm looking for something like the standard TextBox's ForeColor property.
Does anyone have a little API magic to allow me to simulate that property?
I hate to post something that is not really helpful, but this appears to be something beyond the scope of what Microsoft intended developers to do with the control. While there must certainly be an API call to set the color (Windows certainly knows to paint it black when enabled and gray when disabled), the method to do so escapes me.
My recommendation, should no one else respond with how to do what you need, is to either obtain a new DateTime Picker control with the needed properties (it would seem that there are a few 3rd party options), or "roll your own" control.
FWIW, this same issue exists in VB.NET with the exception being that Microsoft specifically overrides (and then hides) the ForeColor (and BackColor) properties inherited from the generic Control object to do nothing.
I'm going to address two issues with the DatePicker object and a workaround for them.
You cannot put a blank value into DatePicker, which lead me to the 2nd problem.
You cannot change the font color to at least make it appear blank.
To keep the functionality of the DatePicker AND gain the ability to have a blank value and your regular font formatting options (colors, etc), I used two objects. First make a DTP object and set the width so that you can only really see the drop down arrow. For me this was 15. Then make a regular TextBox that is wide enough to hold your date. Put the DTP arrow directly to the right (or left) of the text box. Then you simply add code to the Change event of the DTP to copy its .Value into the .Text of the TextBox like so:
Private Sub MyDTP_Change()
MyUserForm.MyDateTextBox.Text = MyUserForm.MyDTP.Value
End Sub
Then have any data references you need access the MyDateTextBox.Text instead of the MyDTP.Value and presto! You get the functionality of the DTP with the formatting control of a regular TextBox.
EDIT:
Sorry JeffK, I wasn't working with VB in a production environment 9 years ago. :) I would like to add the other side of the functionality to this as well. This allows 2-way syncing between the TextBox and the DTP. IE: Manually enter a date into the TextBox and the DTP Calendar follows. If the TextBox is blank or has an invalid date, the DTP defaults to today's date.
Private Sub MyDateTextBox_Change()
If MyUserForm.MyDateTextBox.Text <> "" And
IsDate(MyUserForm.MyDateTextBox.Text) = True Then
If CDate(MyUserForm.MyDateTextBox.Text) <= MyUserForm.MyDPT.MaxDate And _
CDate(MyUserForm.MyDateTextBox.Text) >= MyUserForm.MyDPT.MinDate Then
MyUserForm.MyDTP.Value = MyUserForm.MyDateTextBox.Text
Else
MyUserForm.MyDTP.Value = Date
End If
Else
MyUserForm.MyDTP.Value = Date
End If
End Sub