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. WCF and WF
  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 WCF and WF
graphicshelpquestion
3 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

    L S 2 Replies 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

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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.

      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

        S Offline
        S Offline
        shahankur in
        wrote on last edited by
        #3

        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

        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