Lanc1988 Posted February 16, 2004 Posted February 16, 2004 (edited) This should be a very basic question for almost everyone here, but since I dont know alot about vb.net yet, i dont know the answer. In a button's code I want it to add the value of what is in TextBoxes and display it in a label. I want it to add then divide the following by 4: AttackBox+DefenseBox+StrengthBox+HitsBox/4 Edited February 16, 2004 by Lanc1988 Quote
Lanc1988 Posted February 16, 2004 Author Posted February 16, 2004 NEVERMIND about that above part, i have figured it out, but now i need to know the code to have it round the number Quote
bungpeng Posted February 16, 2004 Posted February 16, 2004 Not sure it is what you want? label.text = (AttackBox.text + DefenseBox. text + StrengthBox.text + HitsBox.text) / 4 Quote
Lanc1988 Posted February 16, 2004 Author Posted February 16, 2004 Well I have figured out how to do the regular math stuff. Could you use the Math.Round funcation in an example for me? Quote
Lanc1988 Posted February 16, 2004 Author Posted February 16, 2004 I tried Label14.Text = Math.Round.Label16 But it says that round failed. How would I use the round function to round whats in Label16 and display it in Label14? Quote
sjn78 Posted February 16, 2004 Posted February 16, 2004 Just type round in help. There is plenty of information on rounding and examples. Quote
Lanc1988 Posted February 16, 2004 Author Posted February 16, 2004 I searched and searched and still cant get the error to go away, keeps on saying that Label14 = Math.Round(Label16.Text, 0) is wrong. Quote
sjn78 Posted February 16, 2004 Posted February 16, 2004 (edited) Try Label14 = Math.Round(cint(Label16.Text), 0) *Post Edit Label14 = cint(Label16.Text) If label16 is 45.3, then cint(45.3) would give 45 as integers are whole numbers anyway. Just tried it and CInt() rounds a decimal to a whole number so Math.Round is not needed. Edited February 16, 2004 by sjn78 Quote
Lanc1988 Posted February 16, 2004 Author Posted February 16, 2004 ok i will try that right now and post if it works for me. Quote
Lanc1988 Posted February 16, 2004 Author Posted February 16, 2004 (edited) this is crazy.. it says Value of type 'Integer' cannot be converted to 'System.Windows.Forms.Label'. it underlines CInt(Label16.Text) in blue and says that above error... any ideas? Edited February 16, 2004 by Lanc1988 Quote
sjn78 Posted February 16, 2004 Posted February 16, 2004 Put up the segment of code, not just the one line but the entire section where you are trying to populate the label Quote
Lanc1988 Posted February 16, 2004 Author Posted February 16, 2004 Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click On Error Resume Next 'Assign text box values to variables Attack = AttackBox.Text Defense = DefenseBox.Text Strength = StrengthBox.Text Hits = HitsBox.Text Ranged = RangedBox.Text Magic = MagicBox.Text Prayer = PrayerBox.Text 'Calculates Combat Level If (Ranged * 1.5) < (Attack + Strength) Then Label16.Text = (Attack + Defense + Strength + Hits) / 4 + (Magic + Prayer) / 8 End If 'Calculates Combat Level If (Ranged * 1.5) > (Attack + Strength) Then Label16.Text = (Defense + Hits) / 4 + (Magic + Prayer) / 8 + Ranged * 0.375 End If 'Calculates how many lvls needed to advance Label21.Text = Label16.Text / 4 / 8 Label22.Text = Label16.Text / 8 / 2 Label14 = CInt(Label16.Text) End Sub Quote
sjn78 Posted February 16, 2004 Posted February 16, 2004 An obvious mistake I found!!, But not sure if you just left it out in the post. You have: Label14 = Math.Round(Label16.Text, 0) Change to Label14.Text = Math.Round(Label16.Text, 0) Quote
Lanc1988 Posted February 16, 2004 Author Posted February 16, 2004 i feel pretty stupid.. lol, considering i almost always try adding ".text" when stuff doesnt work right.. now i just have one more little question.. is it possible to make it round .5 and lower down instead of rounding the .5 up? like if its 71.5 it would round it down to 70. thanks for your help Quote
sjn78 Posted February 16, 2004 Posted February 16, 2004 Label14.Text = Math.Round(CDbl(Label16.Text) - 0.01) May have to change the 0.01 depends on how many decimal places you end up with after your calcs. Quote
DR00ME Posted February 16, 2004 Posted February 16, 2004 Label14.Text = Math.Round(CDbl(Label16.Text) - 0.01) May have to change the 0.01 depends on how many decimal places you end up with after your calcs. Might need Cstr... im not sure...Label14.Text = Cstr(Math.Round(CDbl(Label16.Text) - 0.01)) Quote "Everything should be made as simple as possible, but not simpler." "It's not that I'm so smart , it's just that I stay with problems longer ." - Albert Einstein
sjn78 Posted February 16, 2004 Posted February 16, 2004 Only if Option Strict is On. But by the code that was posted, I would have to say its not on. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.