Using VB2008 to acccess the Betfair API: A tutorial

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts

  • Mumbles0
    replied
    Graphics (continued)

    BigSprout,

    I think you are getting a bit confused by the graphics.
    Let’s look at some fundamentals...

    This code draws a rectangle on a form:

    Code:
    Private Sub bDraw_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bDraw.Click
    
      Dim Graph As Graphics = Me.CreateGraphics   'The graphics object
      Graph.DrawRectangle(Pens.Blue, 20, 10, 200, 300)  'Draw the rectangle
    
    End Sub
    Here Graph is a System.Drawing.Graphics object which incorporates all of the drawing methods we require. I like to think of this object as representing the drawing surface of the current form. We draw the desired rectangle using the DrawRectangle method.

    There you have it. But ......

    The drawing is very fragile. If you put something in front of it on the desktop, it will erase.

    To overcome this problem, graphics are typically drawn from within a form’s Paint event. This event fires whenever the form is required to be redrawn. If you put a Beep() statement in the Paint event handler and rearrange your Desktop a bit you will see what I mean.

    So alternatively we can draw the rectangle from within the Paint event like this:

    Code:
    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    
      e.Graphics.DrawRectangle(Pens.Blue, 20, 10, 200, 300)  'Draw the rectangle
    
    End Sub
    Now the rectangle persists. It is redrawn by the Paint event whenever the system deems that repainting is required. Note that here we do not have to create a Graphics object. One is given to us in the e.Graphics parameter.

    So that’s what the Paint event is for. Personally I find this quite dumb, but there is a better way using buffered graphics. We might have a look at this in a future step.

    Leave a comment:


  • BigSprout
    replied
    Sports,
    checking out how the treeview has been used in the tutorial, I assume you have done the same and you are using:

    Code:
       Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
    
    	My code is here
    
        End Sub
    if that is the case then every time you click on a node it will fire, double click causing double readings, the computer(Treeview) is doing what you are telling it to do.

    What you need to do is investigate using:

    Code:
       Private Sub TreeView1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TreeView1.Click
    
    	Code for single click events
    
        End Sub
    
    
    
        Private Sub TreeView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles TreeView1.DoubleClick
    
     	Code for double click events
    
       End Sub
    One problem is that the Treeview_click/doubleclick do not have the parameter "ByVal e As System.Windows.Forms.TreeViewEventArgs" and so you will not be able to use "e.node" without declaring "....TreeviewEventArgs"

    I didn't need a treeview, so did not investigate further on its workings, but when I have a problem I click on "Help" in the VS2010 and use a few key words for a search.

    Here is something that come up on "Treeview doubleclick" search:
    http://social.msdn.microsoft.com/for...2-5543A60467A3


    cheers Les

    Leave a comment:


  • Sports API + VB .Net
    replied
    Thanks for your reply bro. but the treeview is not working for me i am using service buk.getallmarkets and it displays main events correctly Cricket , Snooker , Horse Racing but when i double click on Cricket event it shows child markets in groups like Group C then other perticular market and sometime it double the same event and sometime more then 2 times double .. the treeview i want is as same as on betfair website how is it possible please help me out

    Leave a comment:


  • BigSprout
    replied
    Thanks for that Mumbles, your way is going to be much neater than how I resolved the problem (temporarily), I added this on Form2:

    Code:
       [COLOR="Gray"]Sub wb_AddRectangle([COLOR="Black"]ByVal e As System.Windows.Forms.PaintEventArgs[/COLOR], ByVal Lt As Integer, ByVal Tp As Integer, ByVal Wd As Integer, ByVal Ht As Integer)
    
            ...the code...
    
        End Sub[/COLOR]
    on the main Form did this:

    Code:
        [COLOR="Gray"]Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            [COLOR="Black"]Dim f As System.Windows.Forms.PaintEventArgs[/COLOR]
            FmWebPage.Show()
            FmWebPage.Left = Me.Left + Me.Width
            FmWebPage.WindowState = FormWindowState.Maximized
            FmWebPage.wb_AddRectangle([COLOR="Black"]f[/COLOR], 20, 20, 500, 400)
    
        End Sub[/COLOR]


    I got this warning, but the the rectangle displayed on FmWebPage okay:

    Warning 1 Variable 'f' is used before it has been assigned a value. A null reference exception could result at runtime...
    Away this weekend, so will have another play next week and report - this is a steep learning curve for me from Delphi-6 (which was outdated by the new millennia)

    cheers Les



    Edited:
    Just re-read your post:
    As you've got it, the sub will only draw a rectangle on Me - being the form on which the sub is located...
    the sub "AddRectangle" was on Form2 but would not display the rectangle on Form2 until I added "ByVal e As System.Windows.Forms.PaintEventArgs" as a parameter, this then added the "f" warning.
    Note. deleted using the "Paint" Sub method as it continually fired (which I didn't want happening
    Last edited by BigSprout; 25-02-2011, 12:57 AM. Reason: re-read answer

    Leave a comment:


  • Mumbles0
    replied
    BigSprout,

    Graphics is indeed an interesting topic.

    As you've got it, the sub will only draw a rectangle on Me - being the form on which the sub is located. If you want to draw a rectangle on another form then add an extra parameter to specify the target form:

    Code:
    [COLOR="Gray"]Sub AddRectangle([COLOR="Black"]ByVal TheForm As Form,[/COLOR] ByVal Lt As Integer, ByVal Tp As Integer, ByVal Wd As Integer, ByVal Ht As Integer)
        'Add rectangles to the Web Page Form for the Price/Volume charts
    
        Dim myBrush As New System.Drawing.SolidBrush(System.Drawing.Color.Red)
        Dim myPen As New System.Drawing.Pen(System.Drawing.Color.Black)
        Dim formGraphics As System.Drawing.Graphics
    
        formGraphics = [COLOR="Black"]TheForm[/COLOR].CreateGraphics()
        formGraphics.DrawRectangle(myPen, New Rectangle(Lt, Tp, Wd, Ht))
        formGraphics.FillRectangle(myBrush, New Rectangle(Lt, Tp, Wd, Ht))
        myPen.Dispose()
        myBrush.Dispose()
        formGraphics.Dispose()
    
      End Sub[/COLOR]
    You can then call it from any form like this:

    AddRectangle(Me, 20, 10, 200, 300)
    or perhaps:
    AddRectangle(Form2, 20, 10, 200, 300)

    Leave a comment:


  • BigSprout
    replied
    Persevered and came up with a solution:
    Code:
       Private Sub Form2_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
            AddRectangle(20, 10, 200, 300)
        End Sub
    ...now to get it all working


    cheers Les

    Leave a comment:


  • BigSprout
    replied
    Graphics drawing

    In keeping with this tutorial I have tried to extend it to include graphics and have come across a problem that I am unable to solve.

    I have dual monitors, on one monitor I have Form1 which displays the markets, fields, Betting Panel etc,
    on the 2nd monitor I have Form2 with 8 WebBrowsers showing the price/volume charts of selected runners.

    a. it is slow in filling all web browsers
    b. the charts are already delayed, so are of minimal use

    My next stage in the project is to collect the data and display it graphically on Form2

    I found this code and it works on Form1, but I can't seem to get it to work on form2 (only see a blank form2)
    Code:
       Sub AddRectangle(ByVal Lt As Integer, ByVal Tp As Integer, ByVal Wd As Integer, ByVal Ht As Integer)
            'Add rectangles to the Web Page Form for the Price/Volume charts
    
            
            Dim myBrush As New System.Drawing.SolidBrush(System.Drawing.Color.Red)
            Dim myPen As New System.Drawing.Pen(System.Drawing.Color.Black)
            Dim formGraphics As System.Drawing.Graphics
    
            formGraphics = Me.CreateGraphics()
            formgraphics.DrawRectangle(myPen, New Rectangle(Lt, Tp, Wd, Ht))
            formgraphics.FillRectangle(myBrush, New Rectangle(Lt, Tp, Wd, Ht))
            myPen.Dispose()
            myBrush.Dispose()
            formGraphics.Dispose()
    
        End Sub
    I have tried sending to form2 from Form1 and also activating the code on form2 to no success.

    I can write the code to display several rectangles and show the nice squiggly lines representing change in last matched prices once I can work out how to display graphics on a separate form.

    I would appreciate some help with this minor problem.

    cheers Les

    Leave a comment:


  • BigSprout
    replied
    Hi Sports,
    I am only interested in Aus horse racing and I have designed my API program with this in mind, I also enjoy betting and don't see the point in designing a bot to do all the enjoyable stuff for me.

    That said, to have the treeview populated on Form_Load, there is a major step that is needed to be carried out beforehand:

    "Login successfully"

    This requires your "Username" and "Password" stored either in a file or in "My.Settings" - storing this type of data is NOT RECOMMENDED, so populating a treeview in Form_Load is something I am not even going to attempt to explain.

    You could however, have your Login sub call the "Sub Call_PopulateTreview()" when the Login is successful

    When reading the tutorials, where you see:
    Print(.bestPricesToBack(0).price)

    replace with:
    dgvField("FdBk", .sortOrder).Value =.bestPricesToBack(0).price

    where dgvField is the name of a "DataGridView",
    and "FdBk" is the name of the column header

    a "for..next" loop could be used to read all 3 back/lay prices into different dgvField columns

    This post by Mumbles explains loading runners, just use the above code to display the odds:
    https://forum.bdp.betfair.com/showpo...&postcount=475

    hope this is of some use
    cheers Les

    Leave a comment:


  • Sports API + VB .Net
    replied
    hi bigsproute....
    brother i am reading this thread for last six months but i didnt get the idea that how can i make a bot which is look like betfair website like on form_load i populate treeview (provide by mumble) then display runners and three back and lay in grid and bet on appropreiate selection i am very confused if i didnt complete this task may be i fail in my life please if you can train me and send me some sort of help i will be very thankfull of yours

    Leave a comment:


  • Sports API + VB .Net
    replied
    Hi Mumble thanks for this great tuitorial can you point out me the page in which you have disscussed about how to display odds in grid (anygrid).......
    what i am trying to do is on form load i populate treeview then get market id then how to display runners and odds in grid to bet please reply me as sooon as possbile with full detail i will be very thank full of yours bye tc have a nice day

    Leave a comment:


  • Timmyag
    replied
    thank you
    missed that, your guide was a big help

    Leave a comment:


  • Mumbles0
    replied
    Timmyag,

    Looks like you've added a Service Reference instead of a Web Reference. This is a common trap. See this post.

    Leave a comment:


  • Timmyag
    replied
    Logging in

    Ok i got stuck at step 1, i've fixed it now, well maybe but either i'm doing something daft or the guide needs updating

    i' tried to follow the guide and got
    Code:
    Public Class testform
        Dim oHeaderGL As New BFGlobal.APIRequestHeader
        Dim BetfairGL As BFGlobal.BFGlobalService
    
        Private Sub BLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Blogin.Click
            Print("*** Login ***")
    
            Dim oLoginReq As New BFGlobal.LoginReq
            Dim oLoginResp As New BFGlobal.LoginResp
            With oLoginReq
                .username = "YourUsername"
                .password = "YourPassword"
                .productId = 82           'For free API
            End With
            'Dim oLoginIn As New BFGlobal.loginIn(oLoginReq)
            oLoginResp = BetfairGL.login(oLoginReq)     'Call the API
            With oLoginResp
                CheckHeader(.header)
                Print("ErrorCode = " & .errorCode.ToString)
            End With
        End Sub
    i'm using VS2010
    firstly the complier wont have
    Code:
    Dim BetfairGL As new BFGlobal.BFGlobalService
    because new cant by used on an interface

    so i skipped the new, and hey presto, suddenly the related call

    Code:
    oLoginResp = BetfairGL.login(oLoginReq)
    is a problem, its not expecting to be passed a BFGlobal.LoginReq it wants a BFGlobal.LoginIn.

    so i was looking in the object browser and BFGlobal.BFGlobalService doesn't have a new method / is a service/ sort of made sense why i couldn't do what i wanted. But BFGlobal.BFGlobalServiceClient did and looked like what i needed.

    so i swapped

    Dim BetfairGL As new BFGlobal.BFGlobalService
    for
    Dim BetfairGL As new BFGlobal.BFGlobalServiceClient

    and it seemed to work. Is this me doing something wrong or have they changed it since the first page was written?

    Thanks
    Tim

    Leave a comment:


  • Grantay.
    replied
    Humble thanks to Mumbles

    While we are doing backslaps and handshakes, I too must offer my most sincere thanks to Mumbles for this thread.

    Like bigsprout I have "hastened slowly" and built a solid app that does what I want and need.

    Not only is the information valuable, accurate and informative, Mumbles has shown infinite patience with a range of requests from users wanting a "quick answer" without going though the thread diligently, to the quite complex intricate detail of the often very confusing object structure that is the BF API. One huge advantage this tutorial provides is the confidence to launch in to the other functions and build on the overall template provided here.

    One series of Mumbles functions alone - PricetoInc and the reverse saved me hours of thinking and programming - it was a simple plug in!

    I come from an Access VBA environment so vb.net was a major leap foward; I hope one day to have the depth of understanding you have Mumbles that you have so generously provided here for all to devour and learn from.

    grantay (Canberra Australia)

    Leave a comment:


  • calypsored
    replied
    Thanks guys

    Mumbles, Grantay and Bigsprout - you can probably deduce that arrays frazzle my brain. I tried lots of different traps but not .length (associating that as I did with string length...).

    Thanks for your help.

    And I'd like to echo BigSprout's comments about the thread. I too now have an application doing exactly what I wanted it to do, and couldn't have done it without this thread. I look forward to fine tuning it now - and am sure I'll be back with other daft questions sooner or later!

    Leave a comment:

Working...
X