Before we look at updateBets we will add a facility to make changing the bet size and price more convenient. The NumericUpDown control is well-suited to this task.
From the Toolbox, place a NumericUpDown control onto the TestForm near the PlaceBet button we added in step 19. Name it nudPrice. Set these properties: TextAlign = Right, DecimalPlaces = 2, Minimum = 1.01, Maximum = 1000, Value = 10. Also add a label with Text = "Price".
Now place another NumericUpDown control near this. Name it nudSize. Set these properties: TextAlign = Right, DecimalPlaces = 2 (or whatever suits your currency unit), Maximum = your maximum bet size (say 20), Minimum = your minimum bet size (say 2), Value = your typical bet size (say 5). Also add a label with Text = "Size".
Also add another Button. Name it bUpdateBet and set its Text = "UpdateBet". We will us this in the next step.
Now drag two RadioButtons from the toolbox onto the form. For the first one, set Name = rbBack, Text = "Back", Checked = True. For the second one, set Name = rbLay, Text = "Lay".
Your “Bet UI” might now look something like this:
BetPanel.bmp
Run the project and test the action of the NumericUpDown controls. Notice that they both change in steps of 1.00 between the maximum and minimum limits we have set. This may be OK for the bet size value, but it would be more convenient if the price value changed according to the preferred Betfair price increments (shown here). To achieve this we include a handler for the nudPrice.ValueChanged event, shown in this code:
Code:
'Price value increments --------------------------------------------------------------
Private OddsLim() As Decimal = {2, 3, 4, 6, 10, 20, 30, 50, 100} 'Odd range limits
Private Sub nudPrice_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nudPrice.ValueChanged
Dim i As Integer, v As Decimal
Static Vprev As Decimal 'Previous value
With nudPrice
v = .Value
For i = 0 To UBound(OddsLim) 'Determine price range
If v <= OddsLim(i) Then Exit For
Next i
Select Case Vprev 'Adjustments required for smooth incrementing
Case 3
If v = 3.02 Then v = 3.025
Case 10
If v = 10.2 Then v = 10.25
Case 50
If v = 52 Then v = 52.5
End Select
.Value = Decimal.Round(v / OddsInc(i), MidpointRounding.AwayFromZero) * OddsInc(i) 'Round to the nearest valid price
.Increment = OddsInc(i) 'Set the increment
End With
Vprev = v
End Sub
Here the OddsLim array holds the price range limits while OddsInc holds the corresponding increments. This sub executes whenever the price value changes. The price range is determined and the control’s Increment property is set to the appropriate value. The price is also rounded to the nearest preferred value. The purpose of the Select Case statement is to iron out some unexpected transition behaviours which would otherwise occur.
Run the project to test this. When you spin the Price control up or down the value always remains on a preferred value. Also, if you enter an arbitrary price (e.g. 3.628), it will be rounded to the nearest preferred value (3.65) when you hit the enter key. Notice how quickly you can spin the control to any desired value.
No code is required for the Size control. You can change its Increment property to a more convenient value if you wish.
To link the two “spin” controls to the PlaceBet button, modify the test code of step 19 as follows:
Code:
Dim Market As Integer = YourMarketId 'The Id of your market
Dim Selection As Integer = YouSelectionId 'The Id of your selection
Dim BetType As BFUK.BetTypeEnum 'Your bet type
Dim BetPrice As Decimal 'Your desired odds
Dim BetSize As Decimal 'Your bet amount in currency units
Dim TheBetId As Long 'The betId (returned from placeBets)
Private Sub bPlaceBet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bPlaceBet.Click
Print("*** PlaceBet ***")
If rbBack.Checked Then BetType = BFUK.BetTypeEnum.B Else BetType = BFUK.BetTypeEnum.L 'Select Back or Lay bet
BetPrice = nudPrice.Value 'The selected price
BetSize = nudSize.Value 'The selected bet size
PlaceBet(Market, Selection, BetType, BetPrice, BetSize) 'The call to place the bet
End Sub
Here we have deleted the constant values for BetType, BetPrice and BetSize, and assigned the values of the new controls to these variables instead. We determine betType by testing the state of the Checked property of the rbBack radio button. Sub PlaceBet remains unchanged.
Now test the PlaceBet button as per step 19. We can now set the bet parameters far more conveniently using the controls we have added to the form.