Making editors for cricket games

Punk Sk8r! said:
Okay I havn't read the whole thread but say I or we wanted to make an editor which allows you to change kits while in-game (not in match jst in-game) would that be possible?

Yes. You'd need to use a global hotkey like Umpire Changer.
 
Wow, well Ive never tried Umpire Changer, so if you wouldnt mind answering my question ;)

So say I have Englands 2007 ODI Kit would I be able to press a hot key while in-game and then have it to switch to Englands 2005 ODI Kit, or any other England kit?

If so that would be great, would anyone make that?
 
Yes but it wouldn't take effect until the next match loaded up. You couldn't do in during a game but you could do it before the match started.
 
If you can wait then HBK is releasing a Kit Manager on his site soon :D:D:D:D ( Lol, thats what ge says , dont count on me )


Yes punk that is possible but as colin said it wouldnt take effect until next match is loaded.
 
Thats still pretty decent (seeing as teams dont change halfway through a match) as long as we can use any kit not just a fair selection I think that would be pretty good.
 
Ok, as promised, here's the calculator tutorial...

calc1.jpg


Create the form as shown. lstnumbers and lstoperations are to be set to visible: false in the properties window as is chkpoint. The buttons are all 30,25 in size. Name them all as follows:

calc2.jpg


The 5 labels containing the operations could more efficiently be done as one label but I did 5 and would have to change a load of other stuff if I made it one!
The labels show which operation is being used so give each of the 5 the correct symbol: +, -, x, /, = and then set them to the same location using the properties window.

Couple of other things to do before we start the coding:
- set txtAnswer to readonly and change the background colour to button highlight.
- Give btnAC a tab index of 0
- In from1 properties set KeyPreview to true

Double click on form1 to start coding...

Form1_load will have the following:

Code:
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txtAnswer.Text = "0"
        lbladd.Hide()
        lblsubtract.Hide()
        lbltimes.Hide()
        lbldivide.Hide()
        lblequals.Hide()
        chkpoint.Checked = False
    End Sub

This is what will be executed as the program loads up.
First we are setting the answer text box to 0, then we are hiding the operation labels. We are also setting chkpoint.checked to false to show that the decimal point has not yet been used.

Next all the click events for the buttons need to be written:

calc3.jpg


Code:
Private Sub btn7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn7.Click
        operationshowing()
        If txtAnswer.Text = "0" Then
            txtAnswer.Text = "7"
        Else
            txtAnswer.Text = txtAnswer.Text + "7"
        End If
        btnequals.Focus()
    End Sub

To get to the click event, double click on a button in design view.
The code is as follows:
operationshowing() - will look at later
If txtAnswer.Text = "0" Then
txtAnswer.Text = "7"
- If the answer box had 0 in it then change the 0 to a 7
Else
txtAnswer.Text = txtAnswer.Text + "7"
- Otherwise add a 7 onto the end of the number in the answer box.
btnequals.Focus() - shifts the focus back to the equals button so that equals will be executed if you press enter.

Repeat this for numbers 1-9.

The decimal point is a bit more complicated. As we said above we can only have one decimal point in a number so use this code:

Code:
Private Sub btnpoint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnpoint.Click
        operationshowing()
        If chkpoint.Checked = True Then
            Exit Sub
        Else
            chkpoint.Checked = True
        End If
        If txtAnswer.Text = "0" Then
            txtAnswer.Text = "0."
        Else
            txtAnswer.Text = txtAnswer.Text + "."
        End If
        btnequals.Focus()
    End Sub

This checks if there is a decimal point already by means of the hidden checkbox we added. If there is one then the program will exit the sub and not add another one. If there isn't it will continue on, add one, and then check the box to show it has been added.

The code for 0 is slightly different too:

Code:
Private Sub btn0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn0.Click
        operationshowing()
        If txtAnswer.Text = "0" Then
        Else
            txtAnswer.Text = txtAnswer.Text + "0"
        End If
        btnequals.Focus()
    End Sub

We don't want to add another 0 if there is one already so we only add one if the answer box does not say "0".

The operations

The four operations have this code in their click button event:

Code:
Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click
        lbldivide.Hide()
        lbladd.Show()
        lbltimes.Hide()
        lblsubtract.Hide()
        lblequals.Hide()
        chkpoint.Checked = False
    End Sub

This hides all the labels apart from their one and resets the decimal point check so another one can be added.

The listboxes

What the hidden list boxes are to do is to store all the numbers and operations used so the calculator will remember what to do when equals is pressed. It stores them using the the operationshowing() sub:

calc5.jpg


Code:
Private Sub operationshowing()
        If lbladd.Visible = True Or lblsubtract.Visible = True Or lbltimes.Visible = True Or lbldivide.Visible = True Or lblequals.Visible = True Then
            If lbladd.Visible = True Then lstoperations.Items.Add("+")
            If lblsubtract.Visible = True Then lstoperations.Items.Add("-")
            If lbltimes.Visible = True Then lstoperations.Items.Add("x")
            If lbldivide.Visible = True Then lstoperations.Items.Add("/")
            If lblequals.Visible = True Then
                txtAnswer.Text = "0"
            Else
                lstnumbers.Items.Add(txtAnswer.Text)
                txtAnswer.Text = "0"
            End If
            lbladd.Hide()
            lblsubtract.Hide()
            lbltimes.Hide()
            lbldivide.Hide()
            lblequals.Hide()
        End If
    End Sub

The sub is designed to work out whether or not a number or operation is to be stored.
The opening if statement says that the code will only execute if one of the operation labels is showing (these appear when an operation button is pressed). In other words if we are still adding numbers to the display and no operation button has been pressed the sub will not run and the number will be added on.
If 125, for example, has been entered and "/" has been pressed then the sub will add 125 to the lstnumbers box and "/" to the lstoperations box.
It will then clear the display back to 0 and hide all the operation labels.

Clear and All Clear

calc4.jpg


All clear will reset the figures by clearing the list boxes, hiding all the labels and setting the display to 0.

Clear will clear the display and hide the labels but will not clear the listboxes:

Code:
Private Sub btnclear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnclear.Click
        txtAnswer.Text = "0"
        lbladd.Hide()
        lblsubtract.Hide()
        lbltimes.Hide()
        lbldivide.Hide()
        lblequals.Hide()
        chkpoint.Checked = False
    End Sub

The Equals sub

This is the most complicated bit of coding in the program as it takes the content of the listboxes and works out the calculation it is to make.

Code:
 Private Sub btnequals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnequals.Click
        lstnumbers.Items.Add(txtAnswer.Text)
        Dim a As Integer = lstnumbers.Items.Count
        Dim x As Integer
        Dim tempanswer As Double
        For x = 0 To a - 1
            Dim num1 As Double = lstnumbers.Items(x)
            If x = 0 Then
                tempanswer = num1
            Else
                Dim operation As String = lstoperations.Items(x - 1)
                Select Case operation
                    Case "+"
                        tempanswer = tempanswer + num1
                    Case "-"
                        tempanswer = tempanswer - num1
                    Case "x"
                        tempanswer = tempanswer * num1
                    Case "/"
                        tempanswer = tempanswer / num1
                End Select
            End If
        Next
        lblequals.Show()
        txtAnswer.Text = tempanswer
        lstoperations.Items.Clear()
        lstnumbers.Items.Clear()
    End Sub

Firstly the last contents of the answer box is added to the numbers listbox. Then we have all the figures we need to make a calculation.

a is defined as the number of items in the numbers list box using items.count. This tells us how many sums we will have to do. tempanswer is defined as double. This allows for non-integer values such as 0.5 (essential in a calculator).

Code:
For x = 0 To a ? 1
Next

The for/next statement tells x to run for every item in the listbox. The index of the first item will be 0 and of the last item (a ? 1) with a being the total.

Code:
If x = 0 Then
tempanswer = num1

On the first time through we want only to find the first number so that each subsequent time we can add the next number onto it. When x is 1 we then find the first operation (index 0) by taking the index as (x-1)

The first number is then added/subtracted/divided/multiplied by the second number using a case select statement. tempanswer is the previous answer which will always be added/subtracted/etc from the next answer. N.B. This doesn?t take account of BODMAS

When all the numbers have been used the sub clears the list boxes and displays the answer.


Hotkeys

This program uses hotkeys for the numbers and operations to save the buttons from having to be clicked. These are set up as follows:

calc6.jpg


e.KeyChar will give the key pressed. Using the select case statement we can then filter out the keys we want to have an effect. In this case for the numbers, operations etc. When that key is pressed the program will go to the click event for that number/symbol.

Code:
 Private Sub frmMain_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
        Select Case e.KeyChar
            Case "0"
                btn0_Click(Nothing, Nothing)
            Case "1"
                btn1_Click(Nothing, Nothing)
            Case "2"
                btn2_Click(Nothing, Nothing)
            Case "3"
                btn3_Click(Nothing, Nothing)
            Case "4"
                btn4_Click(Nothing, Nothing)
            Case "5"
                btn5_Click(Nothing, Nothing)
            Case "6"
                btn6_Click(Nothing, Nothing)
            Case "7"
                btn7_Click(Nothing, Nothing)
            Case "8"
                btn8_Click(Nothing, Nothing)
            Case "9"
                btn9_Click(Nothing, Nothing)
            Case ""c
                btndel_Click(Nothing, Nothing)
            Case "+"
                btnadd_Click(Nothing, Nothing)
            Case "-"
                btnsubtract_Click(Nothing, Nothing)
            Case "x"
                btntimes_Click(Nothing, Nothing)
            Case "*"
                btntimes_Click(Nothing, Nothing)
            Case "/"
                btndivide_Click(Nothing, Nothing)
            Case "="
                btnequals_Click(Nothing, Nothing)
            Case "."
                btnpoint_Click(Nothing, Nothing)
            Case ""c
                btnAC_Click(Nothing, Nothing)
        End Select
    End Sub



Del and Escape are shown as the same character in the code. I ran the editor in debug mode and copied the value from runtime to get the exact symbol. Both work for their keys even though the Case looks the same:

calc7.jpg


Three other buttons

Delete button
Code:
 Private Sub btndel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndel.Click
        Dim a As Integer
        a = Len(txtAnswer.Text)
        If a = 1 Then
            txtAnswer.Text = "0"
            Exit Sub
        End If
        txtAnswer.Text = Strings.Left(txtAnswer.Text, a - 1)
        btnequals.Focus()
    End Sub

Deletes the last number typed unless there is only 1 number there in which case it changes it to 0. Strings.left truncates the last digit off.

+/- button/ Sq button

Both very simple and straight forward buttons for changing the sign and finding the square root.

calc8.jpg



And there you have it a working calculator with support for negative numbers, hotkeys and mutiple functions.

Extra steps:

This will add a Min and MR button, and do some formatting.

Min is memory in and will store a number until you want to retrieve it. If the display shows "0" clicking Min will clear the memory:

Code:
Private Sub btnMin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMin.Click
        lstmem.Items.Clear()
        If txtAnswer.Text <> 0 Then
            lstmem.Items.Add(txtAnswer.Text)
            lblmem.Show()
        Else
            lstmem.Items.Clear()
            lblmem.Hide()
            lblequals.Hide()
            btnequals.Focus()
            Exit Sub
        End If
        lblequals.Show()
        btnequals.Focus()
    End Sub

You'll notice that we've created another hidden listbox - lstmem. We've also created a label to tell us when something is in the memory. Set the label to visible; false.
MR (memory recall) then summons the number you have saved:

Code:
 Private Sub btnMR_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMR.Click
        operationshowing()
        txtAnswer.Text = lstmem.Items(0)
        lblequals.Show()
        btnequals.Focus()
    End Sub

Colour your buttons by using shift click for multiple select and then changing the fore colour. Icons can be edited through the form1 properties and the project; calculator properties option.

As another small extra you can add this to the start of the equals sub:

Code:
If a = 1 And lbltimes.Visible = True Then
            lblequals.Show()
            txtAnswer.Text = lstnumbers.Items(0) * lstnumbers.Items(0)
            lstoperations.Items.Clear()
            lstnumbers.Items.Clear()
            Exit Sub
        ElseIf a = 1 And lbldivide.Visible = True Then
            lblequals.Show()
            txtAnswer.Text = lstnumbers.Items(0) / lstnumbers.Items(0)
            lstoperations.Items.Clear()
            lstnumbers.Items.Clear()
            Exit Sub
        End If

It goes after Dim a As Integer = lstnumbers.Items.Count and tells then prog what to do if someone presses x and then equals without entering another number. x = will now square the number.

Your final project should look like this:

calc9.jpg


:)

Any questions in this thread please.
 

Attachments

  • calculator.zip
    24.2 KB · Views: 33
Can you please give me the source code of umpire changer.it was exellent.
thank you
 
I am thinking about making a tutorial of filestream, streamwriter, and streamreader manipulation, and maybe do a similar one like embi's text editor.

Anyone interested ?
 

Users who are viewing this thread

Top