locked
deleting controls RRS feed

  • Question

  • I'm adding dinamically labels in a predetermined poisition, related to some chart graph.

    Imports System.Windows.Forms.DataVisualization.Charting
    Public Class Form1
        Dim x, y As Single
        Dim N, inizio_unit, fine_unit, interv_unit As Integer
        Dim posiz As PointF
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Me.Controls.Remove(num_unit(1))
        End Sub
    
        Dim num_unit(256) As Control
        Private Sub NumericUpDown1_ValueChanged(sender As Object, e As EventArgs) Handles NumericUpDown1.ValueChanged
            N = NumericUpDown1.Value
            Chart1.Invalidate()
        End Sub
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Chart1.ChartAreas(0).AxisX.Maximum = 1000
            Chart1.ChartAreas(0).AxisX.Minimum = -1000
            Chart1.ChartAreas(0).AxisY.Maximum = 1000
            Chart1.ChartAreas(0).AxisY.Minimum = 0
        End Sub
        Private Sub Chart1_PostPaint(sender As Object, e As ChartPaintEventArgs) Handles Chart1.PostPaint
    
            x = CSng(e.ChartGraphics.GetPositionFromAxis(Chart1.ChartAreas(0).Name, AxisName.X, Chart1.ChartAreas(0).AxisX.Minimum))
            y = CSng(e.ChartGraphics.GetPositionFromAxis(Chart1.ChartAreas(0).Name, AxisName.Y, 0))
            posiz = New PointF(x, y)
            posiz = e.ChartGraphics.GetAbsolutePoint(posiz)
            inizio_unit = posiz.X
    
            x = CSng(e.ChartGraphics.GetPositionFromAxis(Chart1.ChartAreas(0).Name, AxisName.X, Chart1.ChartAreas(0).AxisX.Maximum))
            y = CSng(e.ChartGraphics.GetPositionFromAxis(Chart1.ChartAreas(0).Name, AxisName.Y, 0))
            posiz = New PointF(x, y)
            posiz = e.ChartGraphics.GetAbsolutePoint(posiz)
            fine_unit = posiz.X
    
            interv_unit = (fine_unit - inizio_unit) / (N + 1)
    
            For i = 1 To N
    
                Dim a As New Label
                num_unit(i) = a
                num_unit(i).Text = i
                num_unit(i).Top = 10
                num_unit(i).Left = Chart1.Left + i * interv_unit
                num_unit(i).ForeColor = Color.Black
                num_unit(i).Visible = True
                num_unit(i).Font = New Font("Arial", 7, FontStyle.Bold)
                num_unit(i).Size = New Size(20, 20)
    
                Me.Controls.Add(num_unit(i))
            Next i
        End Sub
    End Class

    When i click on numeric control, labels are correctely added, but the old ones should be deleted.

    I tryed with the button command: 

    Me.Controls.Remove(num_unit(1))

    for example, but nthing happens.

    Thanks, Enzo


    Friday, August 14, 2020 1:04 PM

Answers

  • Hy, i did this way, it works:

    Imports System.Windows.Forms.DataVisualization.Charting
    Public Class Form1
        Dim x, y As Single
        Dim N, inizio_unit, fine_unit, interv_unit As Integer
        Dim posiz As PointF
    
        Dim num_unit(256) As Control
        Private Sub NumericUpDown1_ValueChanged(sender As Object, e As EventArgs) Handles NumericUpDown1.ValueChanged
            N = NumericUpDown1.Value
            Chart1.Invalidate()
            interv_unit = (fine_unit - inizio_unit) / (N + 1)
            For i = 1 To N
                Me.Controls.Remove(num_unit(i))
            Next i
    
            For i = 1 To N
                num_unit(i) = New Label
                num_unit(i).Text = i
                num_unit(i).Top = 10
                num_unit(i).Left = Chart1.Left + i * interv_unit
                num_unit(i).ForeColor = Color.Black
                num_unit(i).Visible = True
                num_unit(i).Font = New Font("Arial", 7, FontStyle.Bold)
                num_unit(i).Size = New Size(20, 20)
    
                Me.Controls.Add(num_unit(i))
            Next i
    
        End Sub
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Chart1.ChartAreas(0).AxisX.Maximum = 1000
            Chart1.ChartAreas(0).AxisX.Minimum = -1000
            Chart1.ChartAreas(0).AxisY.Maximum = 1000
            Chart1.ChartAreas(0).AxisY.Minimum = 0
        End Sub
        Private Sub Chart1_PostPaint(sender As Object, e As ChartPaintEventArgs) Handles Chart1.PostPaint
    
            x = CSng(e.ChartGraphics.GetPositionFromAxis(Chart1.ChartAreas(0).Name, AxisName.X, Chart1.ChartAreas(0).AxisX.Minimum))
            y = CSng(e.ChartGraphics.GetPositionFromAxis(Chart1.ChartAreas(0).Name, AxisName.Y, 0))
            posiz = New PointF(x, y)
            posiz = e.ChartGraphics.GetAbsolutePoint(posiz)
            inizio_unit = posiz.X
    
            x = CSng(e.ChartGraphics.GetPositionFromAxis(Chart1.ChartAreas(0).Name, AxisName.X, Chart1.ChartAreas(0).AxisX.Maximum))
            y = CSng(e.ChartGraphics.GetPositionFromAxis(Chart1.ChartAreas(0).Name, AxisName.Y, 0))
            posiz = New PointF(x, y)
            posiz = e.ChartGraphics.GetAbsolutePoint(posiz)
            fine_unit = posiz.X
        End Sub
    
    End Class


    Tuesday, August 18, 2020 1:16 PM

All replies

  • This mocked up works just fine

    Private num_unit(3) As Control
    Private Sub Load()
        num_unit(0) = New Label() With {.Top = 20, .Left = 20, .Text = "First"}
        num_unit(1) = New Label() With {.Top = 40, .Left = 20, .Text = "Second"}
    
        Controls.Add(num_unit(0))
        Controls.Add(num_unit(1))
    End Sub
    
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Load()
    End Sub
    
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Controls.Remove(num_unit(0))
    End Sub

    Suppose you wanted to remove a control (the first) from the form and the array.

    Public Module Extensions
        <Runtime.CompilerServices.Extension()>
        Public Sub RemoveAt(Of T)(ByRef sender() As T, ByVal index As Integer)
            Array.Copy(
                sender,
                index + 1,
                sender,
                index,
                UBound(sender) - index)
    
            ReDim Preserve sender(UBound(sender) - 1)
    
        End Sub
    End Module
    

    Then 

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If num_unit.Length > 0 Then
            Controls.Remove(num_unit(0))
            num_unit.RemoveAt(0)
        End If
    End Sub


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, August 14, 2020 2:24 PM
  • Hy, i did this way, it works:

    Imports System.Windows.Forms.DataVisualization.Charting
    Public Class Form1
        Dim x, y As Single
        Dim N, inizio_unit, fine_unit, interv_unit As Integer
        Dim posiz As PointF
    
        Dim num_unit(256) As Control
        Private Sub NumericUpDown1_ValueChanged(sender As Object, e As EventArgs) Handles NumericUpDown1.ValueChanged
            N = NumericUpDown1.Value
            Chart1.Invalidate()
            interv_unit = (fine_unit - inizio_unit) / (N + 1)
            For i = 1 To N
                Me.Controls.Remove(num_unit(i))
            Next i
    
            For i = 1 To N
                num_unit(i) = New Label
                num_unit(i).Text = i
                num_unit(i).Top = 10
                num_unit(i).Left = Chart1.Left + i * interv_unit
                num_unit(i).ForeColor = Color.Black
                num_unit(i).Visible = True
                num_unit(i).Font = New Font("Arial", 7, FontStyle.Bold)
                num_unit(i).Size = New Size(20, 20)
    
                Me.Controls.Add(num_unit(i))
            Next i
    
        End Sub
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Chart1.ChartAreas(0).AxisX.Maximum = 1000
            Chart1.ChartAreas(0).AxisX.Minimum = -1000
            Chart1.ChartAreas(0).AxisY.Maximum = 1000
            Chart1.ChartAreas(0).AxisY.Minimum = 0
        End Sub
        Private Sub Chart1_PostPaint(sender As Object, e As ChartPaintEventArgs) Handles Chart1.PostPaint
    
            x = CSng(e.ChartGraphics.GetPositionFromAxis(Chart1.ChartAreas(0).Name, AxisName.X, Chart1.ChartAreas(0).AxisX.Minimum))
            y = CSng(e.ChartGraphics.GetPositionFromAxis(Chart1.ChartAreas(0).Name, AxisName.Y, 0))
            posiz = New PointF(x, y)
            posiz = e.ChartGraphics.GetAbsolutePoint(posiz)
            inizio_unit = posiz.X
    
            x = CSng(e.ChartGraphics.GetPositionFromAxis(Chart1.ChartAreas(0).Name, AxisName.X, Chart1.ChartAreas(0).AxisX.Maximum))
            y = CSng(e.ChartGraphics.GetPositionFromAxis(Chart1.ChartAreas(0).Name, AxisName.Y, 0))
            posiz = New PointF(x, y)
            posiz = e.ChartGraphics.GetAbsolutePoint(posiz)
            fine_unit = posiz.X
        End Sub
    
    End Class


    Tuesday, August 18, 2020 1:16 PM