Quantcast

Jump to content

» «
Photo

Quick Question

12 replies to this topic
Chalkstar
  • Chalkstar

    QvsQ lab monkey

  • BUSTED!
  • Joined: 08 Nov 2004

#1

Posted 20 April 2005 - 09:49 AM Edited by Chalkstar2188, 20 April 2005 - 09:56 AM.

One quick question, on visual basic, I have forgotten how to tell numbers to round up. I think it has something to do with multiplying by 100 or something.

But on a more important note, what kind of programs are both useful and look good, that are able to be made in VB. Any suggestions?

Before you say "learn a real language", it is for my Information Sytems class, where we HAVE to use visual basic. My teacher is just moving so slow, I want to create something and show him, so that he allows me to move forward, ahead of the class.
He doesn't know all that much beyond the basics, and he gets his own coding wrong often. He does help a little but I haven't learn much this year. confused.gif

Cheers.


Just another thing, code for checking that what was entered in the InputBox is a number? I don't want to have to resort to looping.

BenMillard
  • BenMillard

    aka Cerbera

  • Members
  • Joined: 22 Jun 2002
  • None

#2

Posted 20 April 2005 - 02:46 PM Edited by Cerbera, 20 April 2005 - 06:33 PM.

Read the help documents in the MSDN disks. They give you examples of how to use every control, property, command, conversion and so on.

For your specific questions, to round up a number use this:
CODE
CInt(number) + 1 'removes fractional part, effectively rounding down, then adds one to round up

To perform a mathematically correct rounding, use this:
CODE
Round(expression [,numdecimalplaces]) 'only a decimal offset of 0.5 and higher is rounded up

Programs which are useful and look good are probably too complicated for your project. Simple games can work quite well, such as naughts and crosses (tic tac toe) or battleships or other turn-based stuff like that.

To test if a value is a number:
CODE
IsNumeric(expression)

All this was found from the MSDN documentation.

Luke
  • Luke

    suckmyrocket

  • Inactive Staff
  • Joined: 01 Dec 2003
  • None

#3

Posted 20 April 2005 - 03:46 PM

Cerbera's answered most of what you want, just stick it together into a sub, for example:
CODE
Private Sub cmdGetNumber_Click()
   Dim strInput As String
   Dim intNumber As Integer

   strInput = InputBox$("Number:")
   If IsNumeric(strInput) Then
       If strInput < 100 And strInput > -100 Then
           intNumber = CInt(strInput)
       Else
           MsgBox "Number is too large or too small", vbExclamation, "Error"
           Exit Sub
       End If
   Else
       MsgBox "You must enter a valid number", vbExclamation, "Error"
       Exit Sub
   End If

   Debug.Print "Number entered: " & intNumber
End Sub

I'm doing GCSE IT here in England, and there's almost no programming involved at all, consider yourself lucky, my teacher wouldn't be able to teach it even if it were on the syllabus.

VB's often underrated, it's perfectly capable for most programs, I do feel that it's often coded in untidily and badly, but the only real limits with VB are when you try hooking, making DLLs (with the exception of ActiveX ones) and handling data types VB doesn't use. I still use VB for simple things, I'm no fan of VB.NET, but I have no problems with VB6.

Knightmare
  • Knightmare

    man tongues

  • $outh $ide Hoodz
  • Joined: 18 Nov 2001

#4

Posted 20 April 2005 - 03:46 PM

If(IsNumeric(inputbox.text)==False) Then inputbox.text = "Enter a number, dumbass"

Make a payroll program or something.

BenMillard
  • BenMillard

    aka Cerbera

  • Members
  • Joined: 22 Jun 2002
  • None

#5

Posted 20 April 2005 - 06:43 PM

Oh yeah, as Smithers points out, you need to make sure you put the code in the correct control event. Checking user input is best done in the _Validate or _Change events. I prefer using the _Validate event because it avoids any automated change of the text whilst validating causing the validation code to run again, making changes, running again and getting into an infinite loop. _Validate just runs when the control loses the focus and is the way programs like MSPaint check whether the "Stretch/Skew" values are within range and actually numbers.

You can also do things like make the contents lowercase or uppercase in this event without the user getting confused about why the output on screen isn't matching what they are typing in.

Knightmare, giving a textbox the name "inputbox" isn't a good idea since there are input dialogue boxes created from the "Input" function. The conventional way of naming a textbox is "txt<name>[(index)]" where the three-letter prefix shows what type of object it is. Same goes for variables and other stuff, although I hardly ever bother naming them correctly. Makes it easy to see exactly what type of control you are using in the code without having to switch back to the form view.

Chalkstar
  • Chalkstar

    QvsQ lab monkey

  • BUSTED!
  • Joined: 08 Nov 2004

#6

Posted 21 April 2005 - 01:01 AM

QUOTE (Knightmare @ Apr 21 2005, 01:46)
Make a payroll program or something.

I might just do something similar to that. Thanks to everyone for helping, I was just waiting on that part for part of the program, where dividing 10 by 3 for example, bring about 8 deciminal place digits. I'm just going to play around with it a bit, make it generate random numbers between 1 and 1000, instead of making the user input a number. Eventually I'll make a checkbox to allow numbers to be inputed (?sp) by the user, and for it to generate random numbers.

Thanks again.

segosa
  • segosa

    Ghetto Star

  • BUSTED!
  • Joined: 15 May 2002

#7

Posted 21 April 2005 - 03:11 PM

QUOTE (Cerbera @ Apr 20 2005, 20:43)
Knightmare, giving a textbox the name "inputbox" isn't a good idea since there are input dialogue boxes created from the "Input" function. The conventional way of naming a textbox is "txt<name>[(index)]" where the three-letter prefix shows what type of object it is. Same goes for variables and other stuff, although I hardly ever bother naming them correctly. Makes it easy to see exactly what type of control you are using in the code without having to switch back to the form view.

It was an example.

He doesn't even code VB. He does C++ where naming controls doesn't follow conventions like that. If you notice, he used == which is invalid in VB. And I repeat, it was an example. Who cares what he named it for something as trivial as that?

Luke
  • Luke

    suckmyrocket

  • Inactive Staff
  • Joined: 01 Dec 2003
  • None

#8

Posted 21 April 2005 - 03:25 PM

QUOTE (Segosa @ Apr 21 2005, 15:11)
It was an example.

He doesn't even code VB. He does C++ where naming controls doesn't follow conventions like that. If you notice, he used == which is invalid in VB. And I repeat, it was an example. Who cares what he named it for something as trivial as that?

Well, to be honest, Cerbera is trying to help out here, that's not a bad thing, no matter how you look at it, Knightmare on the other hand hasn't really helped out in terms of providing some example code, just an idea.

I agree though, anyone who's going to program in VB needs to learn the conventions attached to naming things.

Knightmare
  • Knightmare

    man tongues

  • $outh $ide Hoodz
  • Joined: 18 Nov 2001

#9

Posted 21 April 2005 - 04:06 PM

QUOTE (Cerbera @ Apr 20 2005, 12:43)
Knightmare, giving a textbox the name "inputbox" isn't a good idea since there are input dialogue boxes created from the "Input" function. The conventional way of naming a textbox is "txt<name>[(index)]" where the three-letter prefix shows what type of object it is. Same goes for variables and other stuff, although I hardly ever bother naming them correctly. Makes it easy to see exactly what type of control you are using in the code without having to switch back to the form view.

I agree, I just used inputbox as an example since thats what he used in the first post. Actually, we'd get points taken off our assignments if we didn't use lbl, txt, frm, etc.

And yea, I'm currently in c++ data structures, haven't had VB in a while, sorry if my syntax is off.

Chalkstar
  • Chalkstar

    QvsQ lab monkey

  • BUSTED!
  • Joined: 08 Nov 2004

#10

Posted 22 April 2005 - 07:14 AM

QUOTE (Knightmare @ Apr 22 2005, 02:06)
Actually, we'd get points taken off our assignments if we didn't use lbl, txt, frm, etc.


And it makes someone else trying to help job's harder. A friend in class today had a textbox named lblMessage. I started to wonder if they chose the right subject. confused.gif

Svip
  • Svip

    I eat babies

  • Members
  • Joined: 12 Nov 2001

#11

Posted 22 April 2005 - 07:55 AM

I need to ask a question myself;

When you say 'round up', do you mean like rounding in the correct manner, or just remove all the decimals?

In case you are using some sort random generator for a variable, you shouldn't round up, you should just remove decimals:

CODE
Randomize: <variable> = int(rnd * <number>) [+ 1]


<number> should be the max amount of numbers the <variable> should go through. If you set it to 5, the <variable> can be 0,1,2,3,4. That's why you can in case add the [+ 1], as this will make it more confortable looking; 1,2,3,4,5.

But if that is not what you're talking about, then sorry.

Chalkstar
  • Chalkstar

    QvsQ lab monkey

  • BUSTED!
  • Joined: 08 Nov 2004

#12

Posted 22 April 2005 - 11:44 AM

That's exactly what I wanted. Thanks. If you want a copy (as if) just PM me. I'll be more then happy for any critisism on it. Still a few bugs I'm guessing, and things that could be more efficient but I'm learning. smile.gif

Svip
  • Svip

    I eat babies

  • Members
  • Joined: 12 Nov 2001

#13

Posted 22 April 2005 - 02:56 PM

QUOTE (Chalkstar2188 @ Apr 22 2005, 13:44)
That's exactly what I wanted. Thanks. If you want a copy (as if) just PM me. I'll be more then happy for any critisism on it. Still a few bugs I'm guessing, and things that could be more efficient but I'm learning. smile.gif

NOTE: In VB, rnd generates a number between 0 and 1 without 1, with about 10 decimales.

That is why you need the int() to make it more logic.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users