[RESOLVED] Advice needed for a message box if not enough data entered.
Hi all,
I have created a program that solves the right-angled triangle by just needing to input two known quantities. I have adapted this code from some I found on another website, it never actually worked, so I spent two weeks figuring out how to get it to work. I have finally succeeded, what I would like to do is display a message box if the user has not entered the required two inputs and presses the 'Calculate' button (see image). I just want it to say "At least two known quantities are required".
I have some code that clears all the text boxes so you can enter new data, but I have tried countless times to find a way of coding so the message box appears.
I would appreciate it if someone could steer me in the right direction.
Many thanks,
Steve.
Code:
Imports System.String
Imports System.Math
Public Class Form1
Dim angleA As Single = 90
Dim angleB As Single
Dim angleC As Single
Dim sidea As Single
Dim sideb As Single
Dim sidec As Single
#Region " If value is Null "
Public Function isItNullString(ByVal value) As Boolean
If value = "" Then
Return True
Else
Return False
End If
End Function
#End Region
#Region " To find one side if two sides known - Pythagoras Theorem "
Public Sub PythagoreanTheorem()
If sidec <> 0 And sidea <> 0 And sideb = 0 Then
sideb = Math.Sqrt((sidec * sidec) - (sidea * sidea))
TextBoxSideb.Text = sideb
ElseIf sideb <> 0 And sidea <> 0 And sidec = 0 Then
sidec = Math.Sqrt((sideb * sideb) + (sidea * sidea))
TextBoxSidec.Text = sidec
ElseIf sidec <> 0 And sideb <> 0 And sidea = 0 Then
sidea = Math.Sqrt((sidec * sidec) - (sideb * sideb))
TextBoxSidea.Text = sidea
End If
End Sub
#End Region
#Region " Find Angle B or Angle C "
Public Sub angleSums()
If angleC = 0 And angleB <> 0 Then
angleC = 180 - angleA - angleB
TextBoxAngleC.Text = angleC
End If
If angleB = 0 And angleC <> 0 Then
angleB = 180 - angleA - angleC
TextBoxAngleB.Text = angleB
End If
End Sub
#End Region
#Region " Angle B and one side known "
Public Sub angleBasWorkingAngle()
'Angle B and side b known, find side c
If angleB <> 0 And sideb <> 0 Then
sidec = sideb / Sin(degreesToRadians(angleB))
TextBoxSidec.Text = sidec
End If
'Angle B and side a known, find side b
If angleB <> 0 And sidea <> 0 Then
sideb = sidea * Tan(degreesToRadians(angleB))
TextBoxSideb.Text = sideb
End If
'Angle B and side b known, find side a
If angleB <> 0 And sideb <> 0 Then
sidea = sideb / Tan(degreesToRadians(angleB))
TextBoxSidea.Text = sidea
End If
'Angle B and side b known, find side a
If angleB <> 0 And sidec <> 0 Then
sidea = sidec * Cos(degreesToRadians(angleB))
TextBoxSidea.Text = sidea
End If
End Sub
#End Region
#Region " Find Angle C "
Public Sub findangleC()
If angleC = 0 Then
'cos
angleC = radiansToDegrees(Acos(sideb / sidec))
TextBoxAngleC.Text = angleC
End If
End Sub
#End Region
#Region " Feed Values "
Public Sub feedValues()
If Not isItNullString(TextBoxSideb.Text) Then
sideb = TextBoxSideb.Text
Else
sideb = 0
End If
If Not isItNullString(TextBoxSidea.Text) Then
sidea = TextBoxSidea.Text
Else
sidea = 0
End If
If Not isItNullString(TextBoxSidec.Text) Then
sidec = TextBoxSidec.Text
Else
sidec = 0
End If
If Not isItNullString(TextBoxAngleC.Text) Then
angleC = TextBoxAngleC.Text
Else
angleC = 0
End If
If Not isItNullString(TextBoxAngleB.Text) Then
angleB = TextBoxAngleB.Text
Else
angleB = 0
End If
End Sub
#End Region
#Region " Convert Degrees to Radians "
Public Function degreesToRadians(ByVal degrees As Double) As Double
Dim radians As Double
radians = (Math.PI * degrees) / 180
Return radians
End Function
#End Region
#Region " Convert Radians to Degrees "
Public Function radiansToDegrees(ByVal radians As Double) As Double
Dim degrees As Double
degrees = radians * (180 / Math.PI)
Return degrees
End Function
#End Region
#Region " Calculate all Data "
Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
For aaa As Integer = 0 To 2 'I just repeated this 6 times because I have a lot of processing power
'you can do it twice.
feedValues()
PythagoreanTheorem()
angleSums()
angleBasWorkingAngle()
findangleC()
Next
End Sub
#End Region
#Region " Clear all input boxes "
Public Sub ClearTextBox(ByVal root As Control)
For Each ctrl As Control In root.Controls
ClearTextBox(ctrl)
If TypeOf ctrl Is TextBox Then
CType(ctrl, TextBox).Text = String.Empty
End If
Next ctrl
End Sub
#End Region
#Region " Button to clear all Input boxes "
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
ClearTextBox(Me)
End Sub
#End Region
#Region " Button to Exit the program "
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
#End Region
End Class
Last edited by SteveHeather; Feb 16th, 2012 at 06:37 AM.
Re: Advice needed for a message box if not enough data entered.
Thanks for your reply, I tried this but it is OK if no values are entered and the calculate button is clicked, you do get the message box appear, but it takes three clicks on the 'OK' to get rid of it. If you enter one input and press calculate, all the text boxes have 'NaN' in them.
Code:
#Region " Calculate all Data "
Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
For aaa As Integer = 0 To 2 'I just repeated this 6 times because I have a lot of processing power
'you can do it twice.
If TextBoxSidea.Text.Length = 0 And TextBoxSideb.Text.Length = 0 And TextBoxSidec.Text.Length = 0 And TextBoxAngleB.Text.Length = 0 And TextBoxAngleC.Text.Length = 0 Then
MessageBox.Show("At least two known quantities are required")
Else
feedValues()
PythagoreanTheorem()
angleSums()
angleBasWorkingAngle()
findangleC()
End If
Next
End Sub
#End Region
Re: Advice needed for a message box if not enough data entered.
Oh... it seems you used my previous code, i have changed it after looking in your form screen shoot which give me a good understand of what you want to do, so please use the new code.
Re: Advice needed for a message box if not enough data entered.
Thanks for that, almost there, the message box appears if you enter less than the two required inputs with no problem. But if you enter at least two inputs and press the 'Calculate' Button, it is only calculating 4 text boxes, it seems to be missing the 'Angle B'. I have posted an image as well as the code.
Many thanks.
Code:
#Region " Calculate all Data "
Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
If IsDataEnough() = True Then
feedValues()
PythagoreanTheorem()
angleSums()
angleBasWorkingAngle()
findangleC()
Else
MessageBox.Show("At least two known quantities are required")
End If
End Sub
Public Function IsDataEnough() As Boolean
Dim intRequiredDataCount As Integer = 5
For Each ctrl As Control In Me.Controls
If TypeOf ctrl Is TextBox Then
If CType(ctrl, TextBox).Text.Length = 0 Then
intRequiredDataCount -= 1
End If
End If
Next ctrl
Return (intRequiredDataCount >= 2)
End Function
#End Region
Re: Advice needed for a message box if not enough data entered.
Originally Posted by SteveHeather
But if you enter at least two inputs and press the 'Calculate' Button, it is only calculating 4 text boxes, it seems to be missing the 'Angle B'. I have posted an image as well as the code.
You said you need only two of five values, so it is your rule to calculate the missing!
If you need 3 values to be entered replace Return (intRequiredDataCount >= 2) with Return (intRequiredDataCount >= 3)
Edit:
From latest screen shoot B should be 90 - 53.1301 = 36.8699
Re: Advice needed for a message box if not enough data entered.
Sorry I must have mislead somewhere, you are correct, You only need to enter two values to get all the correct answers. So in the screenshot I entered sidea as '4' and sideb as '3', then press 'Calculate' and it should populate all the text boxes with the correct answers. It gave all the answers before the alterations. If you look at the first code, it does all the calculations for the angles.
So there must be something in this new code that is preventing angleB from working.
Edit: It worked OK with this code in "For aaa As Integer = 0 To 2"
Re: Advice needed for a message box if not enough data entered.
To get all the correct answers in all five text boxes, you only need to enter two known quantities, this could be any of the two. It could be 'sidea and AngleB', or it could be 'sidec and sideb'. You then press calculate and all the text boxes should show with all the correct answers.
Before this new code with the message box, it worked OK. But now it doesn't seem to put the code in Textbox Angle B.
So I am lost as what to do next really, sorry about this, but I am new to this programming.
Re: Advice needed for a message box if not enough data entered.
you have a finite problem and you know all of the parts and the requirements!
why would you or do you need to write or use a piece of code that is designed to walk about an object and discover the parts you think you need by type and then add them up and return a truth value about the number of things needed that does not have a clue about what you need!
the fat that it is a generic ( or attempt at) function, it forgets to include the requirements of such a generic textbox identifying the use of thingummy bob.
I preferred you other solution, where you! knew! something about what you wanted to achieve but where wandering abit on the logic of it all...
your problem is another arithmetically solvable problem that does not need all that programming!
you wrote if x.length=0 and y.length=0 and so on then...
the truth value in your system is -1 and 0 true and false
you can look at the sumation of all the truth values and get a result equal to the number of boxes filled in!
that means you could simply as if that number was what ever size you wanted to test for!
this is an example...
the clumbsy way...
if (x.length>0) + (y.length>0) + (z.length>0) < -1 then do stuff
that is an example where at least 1 thing needed to be filled
you may wonder why (-1) thats because each test produces -1 / 0 as a result so the result can only be 0 ,-1,-2,-3 in this instance
i hope that helps
no looping no other generic function ( if it was build properly) no more hoping that only the specific textboxes would be found on the page!
you will not be able to add a text box to that dialogue box using the other method
Re: Advice needed for a message box if not enough data entered.
As explained earlier, I am new to VB programming, it looks like I am way in over my head here, perhaps I should go away and learn a lot more before asking for any advice. I tend to learn by other peoples examples, that I have always done.
But I do appreciate all the help you guys have given me.
Re: Advice needed for a message box if not enough data entered.
as you are a newbie - would you be open to a number of programming suggestions that would ake your programming fatsre and easier to manage ( almost always )?
Re: Advice needed for a message box if not enough data entered.
Originally Posted by incidentals
as you are a newbie - would you be open to a number of programming suggestions that would ake your programming fatsre and easier to manage ( almost always )?
these are conceptual not language dependant!
here to help
Yes I am open to all suggestions, I find that I learn more by following other peoples examples, it is just the way I have worked for the last 50 years. I am a lecturer of Engineering and CAD, and I learnt my trade from other peoples experience.
So yes, please show me some other ways, if you don't mind.
Re: Advice needed for a message box if not enough data entered.
G'd Afternoon all!
Steve, you don't need to go away, this is a great place to learn. In fact right now you're learning.
I'm sure that Incidentals didn't meant to be hard on you. We all once were new to programming.
Get back here and let us know how is your project going. We'll be happy to help.
Re: Advice needed for a message box if not enough data entered.
good at least we are both long in the tooth then.
some simple but usefull techniques coming right up ( they might appear a little strange at first but please think through them!
(1) build in a way that makes fixing easy
make code look the same reguardless of what you are coding
write code in an ordered way, with a given structure...
(2) include in your structured approach a way to escape more quickly from any situation!
that is put reasons to get out of a program subroutine function or clause at the front of the structures.
it is important to do this with all structures including tests...
if you consider a complex logic expression... one that uses ANDs has to test all of the things before it can make up its mind, but an expression that is front loaded with ORs can make up its mind as soon as an ORed thing is valid or not!
(3)stop asking question in your code!
questions cost valuable processing time... look more closely at the logic of your program and remove as many questions as you can.
(4)Always mess with your logical statements!
just because it was how you thought the logic of the problem presented itself to you does not mean that it is the only was to express the logic of the problem.
try negating a logical statement and re-write the code and remember to view the new code in light of the previous rules!
(5) Always annotate your code
especially if it is compat and relies on an argument that you might forget later, the best pieces of code involve a eureka moment, you will remember the reasons why you wrote the code that way for about as long as the adrenhalin is still flowing so write it down.
(6) learn all the odd things about your environments - (the optional bits and default values).
like you can add and multiple truth values - it does not sound like much - but you will not realise just how important that is until you need it! knowing that the mid() takes arguments for from and the name of the string, but the length is optional and the default is the rest of the string. these things make building solutions quicker and simpler in the long run! but they come with experiance.
These 6 simple rules will help you approach problems from a number of different angles and help you develope solutions that often do not need all the conditional statements that others suggest and will greatly reduce the shear size of your code. It is applicable to all languages.
do these meet with an understanding
if they do then you should be able to make a small but significant change to your code, have a go and i will get back to you or i can just send a quick rewrite, but i think there is enough understanding here for you to do the rewrite your self.
The most important FACT - not a rule - is the first solution is never the right solution, whether it works or not, there will always be a better way to approach the problem, unless you are "national lottery winner" type lucky programmer!
Re: Advice needed for a message box if not enough data entered.
Ill make a minor suggestion about one of you functions....
Change this:-
vbnet Code:
Public Function isItNullString(ByVal value) As Boolean
If value = "" Then
Return True
Else
Return False
End If
End Function
To:-
vbnet Code:
Public Function isItNullString(ByVal value As String) As Boolean
If value Is Nothing OrElse value = "" Then
Return True
Else
Return False
End If
End Function
You should always practice explicitly declaring your function arguments otherwise you could be in a world of hurt. Also, I've altered it to test for Nothing because it would cause an exception to be thrown if you test for zero-length on a null string.
Re: Advice needed for a message box if not enough data entered.
It will take some time for me to digest all those rules. But thanks, I will have to get some teaching books I think. Start with something a little more plain and simple.
Re: Advice needed for a message box if not enough data entered.
Originally Posted by SteveHeather
It will take some time for me to digest all those rules. But thanks, I will have to get some teaching books I think. Start with something a little more plain and simple.
Its not so much a rule as it is a good practice that would save you a ton of headache.
Re: Advice needed for a message box if not enough data entered.
i will apply the rules to the code niya improved for you
Code:
Public Function isItNullString(ByVal value As String) As Boolean
If value Is Nothing OrElse value = "" Then
Return True
Else
Return False
End If
End Function
my variation - obeying the rules - asumming his code works
this is covered by RULE 3 do not ask questions!
Code:
Public Function isItNullString(ByVal value As String) As Boolean
return (value Is Nothing OrElse value = "" )
End Function
it should be annotated of course! Rule 5 and is dependant on rule 6 ( i know logical expressions have values in there own rite )
this re-write is based on the same argument as "if test=true then" is the same as "if test then"
if you can be sure of 1 of these tests, is nothing and ="" then the return statement would be even simpler!!
something like ...
using Rule 4 - mess with your logicals!
return (value.length=0) ' its another way to look at emptyness of strings
hope that shows the difference in approach
Last edited by incidentals; Feb 16th, 2012 at 05:36 PM.
Re: Advice needed for a message box if not enough data entered.
Thanks for that code Niya, and I do see what you are doing Incidentals, it is examples like this that I learn from, of course there aren't any books that teach you this way of coding, this is why I come to forums like this one, to learn from experienced people that are at the front end.
I will have a go at completely re-writting the code I submitted trying to use your methodology. Or if you can find time would it be possible to write my code in your style, cheating I know, but it is a way I learn much easier. As you said, long in the tooth.
Thanks guys,
Steve.
Edit: That new code for the NullString works satisfactorily.
Last edited by SteveHeather; Feb 17th, 2012 at 06:32 AM.
Re: Advice needed for a message box if not enough data entered.
Originally Posted by incidentals
if you are using this to teach, i would include the labels known angle, adjacent, opposite and the cos tan or sin equations that operated
good for getting the stuff into their heads!
here to help ( i will wait for your rewrite - cheeting not the best way to learn EVER)
Incidentals, I do appreciate all the help you have given me with this problem, however, I will repeat what I have said before; It has always been the case with me that I learn from others examples, for instance; I have just received a reply about my previous post regarding a zoom code for a tabbed webbrowser, the member has shown me by why of example using the correct method. Now that will stick with me for the future, I can now use that information for other projects.
I can assure you that I will not be able to come up with a better solution for my angle calculator. I understand if you dont want to show me what my original post question was about, but i understood that these types of forums were for help, whether practical or by giving hints.
I am too long in the tooth as you put it, so it is actual examples I learn by.
Thanks, sorry for being an old moaner, but that is what I do
Edit: If you ever want to learn AutoCAD, I am your man..
Steve.
Last edited by SteveHeather; Feb 18th, 2012 at 06:19 AM.
Re: Advice needed for a message box if not enough data entered.
So far I have got the message box appearing if you only enter one piece of data, no problem, the message appears, you then enter the second piece of data so that you have the required two inputs, press calculate and it gives you all the answers in all text boxes.
But when you clear all text boxes and then enter the required two inputs for another solution, you again get the message box appear asking for at least two inputs, to get it to calculate all the answers, you have to enter data into three text boxes.
So I have no idea what is happening there, maybe one of you good people can help me out with this.
This is the code I have so far:
vb Code:
Public Function IsDataEnough() As Boolean
Dim intRequiredDataCount As Integer = 5
For Each ctrl As Control In Me.Controls
If TypeOf ctrl Is TextBox Then
If CType(ctrl, TextBox).Text.Length = 0 Then
intRequiredDataCount -= 1
End If
End If
Next ctrl
Return (intRequiredDataCount >= 2)
End Function
Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
If IsDataEnough() = True Then
feedValues()
PythagoreanTheorem()
angleSums()
angleBasWorkingAngle()
findangleC()
findangleB()
Else
MsgBox("You must enter at least two known quantities", _
Re: Advice needed for a message box if not enough data entered.
Thanks 4x2y,
I have deleted that line you suggested, but that doesn't solve my problem.
And the answer to your question, what if the user enters angles 'B' and 'C', that situation cannot arise because the sides can have any variation of lengths with two angles known. So you never have a situation where you enter two angles.
Re: Advice needed for a message box if not enough data entered.
4x2y makes avid point, unfortunately the code supplied by him and myself cannot solve your problem now that he has seen the error in your process, mine however can be modified in light of it.
i was a little later than your responce to 4x2y
this is a program with open acessable textboxes anyone can put anything into them so yes you can enter just 2 values in the angle boxes and your system will say all is ok and then fail!
Re: Advice needed for a message box if not enough data entered.
Originally Posted by incidentals
4x2y makes avid point, unfortunately the code supplied by him and myself cannot solve your problem now that he has seen the error in your process, mine however can be modified in light of it.
i was a little later than your responce to 4x2y
this is a program with open acessable textboxes anyone can put anything into them so yes you can enter just 2 values in the angle boxes and your system will say all is ok and then fail!
here to help
Ok I understand that, but that was not my question, in any case, you never have a situation where you have all three angles known, because that has infinite answers to the sides.
Re: Advice needed for a message box if not enough data entered.
further to my last comment...
on reflection if you would never put 2 angles...
why would you put less than 2 values before pressing solve?
why do you think the first is not worthy of testing but the second one is worthy of a msgbox?
you need to add a test for at least 1 checkable item being selected before you worry about checking for the rest, it would be normal to do the checking after the button press or to only enable the button if enough was filled in!
Re: Advice needed for a message box if not enough data entered.
Originally Posted by SteveHeather
I have deleted that line you suggested, but that doesn't solve my problem.
I know it will not solve your problem, i just want to inform you.
Originally Posted by SteveHeather
And the answer to your question, what if the user enters angles 'B' and 'C', that situation cannot arise because the sides can have any variation of lengths with two angles known. So you never have a situation where you enter two angles.
I mean what is your precautions if the user did?
You must not expect all your users have any background info about Right Triangle theory.
Re: Advice needed for a message box if not enough data entered.
Originally Posted by incidentals
further to my last comment...
on reflection if you would never put 2 angles...
why would you put less than 2 values before pressing solve?
why do you think the first is not worthy of testing but the second one is worthy of a msgbox?
you need to add a test for at least 1 checkable item being selected before you worry about checking for the rest, it would be normal to do the checking after the button press or to only enable the button if enough was filled in!
no code just advice
Incidentals, I am not some 16 year old student that you can give a lesson too. I am here for help, so please, if all you are going to do is run me down on an relatively simple request, please don't respond at all.
I am here for constructive help and examples, not a school maths lesson.
Re: Advice needed for a message box if not enough data entered.
@SteveHeather
It is very hard to predict what cause the problem, it must reside on other parts of your code, so if you need fast and accurate help, submit your full source code (without any binary files).
Re: [RESOLVED] Advice needed for a message box if not enough data entered.
Originally Posted by incidentals
the idea here is to get you to talk your way through your problems until you have refined and solved them.
simply supplying example code will not help you resolve your programing short comings.
I do not talk to you like a 16year old, you are not the oldest of my "pupils"
the answer to your simple question would just help you get into a bigger muddle...
i do not think you are daft, or need spoon feeding, or ranted at.
I think you need appropriate help.
In light of what 4x2y and myself have said or supplied you are either going to have to rework your thoughts or just demand a quick fix
which do you want?
my next communication will be an example ( promise)
Incidentals, I did ask a specific question about a specific problem, you then chose to go off on a tangent about 'what if the user enters two angles, and why do I need a message box for this, or for that'
I started using VB 2010, two weeks ago, so personally i don't think I have done too bad at it.
I am sure in time I would have realised about 'what if the user enters just two angles, should I put a message box up for that'.
And I will again re-iterate what I have said countless times before; "I learn from practical examples much better than somebody giving me a lecturer on the fine details of why we should do this or that".
So I am asking for an answer to the question I posed, it is entirely up to you if you want to give me that answer or not. But I can assure you, I will find the solution to the problem some how, with or without your help.
Re: [RESOLVED] Advice needed for a message box if not enough data entered.
I would appreciate it if someone could steer me in the right direction.
this is what you asked for!
your original attempt to check all of the pieces was a fine was to do the task, the check for at least 2 used textboxes was not!
inlight of our joint discoveries
the rule is:
there is enough if 2 boxes are used but at least 1 side is required
you have named variables to test against
Code:
Dim angleA As Single = 90
Dim angleB As Single
Dim angleC As Single
Dim sidea As Single
Dim sideb As Single
Dim sidec As Single
so
Code:
function ok(ab as single,ac as single,sa as single,sb as single,sc as single)as boolean
dim atest as boolean
dim stest as boolean
ok=false
atest = ((ab>0)-(ac>0))*-1
stest = ((sa>0)-(sb>0)-(sc>0))*-1
if (stest=1 and atest>0) or (stest>2) then ok=true
end function
use by calling
Code:
result=ok(angleb,anglec,sidea,sideb,sidec)
as promised a piece of test code to answer revised question - with out explaination
here to help
2x4y is correct the only place we can see the message is in your supplied code, we may need more to help further
Re: [RESOLVED] Advice needed for a message box if not enough data entered.
Thanks for that piece of code, I do understand how you have done that, very clever way of doing it. I don't understand how to insert it and get it to work though.
However, you have both uncovered areas that need to be looked at, I have now seen something else, if a user were to enter say, 44 in angleb, and 44 in anglec, and a side length, the calculation would still work, but be wrong. So I now need to look at a test for making sure that all three angles add up to 180 degrees.
So I think I will have to sit down, take a deep breath and re-work the whole code. Maybe this project was not such a good idea for a beginner.
Re: [RESOLVED] Advice needed for a message box if not enough data entered.
No project is a bad project for a beginner!
The choice is based purely on the number of valium in the cupboard and the amount of hair left to be pulled!
As I was trying to say earlier, programming is all about thinking through the problem from all angles and taking account of the users.
GWB said it well when he talked all that rubbish about the unknowns and the knowns and the unknown knowns, and unknown unknowns - every one laughed at him except the programmers!
I think it was him?
any way you have missed a wee point, the inputs are what concerns you and they only need to add up to 90.
here to help ( if wanted PM always available )
as for the code - you put the function in your module and the call where the testing wants to happen.