0

With the following global variables:

Dim SpilleBræt(8, 8) As PictureBox
Dim Position(8, 8) As String
Dim MarkeretFelt(8, 8) As String
Dim FeltFarve As String
Dim x As Integer
Dim y As Integer
Dim AktivMarkering As Boolean = 0
Dim SpilleBrik As String

And this code:

    Private Sub PictureBox_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim FeltValg As PictureBox = CType(sender, PictureBox)
    If AktivMarkering = 1 Then
        x = Mid(sender.name, sender.name.Length - 1, 1)
        y = Mid(sender.name, sender.name.Length, 1)

        Select Case Position(y, x)
            Case "LightTileMarked"
                Me.SpilleBræt(y, x).BackgroundImage = My.Resources.ResourceManager.GetObject(SpilleBrik & "LightTile")
            Case "DarkTileMarked"
                Me.SpilleBræt(y, x).BackgroundImage = My.Resources.ResourceManager.GetObject(SpilleBrik & "DarkTile")
            Case Else
                'fjerner markerede
                Select Case Position(y, x)
                    Case "BlackTower", "WhiteTower"
                        MsgBox("Tårn")

                    Case "BlackHorse", "WhiteHorse"
                        MsgBox("Hest")


                    Case "BlackBishop", "WhiteBishop"
                        MsgBox("Løber")

                    Case "WhiteKing", "BlackKing"
                        MsgBox("Konge")

                    Case "WhiteQueen", "BlackQueen"
                        MsgBox("Dronning")

                    Case "WhitePawn", "BlackPawn"
                        For k As Integer = y To 1 Step -1
                            If Position(k, x) = "" Then
                                If (k + x) Mod 2 = 1 Then
                                    FeltFarve = "DarkTile"
                                Else
                                    FeltFarve = "LightTile"
                                End If
                                Me.SpilleBræt(x, k).BackgroundImage = My.Resources.ResourceManager.GetObject(FeltFarve)
                            ElseIf Position(k, x) = "WhitePawn" Or Position(k, x) = "BlackPawn" Then

                                'background død brik

                            Else
                                k = 1
                            End If
                        Next
                End Select
        End Select

    Else
        'indsætter markering
        'x,y i picturebox'ens navn fx ->(SpilBrik44) hvor x=4 og y=4
        x = Mid(sender.name, sender.name.Length - 1, 1)
        y = Mid(sender.name, sender.name.Length, 1)

        Select Case Position(y, x)
            Case "BlackTower", "WhiteTower"
                MsgBox("Tårn")

            Case "BlackHorse", "WhiteHorse"
                MsgBox("Hest")


            Case "BlackBishop", "WhiteBishop"
                MsgBox("Løber")

            Case "WhiteKing", "BlackKing"
                MsgBox("Konge")

            Case "WhiteQueen", "BlackQueen"
                MsgBox("Dronning")

            Case "WhitePawn", "BlackPawn"
                For k As Integer = y To 1 Step -1
                    If Position(k, x) = "" Then
                        If (k + x) Mod 2 = 1 Then
                            FeltFarve = "DarkTileMarked"
                        Else
                            FeltFarve = "LightTileMarked"
                        End If
                        Me.SpilleBræt(x, k).BackgroundImage = My.Resources.ResourceManager.GetObject(FeltFarve)
                        MarkeretFelt(x, k) = FeltFarve
                        AktivMarkering = 1
                    ElseIf Position(k, x) = "WhitePawn" Or Position(k, x) = "BlackPawn" Then

                        'background død brik

                    Else
                        k = 1
                    End If
                Next
        End Select
    End If
End Sub

I have a problem with the first If statement aktivmarkering=1, goes directly to the 'Else' even though if statements is true. First time code is run, aktivmarkering is = 0, and therefore it obviously goes to 'Else', but after that one has ben run AktivMarkering is = 1, and first if should be executed. I don't see why not - anyone whos able to help? Thanks.

  • Even in the debugger, i could see it saying stement is true – Jonas Bernhøft Mar 10 '19 at 13:41
  • `AktivMarkering` is declared as `Boolean`, not `Integer`. – Jimi Mar 10 '19 at 14:24
  • 1
    A Boolean has a value of `True` or `False`, not `0` or `1`. If you use [`Option Strict On`](https://stackoverflow.com/a/29985039/1115360) then Visual Studio will show you where there are mismatches in variable types. – Andrew Morton Mar 10 '19 at 14:30
  • oh thats why. Thought it used 1 or 0 :D. Thanks alot – Jonas Bernhøft Mar 10 '19 at 14:52
  • 1
    Note that when getting an image from `My.Resources` a _new_ bitmap is created every time (even for those you have already gotten before), resulting in a lot of images that you never dispose of. Eventually your app will run out of memory. It's better to load all images once (for instance at startup) and if you need to access them by name you could store them in a [`Dictionary(Of String, Bitmap)`](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2). – Visual Vincent Mar 10 '19 at 15:03
  • Please turn on Option Strict. You can find this setting in Tools Menu -> Options -> Projects and Solutions -> VB Defaults. This will save you from bugs at runtime. – Mary Mar 10 '19 at 16:48
  • @Mary, just note that that will make `Option Strict On` the default for future projects, which is a good thing, but it won't affect the current project. It needs to be turned `On` in the project properties to affect the current project. – jmcilhinney Mar 10 '19 at 23:20
  • @jmcilhinney Thank you. I didn't know that. I will fix my canned response on this topic to include both. – Mary Mar 10 '19 at 23:58

0 Answers0