Jump to content
xisto Community
Sign in to follow this  
iGuest

VB6 Need Help With Object Validation Any help will be appreciated

Recommended Posts

OK, only been using VB6 for less than 2 weeks, but have gone passed expectations and have found things I have overlooked and am now needing either an explanation on how to do it or some good places they will clearly explain how to go about it.To be honest, I'm not wanting to learn this language, it's just a requirement that I must get out of the way before moving onto a much larger project.I will apologise now if I speak a lot of C/C++ Objects/Properties/Methods/Functions, I can explain programming better under this language.So back to VB,Just to ignore my program, lets say I have a simple form, it has 2 textboxes and a command button to process the information.Each textbox has field validation, using validate(), so it raises errors to the user as soon as they try leaving the box, so both text boxes must validate before they can leave that box then they can process the information.However, the first textbox can validate and instead of them changing focus to the second textbox, they click straight on the button, since the first textbox is valid, they're allowed to move on, but they skipped the second textbox and that's the problem.So I need a means to make sure that both fields contain information before allowing the information to be process. I don't want to use the process button to do this (but will if I can't get it working).So my intentions is, I'm going to disable this button and want to enable it when both textboxes contain valid information, which means they won't be able to bypass it.So how would I do this, I think it's called Object Validation, but I'm uncertain if that's VB terminology. While the textbox validation is Field Validation.So any clues, tips, sites anything that will help me?Cheers,MC

Share this post


Link to post
Share on other sites

I have the solution for you - infact I've done the exact same thing with more than 15 textfields in a screen and a save/update button - which gets enabled only when all fields are validated properly. But you gotta gimme time till tomorrow morn. It's 2am here and am almost dozing off on my keyboard. So just hold your breath for another 7-8hours :DRegards,m^e

Share this post


Link to post
Share on other sites

I'm still holding my breath in anticipation :DI've read things like Control Arrays, but whether this has anything to do with it, I am not sure.Basically I want a similar effect to an OK/Apply like dialog, in which if data is changed it will enable the Apply button, but I want an effect where if all the requirements are met then it will enable the button.If it requires Control Arrays and this indexing method, then I guess that's the method I might test, but still please provide anything you have, because it sounds like your 15 textboxes compared to my 11 textboxes is what I'm after. The 2 textboxes above was just a small part of describing the problem.Cheers,MC

Share this post


Link to post
Share on other sites

Ok here goes - there are two approaches. The first one is a little easier but lame which happens to be with control arrays. I guess you know the basic idea of control arrays - you simply copy-paste a single textbox 11 times and they will be put into a virtual array where you can refer to each textbox using its array index.

 

Now all the textboxes (rather most of the vb controls) have an associated event called LostFocus (along with another one named GotFocus). You need to create a skeleton procedure with the lostfocus event. Whenever the cursor leaves a textbox it'll jump to this routine. But here comes the lame part. Supposing all your textboxes have different validation routines for different types of data. You got to manually check each textbox everyone one of them looses focus and enable/disable the button accordingly. With a few textboxes this is fine - but in a screen with a large number of boxes - imagine the amount of cpu cycle this eats up.. on slower systems, you'll notice the a considerable lag before the button is enabled. But unfortunately in vb6 this is the only way to do it - since it doesn't support OOP.

 

However, if you're using vb.net - then the solution can be far more elegant. You can, infact, derive a subclass from the textbox control and attach a validation routine to it inside the class. This way - whenever you try to leave a textbox and the data is incorrect, the cursor is immediately placed back into the textbox with the gotfocus event. Additionaly - you can keep a counter which increases/decreases by 1 whenever a textbox is validated or has invalid data. After lostfocus, you can throw an event which then checks this counter - say if it stands at 11, then the button is enabled else - it remains disabled.

 

I dunno if I could clearly explain - but if you need examples, I'll be happy to provide for both of them. I'd rather go for the second one, but your project might limit you to vb6. In that case solution 1 is the only way to go.

 

Regards,

m^e

 

P.S. - about OK/Apply buttons - you'll notice that the button is enabled as soon as some data is changed in one of the fields. it really doesn't check whether the data is valid or not. Validation is done only after you press OK. That way the validation cycle happens ONLY ONCE when you press the button and not EVERYTIME the textboxes loose focus.

Share this post


Link to post
Share on other sites

I suggest you to use separate function for validation which return True/False value which you can furture use on controls validation method. In these function you have to write each validation for control, after this just select appropriate control by 'active control' then passed control array index key to 'select case' and you get appropriate code of selected text box. Hope I can explain clearly, If you need code for this feel free to contact.Bye.. Have a nice day.. KING-King

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.