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
It would seem that the DataReceived() event is running in a different thread than the (main) thread that owns the MainPanel. You need to provide some communications vehicle between the two threads such that the addition of the new panel occurs in the correct thread.
-
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
Private Sub SetControls() // Method dt = objTest.List() // Call Function For I As Integer = 0 To dt.Rows.Count - 1 CtlName = dt.Rows(I).Item("ControlName") CtlText = IIf(IsDBNull(dt.Rows(I).Item("ControlText")), "", dt.Rows(I).Item("ControlText")) Dim XP As Long = dt.Rows(I).Item("XPosition") Dim YP As Long = dt.Rows(I).Item("YPosition") Dim XS As Long = dt.Rows(I).Item("XSize") Dim YS As Long = dt.Rows(I).Item("YSize") Dim Tab As Long = IIf(IsDBNull(dt.Rows(I).Item("TabOrder")), 200, dt.Rows(I).Item("TabOrder")) Dim tag As String = IIf(IsDBNull(dt.Rows(I).Item("Tag")), "", dt.Rows(I).Item("Tag")) If dt.Rows(I).Item("ControlType") = "LABEL" Then Dim btnLab As Label = Nothing btnLab = New Label() btnLab.Name = CtlName btnLab.TabIndex = Tab btnLab.Text = CtlText btnLab.Location = New System.Drawing.Point(XP, YP) btnLab.Size = New System.Drawing.Size(XS, YS) btnLab.TextAlign = ContentAlignment.MiddleRight pnlControl.Controls.Add(btnLab) End If If dt.Rows(I).Item("ControlType") = "TEXTBOX" Then Dim txtRun As TextBox = Nothing txtRun = New TextBox() txtRun.Name = CtlName txtRun.Tag = tag txtRun.TabIndex = Tab txtRun.Font = New Font(Font, FontStyle.Regular) txtRun.Location = New System.Drawing.Point(XP, YP) txtRun.Size = New System.Drawing.Size(XS, YS) txtRun.TextAlign = HorizontalAlignment.Left pnlControl.Controls.Add(txtRun) End If If dt.Rows(I).Item("ControlType") = "COMBOBOX" Then Dim Cmbbox As ComboBox = Nothing Cmbbox = New ComboBox() Cmbbox.Name = CtlName Cmbbox.Tag = tag Cmbbox.TabIndex = Tab Cmbbox.Font = New Font(Font, FontStyle.Regular) Cmbbox.Location = New System.Drawing.Point(XP, YP) Cmbbox.Size = New System.Drawing.Size(XS, YS) pnlControl.Controls.Add(Cmbbox) Dim ObjCity As New CityBL ObjCity.CityDDL(Cmbbox) End If Next End Sub