I'm developing a forum-based game and am trying to write up a simple program to help determine income bonuses. I've tried my hand at VB a number of times, but usually get frustrated and never try to take it seriously. Now I'm looking for some help, after no luck of searching the internet/help files.
My form has a NumericUpDown element, that goes up to 10 (I would like it so that only numbers 1-10 are available, and the user cannot go down to 0, but I don't know how to do that). This NumericUpDown element selects the "Acumen score", which is supposed to translate into the variable "a".
Variable "a" then undergoes a simple algorithm to produce the variable "b", which then appears in the element Label2. The problem I am running into here, is not being able to "refresh" the Label2 element. I've tried adding a Button1 element, and assigning onclick "Label2.Refresh()", but that doesn't seem to be correct as nothing happens.
Here's my code: (numAcumen is the name of the NumericUpDown element)
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim A As Double = numAcumen.Value
Dim B As Double = A * 1.5 / 3
Label2.Text = B
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Label2.Refresh()
End Sub
Private Sub numAcumen_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles numAcumen.ValueChanged
End Sub
Private Sub Button1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.GotFocus
End Sub
End Class
I have attached a screenshot of the form, if that helps any.
The goal of this simple program is for the user to select the "Acumen score" in the NumericUpDown element, have the algorithm run using the selected number, and then hit the button to display the result of the algorithm in the Label2 element.
I'm sure I'm missing a very small and simple step, but I'm a noob at VB and have tried looking a lot of places but with no luck.
First of all the NumericUpDown control has a maximum and minimum value in its properties which you would want to set to 10 and 1 respectively. Read the documentation on the control you want to use and you will usually find the answer.
You have all the code in the Form_Load event which means that it will only ever really run once - when the form loads. You don't need Refresh at all. You have a few options - you can move the calculation to the ValueChanged event of the numericupdown control (you will need to move your variable declarations to the form level rather than just in that routine) and then in the button click event you would put :
vb Code:
me.label2.text = b.tostring
or have your calculations and the above line all in the button click event which you would probably find easier.
Okay, a new problem that I have run into! I added some new elements and numbers to it. I uploaded the program in a .rar so you can run it and view it for yourself, just click hereto grab it.
Here's the new code:
Code:
1. Dim AcuLord As Double = lordacumen.Value
2. Dim LoyAdvisor As Double = advisorloyalty.Value
3. Dim AcuAdvisor As Double = advisoracumen.Value
4. Dim LAx As Double
5. Dim AAx As Double
6.
7. If LoyAdvisor = 0 Then LAx = 0
8. If LoyAdvisor >= 1 & LoyAdvisor <= 3 Then LAx = 0.16
9. If LoyAdvisor >= 4 & LoyAdvisor <= 7 Then LAx = 0.33
10. If LoyAdvisor >= 8 Then LAx = 0.5
11.
12. If AcuAdvisor = 0 Then AAx = 0
13. AAx = AcuAdvisor * LAx
14.
15. Dim B As Double = AcuLord * 1.5 / 3 + AAx
16.
17. Label2.Text = B
Me.Label2.Text = B.ToString
If you run the program, and you set the Advisor Loyalty to anything between 0 and 7, you will notice that it has no effect. Lines 8 and 9 don't seem to be functioning.
Also, if the Advisor Loyalty is set to 0, and the Advisor Acumen set to 1, after hitting the Generate Income button, the value becomes 0.83 rather than 0.5, what it should be. The program seems to ignore line 7, which declares that if the Advisor Influence is 0, then the LAx variable is 0, which would then make the AAx variable 0. If both of these variables are then 0, how does it come up with the 0.33 added to 0.5, making 0.83?
I don't download people's programs and anyway I can't access rar files.
Looking at your code one problem immediately jumps out -
vb Code:
If LoyAdvisor >= 1 & LoyAdvisor <= 3 Then LAx = 0.16
If LoyAdvisor >= 4 & LoyAdvisor <= 7 Then LAx = 0.33
You can't say that - & is used to join strings, not for use in a a conditional statement. You want to use one of the following in future: AND or ANDALSO or OR