Controls created on one thread cannot be parented to a control on a different thread

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • CharChabil
    New Member
    • Oct 2006
    • 17

    Controls created on one thread cannot be parented to a control on a different thread

    Hello Guys,
    I am trying to mark locations on a map (x,y pixels are derived from a DB)
    This is the code that i am using

    Code:
    Private Sub DisplayOnSoftMap()
    SqlQuey = "Select * FROM SoftwareMap WHERE SoftwareMap.SMSID =myRndTrip.CurrentSwitch.SID & "'"
    tmpSoftMapCol = tmpSoftMapCol.GetAllSoftwareMap(SqlQuey)
    For Each tmpSoftMap In tmpSoftMapCol
    
    DrowLabel(tmpSoftMap.XMap, tmpSoftMap.YMap, tmpSoftMap.LablName)
    
    next
    
    Private Sub DrowLabel(ByVal X As Long, ByVal Y As Long, ByVal lblText As String)
    
    Dim LIndex, Lcount As Integer
    Try
    If Lbl Is Nothing Then
    ReDim Preserve Lbl(0)
    Lcount = 1
    LIndex = 0
    Else
    Lcount = Lbl.Length
    ReDim Preserve Lbl(Lcount)
    LIndex = Lcount
    End If
    Lbl(LIndex) = New LinkLabel
    Control.CheckForIllegalCrossThreadCalls = False
    Lbl(LIndex).Location = New System.Drawing.Point(X, Y)
    Lbl(LIndex).Text = lblText
    
    Lbl(LIndex).Visible = True
    Lbl(LIndex).AutoSize = True
    Control.CheckForIllegalCrossThreadCalls = False
    Me.Controls.Add(Lbl(LIndex))
    AddHandler Lbl(LIndex).Click, AddressOf Label_Click
    Catch ex As Exception
    Throw ex
    End Try
    End Sub
    And this is the Error that Occurs

    System.Argument Exception was caught
    Message="Contro ls created on one thread cannot be parented to a control on a different thread."
    Source="System. Windows.Forms"
    StackTrace:
    at System.Windows. Forms.Control.C ontrolCollectio n.Add(Control value)
    at System.Windows. Forms.Form.Cont rolCollection.A dd(Control value)
    at RoundTrip.frmRo undTrip.DrowLab el(Int64 X, Int64 Y, String lblText)

    --

    The purpose of this code is to mark locations on a map,
    I would be glad: if you help me finding the solution or any other solution which fits my needs

    Regards
    Ramy
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    Here are a couple of threads that may help:
    Threading question "Controls created on one thread cannot be parented to a control on a different thread."
    Adding controls to the form... help

    Comment

    Working...