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. Adding controls to Existing Panel at run time in windows application

Adding controls to Existing Panel at run time in windows application

Scheduled Pinned Locked Moved Visual Basic
graphicshelpquestion
4 Posts 3 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.
  • K Offline
    K Offline
    kjsl2k9
    wrote on last edited by
    #1

    Hi, I want to add new control at run time . I had written the code to add a new control to the Main Panel in the Data recieved event of the Serial port like this : - Public Sub portname_DataRecieved() Dim pnl as new Panel() Dim pts As New System.Drawing.Point(pt.X, pt.Y) pnl.Location = pts pnl.BackColor = System.Drawing.Color.YellowGreen MainPanel.Controls.Add(pnl) End Sub But I am getting the error at the line MainPanel.Controls.Add(pnl) that is cross-thread operation is not valid how can I remove it. Thanks

    M 1 Reply Last reply
    0
    • K kjsl2k9

      Hi, I want to add new control at run time . I had written the code to add a new control to the Main Panel in the Data recieved event of the Serial port like this : - Public Sub portname_DataRecieved() Dim pnl as new Panel() Dim pts As New System.Drawing.Point(pt.X, pt.Y) pnl.Location = pts pnl.BackColor = System.Drawing.Color.YellowGreen MainPanel.Controls.Add(pnl) End Sub But I am getting the error at the line MainPanel.Controls.Add(pnl) that is cross-thread operation is not valid how can I remove it. Thanks

      M Offline
      M Offline
      Muhammad Mazhar
      wrote on last edited by
      #2

      Use current forms Invoke method to call MainPanel.Controls.Add method, hopefully it will resolve the issue.

      Share your experience with others Check my Blog...

      K 1 Reply Last reply
      0
      • M Muhammad Mazhar

        Use current forms Invoke method to call MainPanel.Controls.Add method, hopefully it will resolve the issue.

        Share your experience with others Check my Blog...

        K Offline
        K Offline
        kjsl2k9
        wrote on last edited by
        #3

        I had tried the following code : - If MainPanel.InvokeRequired Then MainPanel.Invoke(AddControlToConatiner) End if Public Function AddControlToConatiner() pnl.Location = pts pnl.BackColor = System.Drawing.Color.YellowGreen MainPanel.Controls.Add(pnl) Return 0 End Function but the problem still remains the same what is the error can u tell or an example

        D 1 Reply Last reply
        0
        • K kjsl2k9

          I had tried the following code : - If MainPanel.InvokeRequired Then MainPanel.Invoke(AddControlToConatiner) End if Public Function AddControlToConatiner() pnl.Location = pts pnl.BackColor = System.Drawing.Color.YellowGreen MainPanel.Controls.Add(pnl) Return 0 End Function but the problem still remains the same what is the error can u tell or an example

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          You're creating a control on a background thread. Don't. Have the AddControlToContainer method create the control. All controls should be created on the UI thread, not on a background thread.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007, 2008
          But no longer in 2009...

          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