Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Controls created on one thread cannot be parented to a control on a different thread.

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

Scheduled Pinned Locked Moved Visual Basic
helpdatabasegraphics
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    charchabil03
    wrote on last edited by
    #1

    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 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.ArgumentException was caught Message="Controls created on one thread cannot be parented to a control on a different thread." Source="System.Windows.Forms" StackTrace: at System.Windows.Forms.Control.ControlCollection.Add(Control value) at System.Windows.Forms.Form.ControlCollection.Add(Control value) at RoundTrip.frmRoundTrip.DrowLabel(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

    T 1 Reply Last reply
    0
    • C charchabil03

      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 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.ArgumentException was caught Message="Controls created on one thread cannot be parented to a control on a different thread." Source="System.Windows.Forms" StackTrace: at System.Windows.Forms.Control.ControlCollection.Add(Control value) at System.Windows.Forms.Form.ControlCollection.Add(Control value) at RoundTrip.frmRoundTrip.DrowLabel(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

      T Offline
      T Offline
      TwoFaced
      wrote on last edited by
      #2

      Setting 'CheckForIllegalCrossThreadCalls' to false won't help you in this case. I tried it myself and I get the same error you do. You could do that way if you were changing the text of a control or something like that, but I don't think you should use that method in any circumstances. I don't know that for sure but I would guess not using that method is the recommended way to go. Here is a procedure you can call to safely add a control to the collection of another from any thread.

      Delegate Sub AddControlDelegate(ByVal parent As Control, ByVal child As Control)
      
      'Safely adds any control to the collection of another
      'parent: The control that is the container
      'child:  The control to add
      Private Sub AddControl(ByVal parent As Control, ByVal child As Control)
          If parent.InvokeRequired Then
              parent.Invoke(New AddControlDelegate(AddressOf AddControl), New Object() {parent, child})
          Else
              parent.Controls.Add(child)
          End If
      End Sub
      

      Now when you want to add your label you just use this procedure to safely do it from any thread you wish.

      AddControl(me,lbl(Lindex))

      For a more in depth tutorial on updating the UI from secondary threads you should read this article http://msdn.microsoft.com/msdnmag/issues/04/05/BasicInstincts/default.aspx[^] and/or search the web. For the moment though you just need to know my procedure works :) -- modified at 4:38 Saturday 17th February, 2007

      C 1 Reply Last reply
      0
      • T TwoFaced

        Setting 'CheckForIllegalCrossThreadCalls' to false won't help you in this case. I tried it myself and I get the same error you do. You could do that way if you were changing the text of a control or something like that, but I don't think you should use that method in any circumstances. I don't know that for sure but I would guess not using that method is the recommended way to go. Here is a procedure you can call to safely add a control to the collection of another from any thread.

        Delegate Sub AddControlDelegate(ByVal parent As Control, ByVal child As Control)
        
        'Safely adds any control to the collection of another
        'parent: The control that is the container
        'child:  The control to add
        Private Sub AddControl(ByVal parent As Control, ByVal child As Control)
            If parent.InvokeRequired Then
                parent.Invoke(New AddControlDelegate(AddressOf AddControl), New Object() {parent, child})
            Else
                parent.Controls.Add(child)
            End If
        End Sub
        

        Now when you want to add your label you just use this procedure to safely do it from any thread you wish.

        AddControl(me,lbl(Lindex))

        For a more in depth tutorial on updating the UI from secondary threads you should read this article http://msdn.microsoft.com/msdnmag/issues/04/05/BasicInstincts/default.aspx[^] and/or search the web. For the moment though you just need to know my procedure works :) -- modified at 4:38 Saturday 17th February, 2007

        C Offline
        C Offline
        charchabil03
        wrote on last edited by
        #3

        Thx/...

        Regards Ramy

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups