Results 1 to 22 of 22

Thread: Hmm dynamic form problem *RESOLVED*

  1. #1

    Thread Starter
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913

    Hmm dynamic form problem *RESOLVED*

    ok. I have a tabcontrol with a couple dynamiclly created tabs with dynamiclly added controls on them. I am writing a Tear function the when you write click a tab header, you get a popup menu with the oiption to Tear. When selected a new form appears that becomes a torn out version of the ontrol that were on the tab( Get what I mean?). First time its fine on a control with 1 control. But do it again and 2 windows appear. again and 3 windows appear.

    But that is not all. With more than 1 control, it ceates a new form for each one.

    Oh and when I place the ocntrol form the tab onto the new form, they disappear from the tab. Ok, so I will make the tab invisible until layet. But no, you cant change the visiblity ot tabs either!

    Argh.

    Anyone got a clue. Here is the relevant Tear code

    VB Code:
    1. Private Sub tabClient_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles tabClient.MouseDown
    2.  
    3.         Dim tab As New TabPage()
    4.         Dim x As Integer
    5.         Dim rect As Rectangle
    6.         Dim ctl As Control
    7.         Dim holdctl As Control
    8.  
    9.         pos.X = e.X
    10.         pos.Y = e.Y
    11.         If e.Button = MouseButtons.Right Then ContextMenu1.Show(tabClient, pos)
    12.         AddHandler MenuItem1.Click, AddressOf Tear
    13.     End Sub
    14.  
    15.     Private Sub Tear(ByVal sender As Object, ByVal e As EventArgs)
    16.  
    17.         Dim tab As New TabPage()
    18.         Dim x As Integer
    19.         Dim rect As Rectangle
    20.         Dim ctl As Control
    21.  
    22.         For x = 0 To tabClient.TabCount - 1
    23.             rect = tabClient.GetTabRect(x)
    24.             If rect.Contains(pos) Then
    25.                 Dim test As New Form()
    26.                 AddHandler test.Closing, AddressOf FormClose
    27.                 For Each ctl In tabClient.TabPages(x).Controls
    28.                     test.Controls.Add(ctl)
    29.                 Next
    30.                 test.Show()
    31.                 Exit For
    32.             End If
    33.         Next
    34.     End Sub
    Last edited by Cander; Jun 3rd, 2003 at 03:26 PM.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  2. #2
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Cander asking questions instead of giving answers.... what universe did I wake up in this morning?

    I think I understand what you're saying....

    Basically you're problem is you have to keep a check to see if an existing child form is present, because if you don't, you have a new TearPage appear on each click.... so everytime the user clicks this certain context Tear option... he/she will keep making more new TearPages?? (bet you're thinking it would be great if you could supressed that handler in the meantime.. ie.. RemoveHandler)

  3. #3

    Thread Starter
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Its not a problem of stopping people from 'tearing' again. The problem is. Tear the first time and close the new torn window. Tear again, and 2 windows appear. Close both of those. Tear again and 3 windows appear! See what I mean?

    Plus, tabs with more than 1 control makes a new form for each control isntead of putting them all on 1 like I thought it should.

    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  4. #4
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    I can only guess that somehow when the user closes the form, it does not dispose, and that handler remains. So when they click on a menuItem again, it now fires off two events, running your sub twice. And the more they click and close, the more event handlers are getting assigned.

    I always used MyDynamicForm.Dispose .... and haven't had a problem... but maybe you should explicitly using RemoveHandler...
    Last edited by nemaroller; Jun 3rd, 2003 at 12:24 PM.

  5. #5

    Thread Starter
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    OMG I am a moron. Of course. DUH! Working now. Except I still cant get all.

    Still have a problem with a tab with more than 1 control. It only puts 1 control onto the new form and ignores the next one.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  6. #6
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    OMG I am a moron.
    Don't worry, tomorrow you'll wake up and everything will be back the way it was for the last 4 years...

    I don't know jack about TabControls... never cared for em much... so good luck with that

  7. #7

    Thread Starter
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Thanks.

    for anyone that can help with second issue. Here is the code where I build a tab with 2 textboxes
    VB Code:
    1. Public Sub Panel_Init(ByRef tab As TabControl)
    2.         Dim page As TabPage
    3.         Dim tb As TextBox
    4.         Dim tb2 As TextBox
    5.  
    6.         ' Create a new tab
    7.         page = New TabPage()
    8.         page.Text = "Chat"
    9.         page.Name = "Chat"
    10.  
    11.         ' Create textbox that will appear in Home tab
    12.         tb = New TextBox()
    13.         tb.Name = "tbChat"
    14.         tb.Multiline = True
    15.         tb.Height = tab.Height - 400
    16.         tb.Width = tab.Width
    17.         tb.Anchor = AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right Or AnchorStyles.Top
    18.         ' Add textbox to tab page and tab page to tab container.
    19.  
    20.  
    21.         tb2 = New TextBox()
    22.         tb2.Name = "tbSend"
    23.         tb2.Multiline = True
    24.         tb2.Top = tab.Height - 360
    25.         tb2.Height = 50
    26.         tb2.Width = tab.Width
    27.         tb2.Anchor = AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right
    28.         ' Add textbox to tab page and tab page to tab container.
    29.         page.Controls.Add(tb)
    30.         page.Controls.Add(tb2)
    31.  
    32.         tab.TabPages.Add(page)
    33.         'tb = Nothing
    34.     End Sub
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  8. #8
    Lively Member
    Join Date
    Jan 2003
    Posts
    71
    I can't quite figure out why its not working, although I am new to .NET.

    The following is some code I wrote to create a tabpage on the fly, prompt for the Text field and put a textbox on it. It works fine. Maybe you will see something I don't.




    Private Sub TopicAddMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TopicAddMenuItem.Click
    ' Create a new tab and add a rich text box to it

    ' Set up variables to prompt for Tab name
    Dim message, title As String
    Dim myValue As Object
    message = "Enter Tab Name" ' Set prompt.
    title = "SmartPad" ' Set title.



    ' Set up variables for new Tab page and Rich Text Box
    Dim TabPageNew As System.Windows.Forms.TabPage
    Dim TextBoxNew As System.Windows.Forms.TextBox

    TextBoxNew = New System.Windows.Forms.TextBox()
    TextBoxNew.Location = New System.Drawing.Point(8, 8)
    TextBoxNew.Name = "TextBoxNew"
    TextBoxNew.Size = New System.Drawing.Size(520, 288)
    TextBoxNew.Multiline = True
    TextBoxNew.TabIndex = 0
    TextBoxNew.Text = ""

    'TabPageNew
    '
    TabPageNew = New System.Windows.Forms.TabPage()

    TabPageNew.Controls.AddRange(New System.Windows.Forms.Control() {TextBoxNew})
    TabPageNew.Location = New System.Drawing.Point(4, 22)
    TabPageNew.Name = "TabPageNew"
    TabPageNew.Size = New System.Drawing.Size(528, 302)
    TabPageNew.TabIndex = 0

    ' Display message, title and get the Tab Name
    Do
    myValue = InputBox(message, title)
    Loop Until myValue <> ""


    TabPageNew.Text = myValue

    ' add the control, make the new tab active and bring focus to the rich text box
    Me.SmartPadTabControl.Controls.AddRange(New System.Windows.Forms.Control() {TabPageNew})
    Me.SmartPadTabControl.SelectedIndex = Me.SmartPadTabControl.TabCount - 1
    TextBoxNew.Focus()
    End Sub

  9. #9

    Thread Starter
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Creating the tab page isnt the problem. I have an option where a user can create a popup form which becomes a copy of the selected Tab(ie all the controls on it).

    So i do a for loop that adds each control on the tab page, tothe new form. Prblem is, when you add a control from the tab page to th form, it is removed from the Tabpage, and after that the for loop bugs out. I dont get it.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  10. #10
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    for anyone that can help with second issue. Here is the code where I build a tab with 2 textboxes.
    It would be nice if you tell me where is the problem with that piece of code?! Maybe I am dumb, cause i see no problem in the code and cant figure out what you are talking about.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  11. #11
    Lively Member
    Join Date
    Jan 2003
    Posts
    71
    I guess I am confused. I took your

    Public Sub Panel_Init(ByRef tab As TabControl)

    and tried to run it in an application. I put a tab control on a blank form, passed it to Panel_init and displayed the form. The text boxes do not appear on the new tab page. Yet, I do the same thing in my code, which I posted, and the textbox works fine.

    IAC, if I can come up with anything I will let you know.

  12. #12

    Thread Starter
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Well its the only place where i am looping through the controls on a tabpage and trying to add them to a new form

    VB Code:
    1. Dim test As New Form()
    2.                 For Each ctl In tabClient.TabPages(x).Controls
    3.                     test.Controls.Add(ctl)
    4.                 Next

    doing that, the 1st control disappears from the tab and appears on the form. A 2nd control doesnt do anything

    now. As a test I put in a MessageBox.Show(ctl.Name) . The second iteration, cause an Object reference not set error.
    Very bizarre.

    It wouldnt be a problem if the control wouldnt get removed from the tab I would bet. But I dont know why it removes it.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  13. #13

    Thread Starter
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Originally posted by ggprogram
    I guess I am confused. I took your

    Public Sub Panel_Init(ByRef tab As TabControl)

    and tried to run it in an application. I put a tab control on a blank form, passed it to Panel_init and displayed the form. The text boxes do not appear on the new tab page. Yet, I do the same thing in my code, which I posted, and the textbox works fine.

    IAC, if I can come up with anything I will let you know.
    Thanks. I have never been in a situation like this years where I am completly boggled as to why it wont work.

    It may very well just be a bug with the TabControl because this SHOULD be straightforward. Take controls from one container and stick it on another is elementary code.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  14. #14
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    The text boxes do not appear on the new tab page. Yet, I do the same thing in my code, which I posted, and the textbox works fine.
    But both text boxes appear, when i test it, you just have to make your tabpage height tall enough.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  15. #15

    Thread Starter
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Let me simplify this. Here is the entire solution. Open the .sln file in the VComm directory, then run it. Right click on the Chat tab and click Tear.
    Attached Files Attached Files
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  16. #16
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Well, both textboxes are being added.... only one shows though... and I noticed when I resize your form, the first textbox seems ANCHORED to all sides.... so i removed the anchor, didn't change anything...


    My guess: Your first textbox is sitting atop (or behind) the second textbox... and since you aren't defining a position, they are effectively covering up the other one...

  17. #17
    Lively Member
    Join Date
    Jan 2003
    Posts
    71
    Somehow Controls.Add is Moving the textbox,not copying the textbox. So I am going to guess that somewhere a shallow copy of a pointer is being made. IOW, when you add the control to the form, a pointer is being changed rather than a new textbox being created. BAsically you want to clone the original textbox and assign it to the new form.

    If you set a Watch on ctl and step through with the degugger, on the first pass, ctl is the first textbox. On the second, its Nothing. Thats becasue there is now one less control in the collection.

    You can prove this to yourself by running the loop twice:

    For Each ctl In tabClient.TabPages(x).Controls
    test.Controls.Add(ctl)
    Next

    For Each ctl In tabClient.TabPages(x).Controls
    test.Controls.Add(ctl)
    Next

    This will move both textboxes to the new form.

    I am not sure if this is some kind of bug ???

  18. #18
    Lively Member
    Join Date
    Jan 2003
    Posts
    71
    In thinking about it more, the workaround should be easy. In the loop create a new textbox, copy the attributes from the original, and then Add the new one to the new form.

  19. #19

    Thread Starter
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Originally posted by nemaroller
    Well, both textboxes are being added.... only one shows though... and I noticed when I resize your form, the first textbox seems ANCHORED to all sides.... so i removed the anchor, didn't change anything...


    My guess: Your first textbox is sitting atop (or behind) the second textbox... and since you aren't defining a position, they are effectively covering up the other one...
    No. The tetbox is not being added. Already checked for that.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  20. #20

    Thread Starter
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Originally posted by ggprogram
    In thinking about it more, the workaround should be easy. In the loop create a new textbox, copy the attributes from the original, and then Add the new one to the new form.
    Not so easy. The whole system needs to be dynamic as these tabs can be added via 3rd party plugins. So i cant expect only certain controls.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  21. #21

    Thread Starter
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Found an acceptable work around

    Groupbox on tabpage. Control's in groupbox. Add groupbox to form.

    voila.

    I guess that will do.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  22. #22
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    I was about to send the solution but i couldnt connect to internet (problem with our up link provider , ooooof). Anyway here is the solution:
    VB Code:
    1. For x = 0 To tabClient.TabCount - 1
    2.             rect = tabClient.GetTabRect(x)
    3.             If rect.Contains(pos) Then
    4.                 Dim test As New Form()
    5.                 Dim i As Integer
    6.                 Dim j As Integer = tabClient.TabPages(x).Controls.Count - 1
    7.                 For i = j To 0 Step -1
    8.                    test.Controls.Add(tabClient.TabPages(x).Controls(j))
    9.                 Next
    10.                 test.Show()
    11.                 Exit For
    12.             End If
    13.         Next
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width