Results 1 to 9 of 9

Thread: Up Down problem

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Location
    Warren, MI
    Posts
    209

    Up Down problem

    Hey everyone,

    I'm having a problem with an updown thingy. When you click the up and down arrows it adds or subtracts from your total money to spend, but if you manually imput a number into the box it doesn't change the money. :-/

    Here's part of my code where "rs" is the total you have to spend:

    VB Code:
    1. Private Sub resourcesValue()
    2.         If purchase Then
    3.             spent = scal + sinf + sgen + sart + skni + sport
    4.             lblResources.Text = "Available Resources: " & rs - spent
    5.             If (rs - spent < 0) Then
    6.                 lblResources.ForeColor = Color.Red
    7.                 btnPurchase.Enabled = False
    8.             Else
    9.                 lblResources.ForeColor = Color.Black
    10.                 btnPurchase.Enabled = True
    11.             End If
    12.         End If
    13.     End Sub
    14.     Private Sub udCalvary_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles udCalvary.ValueChanged
    15.         scal = udCalvary.Value * 8
    16.         resourcesValue()
    17.     End Sub
    18.     Private Sub udKnight_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles udKnight.ValueChanged
    19.         skni = udKnight.Value * 11
    20.         resourcesValue()
    21.     End Sub
    22.     Private Sub udInfantry_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles udInfantry.ValueChanged
    23.         sinf = udInfantry.Value * 5
    24.         resourcesValue()
    25.     End Sub

    Is there any way I can make it read this value change too? Thanks,

    Mike

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,488

    Re: Up Down problem

    The ValueChanged event is not raised until the Text is successfully validated. When you type directly into the control that will not happen until you either click an arrow button, press an arrow key or shift focus to another control. I think pressing the Enter key will also force validation but I haven't tested that to confirm. This happens to prevent the event being raised after you type each digit of a multi-digit number, and also to prevent attempts to validate numbers that may be invalid because you haven't finished typing.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Location
    Warren, MI
    Posts
    209

    Re: Up Down problem

    Hey,

    Well, I guess I understand why it does that, but is there anything I can do to force it to validate. The form is setup so that after you enter in the numbers you must push a button to confirm your selection.

    Here's the button's code:

    VB Code:
    1. Private Sub btnPurchase_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPurchase.Click
    2.         btnPurchase.Enabled = False
    3.         purchase = 0
    4.         udCalvary.Enabled = False
    5.         udKnight.Enabled = False
    6.         udInfantry.Enabled = False
    7.         udArtillery.Enabled = False
    8.         udGeneral.Enabled = False
    9.         udPort.Enabled = False
    10.         rs -= spent
    11.     End Sub

    Is there anything that I can add to force it to validate all the up down things before subtracting the money?

    Thanks

    Mike

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,488

    Re: Up Down problem

    Like I said, when you enter a value manually the Text will be validated and the Value updated when the NUD loses focus. If you are clicking a Button then that validation will have occurred by the time the Button's Click event is raised.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Location
    Warren, MI
    Posts
    209

    Re: Up Down problem

    Well, maybe I'm doing something wrong but it does not validate when they push the button. The money goes away fine when using the up down arrows, but when the enter a number and then push the button (without pressing enter or an arrow) the money stays.

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,488

    Re: Up Down problem

    All I can tell you is what happens when I try it, and it works for me in 2003 and 2005. Are you sure you aren't entering a value that is not valid, like it's outside the min or max limits?

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Location
    Warren, MI
    Posts
    209

    Re: Up Down problem

    well, actually i did enter a value outside of the range. Is there a way to do something about that?

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,488

    Re: Up Down problem

    Quote Originally Posted by spelltwister
    well, actually i did enter a value outside of the range. Is there a way to do something about that?
    If you have limits set on your fields then your user should know about them, and you as the developer should certainly know about them. If you enter a value outside the range then when it is validated it will automatically be changed to either the maximum or minimum value, depending on what limit you've breached. Either you want those limits imposed or you don't. If you do then I don't see the problem. If you you don't then you need to change them. The Maximum and Minimum properties are type Single, so you are limited only by the MinValue and MaxValue of the Single type. The default range is 0 to 100. Have you just left those values unchanged?

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Location
    Warren, MI
    Posts
    209

    Re: Up Down problem

    I have limits set, but it does not return to the maximum when i push the button. Here's what happens.

    I have 80 money to spend. I enter into the udInfantry control the number 80, meaning I need 400 money, but when I push the button, it gives me the 16 infantry that I purchased that turn (IE, 80/5 is 16 infantry) and does not subtract anything from my money.

    I believe this is how you set limits:
    VB Code:
    1. udCalvary.Maximum = Math.Floor(rs / 8)
    2.         udKnight.Maximum = Math.Floor(rs / 11)
    3.         udInfantry.Maximum = Math.Floor(rs / 5)
    4.         udArtillery.Maximum = Math.Floor(rs / 10)
    5.         udGeneral.Maximum = 1
    6.         If Math.Floor(rs / 10) >= 1 Then
    7.             udPort.Maximum = 1
    8.         Else
    9.             udPort.Maximum = 0
    10.         End If

    Any idea?

    Mike

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width