I wrote a program that does rudimentary tile mapping based on reading in information from a "map file" and drawing the map on screen. I need it to be drawn in a scrollable window so it doesnt matter how big the grid is defined in the map file. I have looked into autoscroll but i dont see how to get it to work. Here is the code for the non scrollable window:

Public Class Form1
    'Defining the drawing variables, rectangles, bitmaps, etc.
    'For drawing both the map grid and the backgrounds/icons
    Private rect As Rectangle
    Private rect2 As Rectangle
    Private Draw As Graphics
    Private Draw2 As Graphics
    Private bitmap As Bitmap = New Bitmap(5, 5)
    'bitmap variables for each icon that can be displayed

    Public Structure CellID
        'Declare data members
        Public Loc1, Loc2, Visible, Terrain As Integer
        Public Context As String
    End Structure

    Dim tree As Bitmap = New Bitmap(filename:="C:\dell\tree.gif")
    Dim boulder As Bitmap = New Bitmap(filename:="C:\dell\boulder.gif")
    Dim bush As Bitmap = New Bitmap(filename:="C:\dell\bush.gif")
    Dim power As Bitmap = New Bitmap(filename:="C:\dell\power.gif")
    Dim treasure As Bitmap = New Bitmap(filename:="C:\dell\treasure.gif")
    Dim diamonds As Bitmap = New Bitmap(filename:="C:\dell\diamond.gif")
    Dim hatchet As Bitmap = New Bitmap(filename:="C:\dell\hatchet.gif")
    Dim axe As Bitmap = New Bitmap(filename:="C:\dell\axe.gif")
    Dim chainsaw As Bitmap = New Bitmap(filename:="C:\dell\chainsaw.gif")
    Dim chisel As Bitmap = New Bitmap(filename:="C:\dell\chisel.gif")
    Dim sledge As Bitmap = New Bitmap(filename:="C:\dell\sledge.gif")
    Dim jackhammer As Bitmap = New Bitmap(filename:="C:\dell\jackhammer.gif")
    Dim machete As Bitmap = New Bitmap(filename:="C:\dell\machete.gif")
    Dim binoculars As Bitmap = New Bitmap(filename:="C:\dell\binoculars.gif")
    Dim hero As Bitmap = New Bitmap(filename:="C:\dell\hero.gif")
    'Variable defined to read the file
    Dim fileReader As System.IO.StreamReader
    'Various variables for the data storage
    Dim HeroLocation, HeroLo(), MapName, WhiffelBalance, EnergyBalance, Garbage, EnergyBal, WhiffelBal, Temp As String
    Dim MapItems(100), Inventory(100), TempCellID(), numgridt As String
    Dim numgrid, i As Integer
    Dim Item1 As New ListViewItem

    'Event Handler for the "Load Map" Button Click
    Private Sub Map_Load_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Map_Load_Button.Click
        'Setup for the List View that displays the inventory
        InventoryView.View = View.Details
        InventoryView.Columns.Add("Tool", 76)
        'Read in the file and populate the text boxes
        fileReader = My.Computer.FileSystem.OpenTextFileReader(Map_File_Input.Text)
        MapName = fileReader.ReadLine()
        Map_Name_Text_Box.Text = MapName
        numgridt = fileReader.ReadLine()
        If IsNumeric(numgridt) = True Then
            numgrid = numgridt
            MessageBox.Show("MAP ERROR: No bounds defined")
        End If

        If (numgrid > 35) Then
            MessageBox.Show("MAP ERROR: The Map size is too large. The largest map size supported by this build is 35x35")
        End If

        Garbage = fileReader.ReadLine()
        If System.String.Compare(Garbage, "#########################") <> 0 Then
            MessageBox.Show("MAP ERROR: Deliminating Hash Mark error")
        End If
        HeroLocation = fileReader.ReadLine()
        EnergyBal = fileReader.ReadLine()
        WhiffelBal = fileReader.ReadLine()
        'Parse the hero location from the text file, store in Int variables

        HeroLo = HeroLocation.Split(",")
        If (HeroLo(0) > numgrid) Or (HeroLo(1) > numgrid) Then
            MessageBox.Show("MAP ERROR: The hero location is outside the bounds of the map")
        End If
        'Populate the balance of the energy and whiffle accounts
        If IsNumeric(EnergyBal) = False Then
            MessageBox.Show("MAP ERROR: The Energy Balance is Not Numeric!")
        End If
        Energy_Balance_Box.Text = EnergyBal
        If IsNumeric(WhiffelBal) = False Then
            MessageBox.Show("MAP ERROR: The Whiffel Balance is Not Numeric!")
        End If
        Whiffel_Balance_Box.Text = WhiffelBal
        'Read in the next line (Inventory)
        Inventory(i) = fileReader.ReadLine()
        'Store the first inventory item as a temp variable (for comparison in the loop)
        Temp = Inventory(i)
        'Increment i
        i = i + 1
        'variable as a new list item
        Dim mitem As New ListViewItem
        'set this item to the first inventory item
        mitem.Text = Temp
        'Add to the list view
        'Loop for reading the rest of the inventory
        Do While System.String.Compare(Temp, "#########################") <> 0
            Inventory(i) = fileReader.ReadLine()
            Temp = Inventory(i)
            If System.String.Compare(Temp, "#########################") <> 0 Then
                Dim litem As New ListViewItem
                litem.Text = Inventory(i)
                i += 1
            End If

        i = 0 'reset i to 0
        Temp = fileReader.ReadLine() ' Read the next line into temp
        MapItems(i) = Temp ' set the first item of MapItems to the Temp
        While System.String.Compare(Temp, "") > 0 'loop for reading the rest of the map items
            i += 1 'increment i
            MapItems(i) = fileReader.ReadLine() 'read in next line
            Temp = MapItems(i) 'reset temp
        End While
        'Lots of conditionals for the number of items that could be in the map file
        'Each conditional splits the items into 4 different strings, where they are deliminated by a comma in the map file

        Dim aryCellID(i) As CellID

        For x As Integer = 1 To i - 1
            TempCellID = MapItems(x).Split(",")
            aryCellID(x).Loc1 = TempCellID(0)
            aryCellID(x).Loc2 = TempCellID(1)
            aryCellID(x).Visible = TempCellID(2)
            aryCellID(x).Terrain = TempCellID(3)
            aryCellID(x).Context = TempCellID(4)
        Next x

        'Set window to maximize
        Me.WindowState = FormWindowState.Maximized
        'For loop to draw the map!
        For tilex = 1 To numgrid 'tile in the x direction the number of boxes in the grid
            For tiley = 1 To numgrid + 1 'tile in the y direction the number of boxes in the grid (+1 due to nested loop)
                rect = New Rectangle(tilex * 12, tiley * 12, 12, 12) 'define new rectangle
                Draw = Graphics.FromHwnd(Me.Handle)
                'Define a drawer
                'Draw the main grid of rectangles
                Draw.DrawRectangle(Pens.Ivory, rect)
                'Locate the hero and draw the hero icon
                If tilex = HeroLo(0) And tiley = (HeroLo(1) + 1) Then
                    Draw.DrawImage(image:=hero, rect:=rect)
                End If

                For index As Integer = 1 To i
                    If tilex = aryCellID(index).Loc1 And tiley = aryCellID(index).Loc2 + 1 And aryCellID(index).Visible = 1 Then
                        If System.String.Compare(aryCellID(index).Context, "None") = 0 Then
                            If aryCellID(index).Terrain = 0 Then
                                Draw.FillRectangle(Brushes.YellowGreen, rect)
                            End If
                            If aryCellID(index).Terrain = 1 Then
                                Draw.FillRectangle(Brushes.ForestGreen, rect)
                            End If
                            If aryCellID(index).Terrain = 2 Then
                                Draw.FillRectangle(Brushes.Blue, rect)
                            End If
                            If aryCellID(index).Terrain = 3 Then
                                Draw.FillRectangle(Brushes.Black, rect)
                            End If
                        End If
                        If System.String.Compare(aryCellID(index).Context, "Tree") = 0 Then
                            Draw.DrawImage(image:=tree, rect:=rect)
                        ElseIf System.String.Compare(aryCellID(index).Context, "Boulder") = 0 Then
                            Draw.DrawImage(image:=boulder, rect:=rect)
                        ElseIf System.String.Compare(aryCellID(index).Context, "Blackberry Bushes") = 0 Then
                            Draw.DrawImage(image:=bush, rect:=rect)
                        ElseIf System.String.Compare(aryCellID(index).Context, "Power Bar") = 0 Then
                            Draw.DrawImage(image:=power, rect:=rect)
                        ElseIf System.String.Compare(aryCellID(index).Context, "Treasure Chest") = 0 Then
                            Draw.DrawImage(image:=treasure, rect:=rect)
                        ElseIf System.String.Compare(aryCellID(index).Context, "Royal Diamonds") = 0 Then
                            Draw.DrawImage(image:=diamonds, rect:=rect)
                        ElseIf System.String.Compare(aryCellID(index).Context, "Hatchet") = 0 Then
                            Draw.DrawImage(image:=hatchet, rect:=rect)
                        ElseIf System.String.Compare(aryCellID(index).Context, "Axe") = 0 Then
                            Draw.DrawImage(image:=axe, rect:=rect)
                        ElseIf System.String.Compare(aryCellID(index).Context, "Chainsaw") = 0 Then
                            Draw.DrawImage(image:=chainsaw, rect:=rect)
                        ElseIf System.String.Compare(aryCellID(index).Context, "Chisel") = 0 Then
                            Draw.DrawImage(image:=chisel, rect:=rect)
                        ElseIf System.String.Compare(aryCellID(index).Context, "Sledge") = 0 Then
                            Draw.DrawImage(image:=sledge, rect:=rect)
                        ElseIf System.String.Compare(aryCellID(index).Context, "Jackhammer") = 0 Then
                            Draw.DrawImage(image:=jackhammer, rect:=rect)
                        ElseIf System.String.Compare(aryCellID(index).Context, "Machete") = 0 Then
                            Draw.DrawImage(image:=machete, rect:=rect)
                        ElseIf System.String.Compare(aryCellID(index).Context, "Binoculars") = 0 Then
                            Draw.DrawImage(image:=binoculars, rect:=rect)
                        ElseIf System.String.Compare(aryCellID(index).Context, "None") = 0 Then
                            MessageBox.Show("MAP ERROR: At least one of your cell items is undefined and will not be shown")
                        End If
                    End If
                Next index
    End Sub
    Private Sub Browse_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Browse_Button.Click
        Using OpenFile As New OpenFileDialog
            OpenFile.Filter = "Text Files (*.txt)|*.txt"
            OpenFile.Title = "Select File"
            If OpenFile.ShowDialog() = Windows.Forms.DialogResult.OK Then
                Map_File_Input.Text = OpenFile.FileName
            End If
        End Using
    End Sub
End Class

And here is a sample map file:

Sample Frupal Game Map
Pretty Rock
4,4,0,2,Blackberry Bushes
6 Years
Discussion Span
Last Post by Unhnd_Exception

Too much code to go through.

You should be able to add a panel to the form that is the maximum display size you want. Set the panel's autoscroll = true. Add a picturebox to the panel and set is location to 0,0 and set its borderstyle = none. When you need to load a map set the picturebox to the size the map should be. If its too big the panel's scollbars will kick in. You can do the drawing on the picturebox's graphics directly or create a bitmap and set it as the picturebox's image and do your drawing with the bitmap's graphics.


'with the picturebox's graphics
Dim Graphics as Graphics = PictureBox1.CreateGraphics
'or inside the picturebox's paint event with e.Graphics

'or with a bitmaps graphics
Dim BitmapGraphics as Graphics = Graphics.FromImage(PictureBox1.Image)

Let me know how it goes.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.