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