Help with coding for GUI


Posted by gart on December 20, 2001 9:24 PM

I need to be able to calculate the area of a triangle and place it into a list box. The GUI will have 3 text boxes to enter the lengths of the side to be used in the formulas. After putting the area in a separate list box I need it to draw the triangle in a picture box with the same command button.
The 3 corners of the triangle need to be (0,0); (c,0); and (x,y)

a,b,c are the three respective sides entered into the text boxes.

x=(a^2+c^2-b^2)/2*c

y=(a^2-x^2)^(1/2)

The area=(c*y)/2

I would really appreciate help with this. I have Visual Basic to design the GUI if someone could help me with the code with the formulas.
Thanks a ton
Gart

Posted by Mark O'Brien on December 21, 2001 8:26 AM

Brad,

This sounds suspiciously like a school programming project and to get help from here would be cheating. However, since I cheated a lot in school and programmers "borrow" code all the time, here's the functions you're looking for.

Public Function x(a As Single, b As Single, c As Single) As Single
x = (a ^ 2 + c ^ 2 - b ^ 2) / 2 * c
End Function
Public Function y(a As Single, b As Single, c As Single) As Single
y = (a ^ 2 - x ^ 2) ^ (1 / 2)
End Function
Public Function Area(c As Single, y As Single) As Single
Area = (c * y) / 2
End Function

Posted by gart on December 21, 2001 9:38 AM

Mark,
I appreciate the help. I'm still confused if you could help me out. How do I make the values of a, b, and c be entered into a GUI by text boxes? I've written:
a = Val(txt1.Text)
b = Val(txt2.Text)
c = Val(txt3.Text)

and then I also need it to show the area in a list box. I need to draw it in a pic box too. I've tried:

picTriangle.Line (0, 0)-(c, 0)
picTriangle.Line (c, 0)-(x, y)
picTriangle.Line (x, y)-(0, 0)

I would really appreciate this if you could show your expertise on this.

thanks,
Brad


Brad,

Posted by Mark O'Brien on December 21, 2001 10:08 AM

No problem on the first part, second part I can't really help with.

Put those functions I gave you in a module are on the form or whatever you're using for the GUI.

To use the functions, you need this code.


' Declare Variables
dim a as single
dim b as single
dim c as single
dim MyX as single
dim MyY as single
dim MyArea as single

' Initialise Variables
a = Val(txt1.Text)
b = Val(txt2.Text)
c = Val(txt3.Text)

' To calculate x and y

MyX = x(a,b,c)
MyY = y(a,b,c)

' To calculate Area
MyArea = area(c, MyY)

' you should now use MyArea, MyX and MyY for drawing your triangle.

HTH

Mark,



Posted by brad on December 21, 2001 10:20 AM

Mark,
You're the man. I can get most of it to work but there's still some bugs. How do you write an error message? That is if I enter in wrong data in text box how can I pop up an error message saying the user screwed up? Also, what is the Cls method? I need to be able to clear the triangle (i did get it to draw!) and redraw everytime I enter new data for the sides.

Thanks,
Brad

No problem on the first part, second part I can't really help with. Put those functions I gave you in a module are on the form or whatever you're using for the GUI. To use the functions, you need this code.