MedVision ad

vb-textboxes (2 Viewers)

{*(00)*}

New Member
Joined
Jun 8, 2005
Messages
29
Location
sydney
Gender
Male
HSC
2006
in regards to textboxes in vb(visual basics) how can you make it so that you can write large numbers e.g. (250,000) in them and make them work in formulas without getting an overflow error?

btw. im making a program that calculates loan repayment. ......mike
 

sunny

meh.
Joined
Jul 7, 2002
Messages
5,350
Gender
Male
HSC
2002
Cast and store the value in the textbox in something like a long variable instead of using the value from the textbox straight out.
 

dddman

Banned
Joined
Jun 13, 2006
Messages
120
Gender
Male
HSC
2005
say u got a textbox like 200060898568748948, now thats fucking long, and that the highest varable you can have in vb is a double.


Private Sub Form1.click()
dim a as double
boxone.text = a
End SUB

i hope that helped, if it didnt send me a pm. UNSW mods are usless
 

{*(00)*}

New Member
Joined
Jun 8, 2005
Messages
29
Location
sydney
Gender
Male
HSC
2006
dddman said:
say u got a textbox like 200060898568748948, now thats fucking long, and that the highest varable you can have in vb is a double.


Private Sub Form1.click()
dim a as double
boxone.text = a
End SUB

i hope that helped, if it didnt send me a pm. UNSW mods are usless
thanks! i didn't know there was a data type called double. is that like an integer?
 

{*(00)*}

New Member
Joined
Jun 8, 2005
Messages
29
Location
sydney
Gender
Male
HSC
2006
thanks... and also, is there a way to include a line graph and make it change as you input different variables into the textboxes?... like um..the amount you owe on the y-axis and the months on the x-axis so it kinda looks like an exponential?
 

dddman

Banned
Joined
Jun 13, 2006
Messages
120
Gender
Male
HSC
2005
{*(00)*} said:
thanks... and also, is there a way to include a line graph and make it change as you input different variables into the textboxes?... like um..the amount you owe on the y-axis and the months on the x-axis so it kinda looks like an exponential?

i believe that you can indeed, however i forgot how it is done, what you need to do is insert a picture and then you set up the variables as the pixels of that image. sorry i cant say much you should really try www.techguy.org, their the best at EVERYsingle programming languages there is
 

sunny

meh.
Joined
Jul 7, 2002
Messages
5,350
Gender
Male
HSC
2002
dddman said:
how do i know the answer of that question isnt gonna result me getting a ban
Because there is a difference between constructive criticism and being outright offensive/insulting.
 

dddman

Banned
Joined
Jun 13, 2006
Messages
120
Gender
Male
HSC
2005
sunny said:
Because there is a difference between constructive criticism and being outright offensive/insulting.
alright hows this

the fact that ur now a 4th yea comp sci student that has graduated has seen to get u out of touch with the fine detail of SDD, im not trying to b a dick but thats what i feel that has become of ya just like anyother student
 

sunny

meh.
Joined
Jul 7, 2002
Messages
5,350
Gender
Male
HSC
2002
No I actually agree with you there. I've been looking for a replacement mod for the SDD forum, but the fact that this forum isn't actually that active (not actually requiring that much modding) and I haven't seen anyone suitable just means it hasn't happened yet.

edit: apologies for the late reply, I haven't been in Sydney.
 
Last edited:

sasquatch

Member
Joined
Aug 15, 2005
Messages
384
Gender
Male
HSC
2006
Graphing a set of data within visual basic as well as other programming languages is quite a complex task.

If you place a picture box within a form of your program, you can use the command picturebox.pset (x, y), color

x refers to the x-coordinate of where the point is to be placed, and y the y-coordinate.

color refers to the colour of the point printed in long format hence if you know any hexadecimal colour codes you can flip around the red and blue bytes (as visual basic's hexadecimal colour format is &HBBGGRR.

Using this or if you would like the line command you can draw a x and y axis within the picture box by using a loop of the pset command:

me! said:
Public Sub DrawAxes()

Call Picture1.Cls

Dim x As Long
Dim y As Long

x = 0
y = 0

For x = 0 To Picture1.ScaleWidth
Picture1.PSet (x, 0 + (Picture1.ScaleHeight / 2)), &HCCCCCC 'this is light grey colour
Next x

For y = 0 To Picture1.ScaleWidth
Picture1.PSet (0 + (Picture1.ScaleWidth / 2), y), &HCCCCCC
Next y

End Sub
You will also need to set the picture box property "AutoRedraw" such that the drawed region remains onscreen. You may also change the ScaleMode property from "Twips" to "Pixel" so that the smallest point to be drawed on the screen is increased (i think twips are 1/15th of the size of a pixel).

Now to draw an exponetial function all you will need to do is enter code similar to the following:

me! said:
Public Sub DrawGraph()
On Error Resume Next
Call DrawAxes

Dim aScale As Double
Dim x As Double
Dim y As Double
Dim z As Double

aScale = 10

For z = 0 To Picture1.ScaleWidth
x = z - (Picture1.ScaleWidth / 2)
x = x / aScale

y = Exp(x) 'change this to suit the equation you wish to graph

y = -1 * y

y = y * aScale

Picture1.PSet (z, y + (Picture1.ScaleHeight / 2)), &H0
DoEvents
Next z

End Sub
With this code above you can graph any time of equation visual basic permits (which is almost all of them) you can change y = sin(x) to graph the sine function, change it to tan(x), make it simply y = x for a linear function, and etc. If you use functions that rapidly increase/decrease (e.g. y = ex, y = 1/x, etc.) you'll notice the graph will have big gaps between the dots. You change this by putting the "ScaleMode" property back to "Twips" and changing the "aScale" value to about "100", or just play around with the scale until it looks as you wish. By putting the picturebox on twip mode the processing time for plotting the graph will increase as there are 15 times more points to plot.

Hope this helps you!
 

{*(00)*}

New Member
Joined
Jun 8, 2005
Messages
29
Location
sydney
Gender
Male
HSC
2006
sasquatch and others:

i tried to put in the code, the axis came up fine,i also tried various functions but i didnt see any function graphs come up.

maybe i did something wrong...


Public Sub DrawGraph()
On Error Resume Next
Call DrawAxes

Dim aScale As Double
Dim x As Double
Dim y As Double
Dim z As Double

aScale = 10

For z = 0 To Picture1.ScaleWidth
x = z - (Picture1.ScaleWidth / 2)
x = x / aScale

y = x 'change this to suit the equation you wish to graph

y = -1 * y

y = y * aScale

Picture1.PSet (z, y + (Picture1.ScaleHeight / 2)), &H0
DoEvents
Next z

End Sub

...and may i ask, how would you plot coordinates?

thanks, {*(00)*}
 

sasquatch

Member
Joined
Aug 15, 2005
Messages
384
Gender
Male
HSC
2006
i copied exactly what you pasted there (as well as my original drawaxes subroutine) and it worked fine.

Make sure that you have AutoRedraw set to True and also that you call only the "DrawGraph" subroutine.

If you see the way i coded it, the "DrawGraph" sub will call the "DrawAxes" sub. If you did something like the following:

Call DrawGraph
Call DrawAxes

The resultant image in the picture box would just be the axes as the second call to drawaxes would clear the picture of the plotted function and the original plotting of the axes, then redraw the axes.

If you dont get what i said, or you cant fix it up, attach your project and ill see if i can find out why theres a problem.

Also to plot coordinates all you need to do is use the PSet command:

Picture1.PSet (x,y), color

Remember x refers to distance horizontally left from the leftmost side of the picture box, and y refers to the distance vertically down from the topmost side of the picture box. Thats why it is neccessary to manipulate the x and y locations within the "DrawGraph" subroutine:

x = z - (Picture1.ScaleWidth / 2)
y = -1 * y
and Picture1.PSet (z, y + (Picture1.ScaleHeight / 2)), &H0

You could say this is evidence..hehe..
View attachment 13233
 
Last edited:

{*(00)*}

New Member
Joined
Jun 8, 2005
Messages
29
Location
sydney
Gender
Male
HSC
2006
urrghh.. i tried my best. ive attached my project. see if you can try and fix it up for me. you're a lifesaver! oh..and btw in my project i put up textboxes for inputting the loan, month and rate amount. do you think i could get the get the graph to change everytime i click calculate corresponding to the loan and month?.. and how do you set the scale on the axis's graph?.......thanks {*(00)*}
 
Last edited:

sasquatch

Member
Joined
Aug 15, 2005
Messages
384
Gender
Male
HSC
2006
There are several reasons why the graph fails to draw. The axes are only drawn as you put the code of the "DrawAxes" subroutine within the "Picture1_Click" event subroutine.

Code:
Private Sub Picture1_Click()

Call Picture1.Cls

Dim x As Long
Dim y As Long

x = 0
y = 0

For x = 0 To Picture1.ScaleWidth
Picture1.PSet (x, 0 + (Picture1.ScaleHeight / 2)), &H8080FF    'this is light grey colour
Next x

For y = 0 To Picture1.ScaleWidth
Picture1.PSet (0 + (Picture1.ScaleWidth / 2), y), &H8080FF
Next y
End Sub
This means that the "DrawGraph" subroutine will never be called as you did not put any code in to call it.

What you will need to do is cut the code segment shown above that is below the
"Private Sub Command1_Click()" and "End Sub" statements and create a new function as such:

Code:
Public Sub DrawAxes()

Call Picture1.Cls

Dim x As Long
Dim y As Long

x = 0
y = 0

For x = 0 To Picture1.ScaleWidth
Picture1.PSet (x, 0 + (Picture1.ScaleHeight / 2)), &H8080FF    'this is light grey colour
Next x

For y = 0 To Picture1.ScaleWidth
Picture1.PSet (0 + (Picture1.ScaleWidth / 2), y), &H8080FF
Next y

End Sub
Then say, within the "calculate" button (which youve called Command1) place the call to the DrawGraph statement:

Code:
Call DrawGraph()
This will mean that when the calculate button is pressed, the graph will be drawn.

--------
For your first question, you can alter the graph each time calculate is pressed, but i dont know what type of function you wish to graph. You only have one output for each of your calculations, so if this data is graphed, it would only be a horizontal line. You need to tell me what you want to graph in more detail so i can help you.
---------
For your second question, to change the value of the scale you will need to do something like the following:

If your "ScaleMode" is on "1 - Twip" then each multiple of 15 for the variable "aScale" will change the scale of the graph by 1 pixel. E.g. If you have aScale = 600 (600 / 15 = 40) Then 40 pixels will represent the value of "1" for the function.

If your "ScaleMode" is on "3 - Pixel" then each multiple of 1 for the variable "aScale" will change the scale of the graph by 1. This is simpler, aScale = 1, then 1 pixel represents 1 for the function.

The advantage of using Twips over Pixels however is that you will get a smoother graph for rapidly increasing/decreasing functions such as the exponential function.

View attachment 13239

Also theres a few things about your code that you could improve, well it doesnt really matter but im just saying anyway.

Code:
Private Sub Form_Load()

Text1.Text = ""
Text2.Text = ""
Text3.Text = ""

Label3.Caption = "enter loan"
Label4.Caption = "enter months"
Label5.Caption = "enter rate"

End Sub
In visual basic you dont have to set properties of the objects in this way. For your text boxes, youve removed the default "Text#" that appears when you first create the object, leaving the text blank. Upon startup it will remain blank (until you enter something of course) so there is no need for the first three statements.

Also within the properties of the lables you can set the caption without the need to do it through code. It doesnt really matter but incase you werent aware then yeah im just saying.
 

{*(00)*}

New Member
Joined
Jun 8, 2005
Messages
29
Location
sydney
Gender
Male
HSC
2006
to give you an idea of what my graph would look like:

only the positive side of both axis would be graphed [ie. (loan amount) x>=0 &
(the duration in months)y>=0]

the graph i think should turn out to be a line, but it should have the duration of the loan on the x axis and the total amount of the loan at that point in time on the y axis. but specific points that show the remaining payments to be made.

it should look similar to this one

http://www.infochoice.com.au/distributions/10135/LoanRepayment.asp

the graph on that website should have a green and a red line, my one will only have one line (the amount owing, the red line on that website), when you put your mouse over the graph on that website it should have a blue line and also print details in the corner. mine wont be that complicated, i think the x and y axis should be enough.

Try using this as test data

loan amount : $250,000
number of months : 300
rate : 12.5

also for the project, the data in the textboxes should be linked to the graph somehow... the first coordinate should be (0,total amount to be repayed) and from that coordinate a line is drawn to (end of payment,0)


thanks. {*(00)*} and thanks also for letting me know that you didnt have to set the caption through the code!
 

sasquatch

Member
Joined
Aug 15, 2005
Messages
384
Gender
Male
HSC
2006
If you wanna draw a straight line, it is much more easier to use the "Line" function rather than using that segment of code for drawing any type of graph. What you will need to do is use the line function:

Code:
Picture1.Line (x[sub]1[/sub], y[sub]1[/sub])-(x[sub]2[/sub], y[sub]2[/sub]), Color
(x1, y1) refers to the first point
(x2, y2) refers to the second point (guess that was obvious)

So you can remove the original call "Call DrawGraph()" and instead put in a segment of code in your calculate button "Command1_Click()" event similar to the following:

Code:
Dim y1 As Double
Dim x2 As Double

y1 = (Label2.Caption * Months) / 8000
x2 = Months / 2

Call Picture1.Cls
Picture1.Line (0, Picture1.ScaleHeight - y1)-(x2, Picture1.ScaleHeight), &H0
For both y1 and x1, you can see their values being divided by a number. This is necessary to set the scale, and depending on what you want for the scale, you may change this.

It you wish to indicate the scale on the graph, it would probably be best achived by placing labels alongside the picturebox.

Hope that helps.
 

{*(00)*}

New Member
Joined
Jun 8, 2005
Messages
29
Location
sydney
Gender
Male
HSC
2006
hey thanks everbody for your support.

ok, here's the thing. i've got these labels, and the text in it (numbers in the 1000's) changes frequently in my program, but i cant get the thousands separator to work despite the fact that i even changed the data format and ticked the box for the thousands separator. similarly, i cant seem to make the numbers to 2 decimal places for the same reason. do you think im doing something wrong?

also, an error comes up every time i refer to a combobox that i had created.
eg (combo1.listindex). it says "type mismatch"
 

Users Who Are Viewing This Thread (Users: 0, Guests: 2)

Top