在VB.net中寻找特定的标签属性

时间:2016-03-18 13:51:20

标签: vb.net

Image of the problem

Sub DrawGraph()
    'Used to draw the current state.
    G = Me.CreateGraphics
    'G.Clear(Color.White)    'Sets entire background to white
    G.clear(transparent)
    Dim placeholder As Integer = 0  'Used to store the current point being checked.
    If UsedLocations > 0 Then   'This part will only run if any points have been made
        For i = 0 To 19
            If Locations(i).Name <> "unused" Then   'only draws points that aren't unused.
                If Locations(i).StartPoint = True Then  'only draws light blue outline if the point is selected as the start.
                    'the -3 on the end is to correct positions.
                    G.FillEllipse(Brushes.LightBlue, Locations(i).Xcoord - 3, Locations(i).Ycoord - 3, 16, 16)
                End If
                If Locations(i).Selected = True Then    'only draws the light green outline if the point is currently selected.
                    G.FillEllipse(Brushes.LightGreen, Locations(i).Xcoord - 3, Locations(i).Ycoord - 3, 16, 16)
                End If
                G.FillEllipse(Brushes.Black, Locations(i).Xcoord, Locations(i).Ycoord, 10, 10)
            End If
        Next
        For i = 0 To UsedConnections - 1
            'draws connections
            If Connections(i).PartOfSolution = True Then
                G.DrawLine(Pens.Red, Locations(Connections(i).PointOne).Xcoord + 5, Locations(Connections(i).PointOne).Ycoord + 5, Locations(Connections(i).PointTwo).Xcoord + 5, Locations(Connections(i).PointTwo).Ycoord + 5)
            Else
                G.DrawLine(Pens.Black, Locations(Connections(i).PointOne).Xcoord + 5, Locations(Connections(i).PointOne).Ycoord + 5, Locations(Connections(i).PointTwo).Xcoord + 5, Locations(Connections(i).PointTwo).Ycoord + 5)
            End If

        Next
        'creating labels
        Controls.Clear()
        Dim NumberToMake As Integer = (39 + UsedConnections)
        Dim infolabels(NumberToMake) As Label
        For i = 0 To NumberToMake
            infolabels(i) = New Label

            infolabels(i).Height = 13
            infolabels(i).BackColor = Color.Red

            If i < 20 Then
                infolabels(i).Text = Locations(i).Name
                infolabels(i).Top = Locations(i).Ycoord - 15
                infolabels(i).Left = Locations(i).Xcoord
                If Locations(i).Name <> "unused" Then
                    Me.Controls.Add(infolabels(i))
                End If
            ElseIf i > 19 And i < 40 Then
                'dijkstra labels
            Else
                Console.WriteLine(i)
                Console.WriteLine(Connections(i - 40).Length)
                infolabels(i).Text = CStr(Connections(i - 40).Length)
                infolabels(i).Top = 0
                infolabels(i).Top = (Locations(Connections(i - 40).PointOne).Ycoord + Locations(Connections(i - 40).PointTwo).Ycoord) * 0.5
                'infolabels(i).Left = (Locations(Connections(i - 40).PointOne).Xcoord + Locations(Connections(i - 40).PointTwo).Xcoord) * 0.5
                Me.Controls.Add(infolabels(i))
            End If
            infolabels(i).Width = infolabels(i).Text.Length * 15
        Next
    End If

End Sub

因此,在尝试向表单添加标签时,要显示点数和点之上的信息。连接,我发现他们正在覆盖他们。我已经设定了宽度&amp;适当的高度,不改变任何东西。 我尝试将背景设置为红色以找到问题,但什么也没做。 在使用表单的背景颜色后,我发现标签上有一些白色部分添加到侧面(如上图所示),我无法找到任何方法来控制它,以便它不会掩盖绘制对象。

提前感谢您的帮助。

编辑:在调查了一点之后,似乎白色空间是标签通常在调整大小之前占用的空间。

1 个答案:

答案 0 :(得分:0)

我必须在向表单添加控件之前调整标签大小,如下所示:

If i < 20 Then
                infolabels(i).Text = Locations(i).Name
                infolabels(i).Top = Locations(i).Ycoord - 15
                infolabels(i).Left = Locations(i).Xcoord
                If Locations(i).Name <> "unused" Then
                    infolabels(i).Width = infolabels(i).Text.Length * 10
                    Me.Controls.Add(infolabels(i))
                End If
            ElseIf i > 19 And i < 40 Then
                'dijkstra labels
            Else
                Console.WriteLine(i)
                Console.WriteLine(Connections(i - 40).Length)
                infolabels(i).Text = CStr(Connections(i - 40).Length)
                'infolabels(i).Top = 0
                infolabels(i).Top = (Locations(Connections(i - 40).PointOne).Ycoord + Locations(Connections(i - 40).PointTwo).Ycoord) * 0.5
                infolabels(i).Left = (Locations(Connections(i - 40).PointOne).Xcoord + Locations(Connections(i - 40).PointTwo).Xcoord) * 0.5
                infolabels(i).Width = infolabels(i).Text.Length * 10
                Me.Controls.Add(infolabels(i))
            End If