Adding Labels in Vba in 4.9.3

Jan 28, 2015 at 4:26 PM
Hello everybody,

I'm still working on Labels in MapWinGis.Ocx 4.9.3
I use the following code to add Labels - but nothing seems to happen (even the extents aren't changed) though the code did not produce an error:
Private Sub cmdAddLayers_Click()
    Dim objText As MapWinGIS.Shapefile
    Dim i As Integer
    Dim strLabel As String
    Dim dblX As Double, dblY As Double
   
    Set objText = New MapWinGIS.Shapefile
    If objText.Open(Application.CurrentProject.Path & "\texte.shp") = True Then
        objText.Labels.FontName = "Arial"
        objText.Labels.FontSize = 12
        ' Shape-Objekt durchlaufen
        For i = 0 To objText.NumShapes - 1
            ' Label-Text ermitteln
            strLabel = objText.CellValue(6, i)
            ' Label-Koordinaten ermitteln
            dblX = objText.Shape(i).Point(0).x
            dblY = objText.Shape(i).Point(0).y
            objText.Labels.AddLabel strLabel, dblX, dblY
        Next i
    End If
End Sub
If I add an "ordinary" shapelayer instead everything works fine. I can't see whats wrong.

Can anybody help?
Developer
Jan 29, 2015 at 3:59 PM
I would suggest to add some debugging code (with Debug.Writefor example). Right now the code doesn't produce any output if shapefile opening fails. Also there is no check that strLabel = objText.CellValue(6, i) actually returns some text (perhaps there is no 6th field).
Jan 29, 2015 at 6:04 PM
Hello sleschinski,

thank you for your quick answer. Yes I've added debugging code before.

The shepefile is opened, the for - next loop is performed, strLabel contains a string-value and the x- and y- coodinates contain double-values. So far I can see everything's fine exept that nothing seems to happen.

So it leaves me confused.
Developer
Jan 29, 2015 at 8:02 PM
Ok, how about Map.AddLayer(sf, true)? I don't see it in the code so I have to ask obvious before thinking of possible bugs.
Jan 29, 2015 at 9:17 PM
Hello sleschinski,

that's it - I'm so sorry, how stupid of me...

Thanls again
Feb 11, 2015 at 8:51 PM
Hello Dimp, I try with your code in Vb (VS2013 in W7x64) but I can't see the labels, can you give and idea?.

When I watch the variable objText.labels I see "Labels = {"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."}"

The code is:
Private Sub cmdAddLayers_Click()
        Dim objText As MapWinGIS.Shapefile
        Dim i As Integer
        Dim strLabel As String
        Dim dblX As Double, dblY As Double

        objText = New MapWinGIS.Shapefile

        If objText.Open("D:\Temp\Cascos Urbanos_region.shp") = True Then
            AxMap1.AddLayer(objText, True)

            With objText
                .Labels.FontName = "Arial"
                .Labels.FontSize = 12
                'Shape-Objekt durchlaufen
                For i = 0 To objText.NumShapes - 1
                    ' Label-Text ermitteln
                    strLabel = objText.CellValue(0, i)
                    ' Label-Koordinaten ermitteln
                    dblX = objText.Shape(i).Point(0).x
                    dblY = objText.Shape(i).Point(0).y
                    Try
                        objText.Labels.AddLabel(strLabel, dblX, dblY)
                    Catch ex As Exception
                        MsgBox(ex.ToString)
                    End Try

                Next i
            End With

        End If
    End Sub
The form display the shapefile but not the labels.