none
When I select the button to purchase upgrades for one tanks it works but for another it does nothing

    Question

  •     Private Sub Pay1_UPG_Click(sender As Object, e As EventArgs) Handles Pay1_UPG.Click
            If Loot_XP >= Top_Up_1.SelectedItem.item(1) Then
                Loot_XP = Loot_XP - Top_Up_1.SelectedItem(1)
                Zero_XP1.Text = Loot_XP
                MsgBox("Purchased")
    
                'Upgrade medium tanks from multicolumn combobox and then heavy tank will be available
                If Top_Up_1.Text = "Medium Artillery" Then
                    MsgBox("New tank available")
                    dtv2.Rows.Add(New Object() {"Medium Artillery", 28})
                    MsgBox("New upgrade available")
                    dtv3.Rows.Add(New Object() {"Heavy Artillery", 400})
    
                    If Top_Up_1.Text = "Medium Panzer" Then
                        MsgBox("New tank available")
                        dtv2.Rows.Add(New Object() {"Medium Panzer", 20})
                        MsgBox("New upgrade available")
                        dtv3.Rows.Add(New Object() {"Heavy Panzer", 400})
    
                                'Upgrade maxed tanks from multicolumn combobox and then heavy tank will be available
    
                                If Top_Up_1.Text = "Heavy Artillery" Then
                                    MsgBox("New tank available")
                                    dtv2.Rows.Add(New Object() {"Medium Artillery", 42})
                                    MsgBox("Maxed Out")
    
                                    If Top_Up_1.Text = "Heavy Panzer" Then
                                        MsgBox("New tank available")
                                        dtv2.Rows.Add(New Object() {"Heavy Panzer", 30})
                                        MsgBox("Maxed Out")
                                End If
                                    End If
                                End If
                            End If
                    End if 
        End Sub
    What I am trying to do here is as soon as the player makes a purchase for the medium artillery upgrade it unlocks the medium artillery in the tank combobox and the new upgrade for heavy becomes available. However, when I try to upgrade another tanks in this case the medium panzer upgrade, it takes the purchase but it doesnt show the rest of the msgboxes indicating a new upgrade is available and new tank is available.

    WRA

    Friday, April 14, 2017 3:11 PM

Answers

  • Maybe you should put End If differently reorganizing the If statements:

       If Top_Up_1.Text = "Medium Artillery" Then
          . . .
       End If
       If Top_Up_1.Text = "Medium Panzer" Then
          . . .
       End If
       If Top_Up_1.Text = "Heavy Artillery" Then
          . . .
       End If
       If Top_Up_1.Text = "Heavy Panzer" Then
          . . .
       End If

    It is also possible to use Else If. Or Select statement instead of If.


    • Edited by Viorel_MVP Friday, April 14, 2017 5:38 PM
    • Marked as answer by Waliur Rahman Friday, April 14, 2017 9:07 PM
    Friday, April 14, 2017 5:37 PM
  • So the combobox with the list of upgrades such as medium panzer, once that is purchased the name of the tank with the price of it appears into a combobox called tank otherwise when there is a lack of fund it still carries out this process.

    I assume that 'it still carries out this process' is NOT what you want it to do.  The code I provided makes that work correctly - it will not update the collections if the balance is insufficient.  However your code does not match your description - if there is sufficient balance in Loot_XP (I presume that is what you mean by 'purchased') then both 'tanks' (dtv2) and 'upgrades' (dtv3) are updated with additional items.

    • Marked as answer by Waliur Rahman Saturday, April 15, 2017 12:51 PM
    Saturday, April 15, 2017 12:18 PM

All replies

  • Maybe you should put End If differently reorganizing the If statements:

       If Top_Up_1.Text = "Medium Artillery" Then
          . . .
       End If
       If Top_Up_1.Text = "Medium Panzer" Then
          . . .
       End If
       If Top_Up_1.Text = "Heavy Artillery" Then
          . . .
       End If
       If Top_Up_1.Text = "Heavy Panzer" Then
          . . .
       End If

    It is also possible to use Else If. Or Select statement instead of If.


    • Edited by Viorel_MVP Friday, April 14, 2017 5:38 PM
    • Marked as answer by Waliur Rahman Friday, April 14, 2017 9:07 PM
    Friday, April 14, 2017 5:37 PM
  • I tried the end if in the order you have shown, it shows the same error but I haven't tried the elseif statement yet I was considering that but didn't think I will need it. 

    WRA

    Friday, April 14, 2017 8:29 PM
  • Thanks the ELSEIF was the answer to my question. 

    WRA

    Friday, April 14, 2017 9:07 PM
  • I was considering that but didn't think I will need it.

    Please look carefully at your code.  If Top_Up_1.Text is equal to "Medium Artillery" Then Top_Up_1.Text cannot be equal to "Medium Panzer". A single variable cannot be equal to two things at the same time.  You cannot nest your If statement like that.

    Friday, April 14, 2017 9:08 PM
  • Another quick question the same code above it shows that there should be an insufficient fund however it still carries out the transaction, do I need to put an else within the elseif statements?

    WRA

    Friday, April 14, 2017 9:13 PM
  • Another quick question the same code above it shows that there should be an insufficient fund however it still carries out the transaction, do I need to put an else within the elseif statements?

    You will need to show the code you are currently using.  Also explain what 'insufficient fund' and 'carries out the transaction' mean in terms of that code - which parts of the code should execute under what conditions.

    Friday, April 14, 2017 9:28 PM
  • Reasonable point Acamar here is the piece of code that is most reccomended for you to take a good look at. 

         Private Sub Player1_Game_Grid_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
    
    'This automatically loads the data within the given tables: Tanks and CostA
    
            dtv2.Columns.Add("Tanks", GetType(String))
            dtv2.Columns.Add("CostA", GetType(Integer))
    
            dtv2.Rows.Add(New Object() {"Light Artillery", 14})
            dtv2.Rows.Add(New Object() {"Light Panzer", 10})
            dtv2.Rows.Add(New Object() {"Light Tesla", 12})
            dtv2.Rows.Add(New Object() {"Light Scorcher", 13})
            Tank_1.DataSource = dtv2
            Tank_1.DisplayMember = "Tanks"
            Tank_1.ValueMember = "CostA"
    end sub 

    This is the example to load the following from my database into the mutlicolumn combobox.

     Private Sub Pay1_UPG_Click(sender As Object, e As EventArgs) Handles Pay1_UPG.Click
            If Loot_XP >= Top_Up_1.SelectedItem.item(1) Then
                Loot_XP = Loot_XP - Top_Up_1.SelectedItem(1)
                Zero_XP1.Text = Loot_XP
                MsgBox("Purchased")
            Else
                MsgBox("Insufficient funds")
            End If
    
            'Upgrade medium tanks from multicolumn combobox and then heavy tank will be available
            If Top_Up_1.Text = "Medium Artillery" Then
                MsgBox("New tank available")
                dtv2.Rows.Add(New Object() {"Medium Artillery", 28})
                MsgBox("New upgrade available")
                dtv3.Rows.Add(New Object() {"Heavy Artillery", 400})
    
            ElseIf Top_Up_1.Text = "Medium Panzer" Then
                MsgBox("New tank available")
                dtv2.Rows.Add(New Object() {"Medium Panzer", 20})
                MsgBox("New upgrade available")
                dtv3.Rows.Add(New Object() {"Heavy Panzer", 400})
    End if 

    Here is the example of the transaction taking place. If i exclude the else if statements it fully works but I wan it to operate with the else if statements. 


    WRA

    Saturday, April 15, 2017 11:10 AM
  • Reasonable point Acamar here is the piece of code that is most reccomended for you to take a good look at.

    I think that this is what you mean, but as you have provided no explanation. it is only a guess. You are already using ElseIf.

        'This is the example to load the following from my database into the mutlicolumn combobox.
        Private Sub Pay1_UPG_Click(sender As Object, e As EventArgs) Handles Pay1_UPG.Click
            If Loot_XP >= Top_Up_1.SelectedItem.item(1) Then
                Loot_XP = Loot_XP - Top_Up_1.SelectedItem.item(1)
                Zero_XP1.Text = Loot_XP
                MsgBox("Purchased")
                'Upgrade medium tanks from multicolumn combobox and then heavy tank will be available
                If Top_Up_1.Text = "Medium Artillery" Then
                    MsgBox("New tank available")
                    dtv2.Rows.Add(New Object() {"Medium Artillery", 28})
                    MsgBox("New upgrade available")
                    dtv3.Rows.Add(New Object() {"Heavy Artillery", 400})
                ElseIf Top_Up_1.Text = "Medium Panzer" Then
                    MsgBox("New tank available")
                    dtv2.Rows.Add(New Object() {"Medium Panzer", 20})
                    MsgBox("New upgrade available")
                    dtv3.Rows.Add(New Object() {"Heavy Panzer", 400})
                End If
            Else
                MsgBox("Insufficient funds")
            End If
        End Sub
    

    Saturday, April 15, 2017 11:30 AM
  • Ok what the top code does is as soon as we select any item it will subtract from the capacity (loot) stored so far by the selected item. but when any item is selected and there isnt enough loot then it shows insufficient funds but when the item in the upgrade cobobox appears it pops up a new item in another combobox but when it is insufficient funds it will still open it.

    Therefore are you suggesting the insufficient funds should be another else if statement or the else statement should be inbetween some of the text items "medium panzer" "medium artillery".

     

    WRA

    Saturday, April 15, 2017 11:42 AM
  • Ok what the top code does is as soon as we select any item it will subtract from the capacity (loot) stored so far by the selected item

    The code checks that Loot_XP ('loot'?) is large enough to have the cost of the selected item subtracted without going negative.  If it is large enough, the cost of the selected item is subtracted, a text box is updated with the balance in Loot_XP and the message "Purchased" is displayed.  If Loot_XP is not large enough the message 'insufficient funds" is displayed and the balance in Loot_XP is not changed.  I don't think that part is the problem.

    ...but when the item in the upgrade cobobox appears it pops up a new item in another combobox

    I have no idea what that means.  I can't see any code that could change the items appearing in a combobox until after you have tested the value of Top_Up_1.text.  So whatever else happens is going to be dependant on that value.

    ... but when it is insufficient funds it will still open it.

    Is that what it now does, or what you want it to do?

    Not sure what 'open it' means, but the code I posted checks the value of Top_Up_1.text only if there was sufficient balance in Loot_XP.  If there was, then it tests whether Top_Up_1.text is "Medium Artillery" or "Medium Panzer" and adds a tank and an upgrade to the respective collections, depending on which it is.

    Saturday, April 15, 2017 12:01 PM
  • When it selects insufficient item that is what it does exactly, sorry for lack of clarity. 

    So the combobox with the list of upgrades such as medium panzer, once that is purchased the name of the tank with the price of it appears into a combobox called tank otherwise when there is a lack of fund it still carries out this process. 


    WRA

    Saturday, April 15, 2017 12:09 PM
  • So the combobox with the list of upgrades such as medium panzer, once that is purchased the name of the tank with the price of it appears into a combobox called tank otherwise when there is a lack of fund it still carries out this process.

    I assume that 'it still carries out this process' is NOT what you want it to do.  The code I provided makes that work correctly - it will not update the collections if the balance is insufficient.  However your code does not match your description - if there is sufficient balance in Loot_XP (I presume that is what you mean by 'purchased') then both 'tanks' (dtv2) and 'upgrades' (dtv3) are updated with additional items.

    • Marked as answer by Waliur Rahman Saturday, April 15, 2017 12:51 PM
    Saturday, April 15, 2017 12:18 PM
  • Precisely Acamar I will try your code and report back asap.

    WRA

    Saturday, April 15, 2017 12:47 PM
  • Thanks Acamar that did the trick now it doesn't add the new tanks even with no loot in storage. Thanks Viorel too for helping out aswell you both are very helpful :)

    WRA

    Saturday, April 15, 2017 12:52 PM