• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

VB.Net Help (If statements)

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

kraven

Member
Joined
Nov 11, 2004
My final is due on monday and i can't seem to get this data validation to work. I've created a computer building program that would allow the user to select computer components and then have a final order at the end.

The problem that I've come across is when the user would select "next" and go to the next screen/form. I want the program to check and see if all required components have been selected and if not, show a message box letting them know that they need to select the required components.

I have tried:

If radCpu1.Enabled = False Then
MessageBox.Show("please input")
ElseIf radCpu2.Enabled = False Then
MessageBox.Show("please input")
ElseIf radCpu3.Enabled = False Then
MessageBox.Show("please input")
End If

but it doesn't do that operation and allows the user to go to the next screen without selecting a CPU. All help is greatly appreciated and if screenshots, etc would be helpful just say so and I'll throw them up.

*edit* i have it set to "please input" just as a place holder for the text. also, the rad buttons are being used to define variables declared in another form. would that be the issue? i wouldn't think so but, meh.

*second edit*
figured out that i need it to be set to .checked instead of .enabled. now i need to figure out what kind of if statement i want to use or if i want to base the if statement off the variables instead of the state of the rad buttons. i.e. if intcpu = 0 then blah blah instead of if radcpu = check then blah blah.

*third edit*
i figured out that i can have it check the values for the variables in the final form to see if there is anything in there. if it's 0, then it hasn't been selected and needs a value entered (can't build a computer without a cpu). so it's all set up and working! woot! if i come up with something else i can't figure out, i'll post it here.

*fourth edit*
what's a quick easy way to have the messagebox.show display a picture and nothing else?
 
Last edited:
Ive not programmed in Basic for a few yrs and tbh Im not all that clued up on it, however I do program for a large company using C/C++.

Anyway without seeing ur code and understanding quite what ur doing I cant help too much but for ur IF statements u may want to use the logical or - ||.

You would want something like:

if( (!chkbox1.checked) || (!chkbox2.checked) || (!chkbox3.checked) )
then display an error message etc

This would check that each checkbox if one of them returned false then it would display error message etc. Like I said Ive not done Basic for some time so I wouldnt know the exact syntax but something along those lines should work i would think
 
Ive not programmed in Basic for a few yrs and tbh Im not all that clued up on it, however I do program for a large company using C/C++.

Anyway without seeing ur code and understanding quite what ur doing I cant help too much but for ur IF statements u may want to use the logical or - ||.

You would want something like:

if( (!chkbox1.checked) || (!chkbox2.checked) || (!chkbox3.checked) )
then display an error message etc

This would check that each checkbox if one of them returned false then it would display error message etc. Like I said Ive not done Basic for some time so I wouldnt know the exact syntax but something along those lines should work i would think

Yeah, I haven't coded it yet but I think I figured it out when I was falling asleep last night. I'm going to try a If And statement.

Like If blah and blah = blah then...

I think this will work how I want it to. I haven't done any C/C++ so the || make no sense to me.
 
I think this will work how I want it to. I haven't done any C/C++ so the || make no sense to me.

|| means or, so

Code:
if( (!chkbox1.checked) || (!chkbox2.checked) || (!chkbox3.checked) )

is in essence

Code:
if( (!chkbox1.checked) or (!chkbox2.checked) or (!chkbox3.checked) )

So that if anyone of those are checked it will run the code in the if statement.

I haven't programmed VB but I would think there is something similar.
 
|| means or, so

Code:
if( (!chkbox1.checked) || (!chkbox2.checked) || (!chkbox3.checked) )

is in essence

Code:
if( (!chkbox1.checked) or (!chkbox2.checked) or (!chkbox3.checked) )

So that if anyone of those are not checked it will run the code in the if statement.

I haven't programmed VB but I would think there is something similar.

Cheers for explaining that Insane, however the ! is a logical NOT therefore it actually reads:

Code:
(!chkbox1.checked) or in psuedo (checkbox.checked == false) or checkbox unchecked

Kraven, programming is very much logical. The first thing u must understand is logic, I was fortunate enough to do a yr of it at uni and it has come in very useful. It may be worth spending some time learning logic before jumpin into programming. In the mean time its a Javascript site but this site will give u the basic logical operators:

http://www.pageresource.com/jscript/jifelse.htm
 
Also just to clarify the logical operators are universal across all programming languages (that I've came into contact with) as well as other aspects such as design. You will need logic to understand how the computer works, this will save u time in coding and make it easier to read which will become more important as projects get bigger.

Finally, my personal opinion would be to move to C# as soon as u understand the basics (syntax, logic etc) in VB. Basic is not used in industry much and because it is a high level language changing to lower is harder than the other way around. Ive not had the opportunity to work in C# much but from what Ive read and what work colleagues have told me it is a very strong language and very nice to use.
 
I figured out what was going on and set up a "If And" statement that checked to make sure all values were true before continuing. It was rather complicated for what I wanted to do so I'm not going to post the code here as it would take up probably more than one post.

Thanks for the help guys. :beer:
 
It really took up that much? Did you try the mothod I said? Sad I know but I'm interested in how u done it, can u post a small section just so I get an idea?
 
sure, gimme a sec. btw, the vb is for college. after this i take java.

'check to ensure all components are selected
If Order.intcpu >= 1 And Order.inthdd >= 1 And Order.intram >= 1 And Order.intgfx >= 1 And Order.intmsk >= 1 And Order.intmonitor >= 1 And Order.intcase >= 1 Then

i figured the best way to validate is to check the values instead of the state of the radio buttons. values are passed to another form, this checks to ensure that there is a greater than 1 value, probably could have use a 0 but it's all good cause nothing's less than 20 anyway. well, this is just one small piece, there's other stuff to it but i think this was the clincher.
 
Last edited:
Try:

If (Order.intcpu) And (Order.inthdd) And (Order.intram) And (Order.intgfx) And (Order.intmsk) And (Order.intmonitor) And (Order.intcase) Then

You might get a bit of extra credit for this as an IF always evaluates to a boolean result. Any result that is not NULL (0) is true. So that would check that a value is not NULL and may impress ur tutor a bit lol

EDIT: U might need to put the whole thing in brackets as well
 
I've already submitted my assignment but I'll keep this type of thing in mind for when I go into Java. I will try this later on though to see how it actually works.
 
Back