dcsimg
Results 1 to 15 of 15

Thread: Update control content in a form inside a tab page from another form

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2019
    Posts
    9

    Update control content in a form inside a tab page from another form

    Hi,

    I have searched both in the forum and in Google but I couldn't find my exact situation, so this is my case.

    I have an application with a tab control that is used to open forms inside each tab page, in a way that each form can stay opened at the same time.

    From one form (form 1) opened in one tab (tab A) I do some stuff and I want to reflect the results inside a control that is on another form (form 2) on another tab page (tab B), namely updating a listView that acts like a log.

    In form 1 I call a Friend sub from form 2 which executes all the updating of the listView. If I execute the code step-by-step, it goes through all the code, but no result is displayed in the listView.

    However, if I open form 2 as an independent window rather than inside a tab page, it works perfectly as expected.

    Am I missing something related with permissions/access/...? Thank you very much for your help!

  2. #2
    Addicted Member Goggy's Avatar
    Join Date
    Oct 2017
    Posts
    162

    Re: Update control content in a form inside a tab page from another form

    Do you create a new instance of the forms every time you open a tab?
    Utterly useless, but always willing to help

    As a finishing touch god created the dutch

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2019
    Posts
    9

    Re: Update control content in a form inside a tab page from another form

    Hi Goggy, I think yes. The Sub that manages the tab page open method is as follows:

    Code:
        
    Friend Sub OpenTabPage(Of typeForm As {Form, New})(container As TabControl, pageName As String, pageTabText As String)
            Dim page As New TabPage()
            page.Text = pageTabText
            page.Name = pageName
    
            page.Controls.Add(New typeForm With {.TopMost = False, .TopLevel = False, .Visible = True, .FormBorderStyle = FormBorderStyle.None, .Dock = DockStyle.Fill, .AutoScroll = True})
            container.TabPages.Add(page)
            container.SelectedIndex = container.TabPages.IndexOfKey(pageName)
        End Sub
    However, even if the tab page of form 2 (update destination) is already open and neither close nor open again, it doesn't update. Any suggestion?

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,936

    Re: Update control content in a form inside a tab page from another form

    Quote Originally Posted by mrwengineer View Post
    I have searched both in the forum and in Google but I couldn't find my exact situation
    And that's the problem: you're looking for an "example" that isn't really an example but rather a turnkey solution to your exact problem. With programming, as with many subjects, you should learn the principles involved first and then you can apply them to many different situations. Initiating a change on one form from another form is a very, very common thing to do. If you know how to do it in the general case, you know how to do it in yours. The fact that the form is hosted in a TabPage isn't really relevant because it still gets created in the same way, you still reference it in the same way and you still call methods, set properties and raise and handle events in the same way.

  5. #5

    Thread Starter
    New Member
    Join Date
    Jun 2019
    Posts
    9

    Re: Update control content in a form inside a tab page from another form

    Quote Originally Posted by jmcilhinney View Post
    And that's the problem: you're looking for an "example" that isn't really an example but rather a turnkey solution to your exact problem. With programming, as with many subjects, you should learn the principles involved first and then you can apply them to many different situations.
    I totally agree with you, this is what I have been doing for a long time, and I have learned a lot from the general cases, official MS documentation, books and forums. If I ask this situation here is because, after trying to figure out the problem, implemented several alternatives and looking for an existing similar case, I was not able to succeed. Please take this into account

    Quote Originally Posted by jmcilhinney View Post
    ]The fact that the form is hosted in a TabPage isn't really relevant because it still gets created in the same way, you still reference it in the same way and you still call methods, set properties and raise and handle events in the same way.
    I know how to pass information between forms and call methods, and in fact this is what I am doing here, but I do not understand why I can't see the results in this case. As I mentioned, if I execute the same with an independent windows, it works like a charm, so my believe is that the fact that the form is embeeded in a tabPage has definitely something related with that.

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,936

    Re: Update control content in a form inside a tab page from another form

    Quote Originally Posted by mrwengineer View Post
    Please take this into account
    We can't really take into account things that you haven't told us.
    Quote Originally Posted by mrwengineer View Post
    in fact this is what I am doing here
    Doing what here?
    Quote Originally Posted by mrwengineer View Post
    I do not understand why I can't see the results in this case
    We're unlikely to understand either, if we have never seen what you're doing.
    Quote Originally Posted by mrwengineer View Post
    if I execute the same with an independent windows, it works like a charm
    Execute what?

    To sum up, show us what you did because we can't tell you what's wrong with it if we've never seen it. What I can see from post #3 is that you don't retain a reference to the form you create so it wouldn't be a surprise to learn that you are actually referring to a different form later on. We can only speculate though. If you want help with code that doesn't work, show the code.

  7. #7
    Addicted Member Goggy's Avatar
    Join Date
    Oct 2017
    Posts
    162

    Re: Update control content in a form inside a tab page from another form

    page.Controls.Add(New typeForm With {.TopMost = False, .TopLevel = False, .Visible = True, .FormBorderStyle = FormBorderStyle.None, .Dock = DockStyle.Fill, .AutoScroll = True})

    This line will create an new instance of typeForm, ... so what ever you think you send to a Form, it was NOT send to this new form.
    Utterly useless, but always willing to help

    As a finishing touch god created the dutch

  8. #8

    Thread Starter
    New Member
    Join Date
    Jun 2019
    Posts
    9

    Re: Update control content in a form inside a tab page from another form

    Quote Originally Posted by Goggy View Post
    page.Controls.Add(New typeForm With {.TopMost = False, .TopLevel = False, .Visible = True, .FormBorderStyle = FormBorderStyle.None, .Dock = DockStyle.Fill, .AutoScroll = True})

    This line will create an new instance of typeForm, ... so what ever you think you send to a Form, it was NOT send to this new form.
    OK I understand that. So how do you keep "track" of the opened form (inside a tabPage) so you can update the content on that form? I tried this call from form1 (referencing the form2 control by it's name) but doesn't work:

    frmContainer.tcTabControlContainer.TabPages(index_tabPage_form2).frmForm2.UpdateMethodFromForm2()

    because the TabPage is created at run time, so it does not exist already. It tells me that frmForm2 is not a member of TabPages.

    Thank you again.

  9. #9
    Addicted Member Goggy's Avatar
    Join Date
    Oct 2017
    Posts
    162

    Re: Update control content in a form inside a tab page from another form

    I would guess,

    Create all instances of forms you are going to create on Form load (Your main form, the one that holds the tabcontrol).
    And then re-use these forms...
    Utterly useless, but always willing to help

    As a finishing touch god created the dutch

  10. #10

    Thread Starter
    New Member
    Join Date
    Jun 2019
    Posts
    9

    Re: Update control content in a form inside a tab page from another form

    Quote Originally Posted by Goggy View Post
    Create all instances of forms you are going to create on Form load (Your main form, the one that holds the tabcontrol).
    And then re-use these forms...
    For the type of application I am developing, I can't open all the forms at the same time. It is for that reason that I use tabs to open and close the forms as required by the user.

    For the moment I am changing my approach, storing the log data from form 1 into a table in a database, and then load the information into the controls of form 2 when it is loaded. The problem is that it is not in "realtime"; once the form 2 is loaded, the information is there at that particular time. I added a "Refresh" functionality which loads again whole the database, but still not realtime.

    Any other suggestions?

  11. #11
    Frenzied Member ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    2,047

    Re: Update control content in a form inside a tab page from another form

    not sure what you are trying to do

    here my guess
    add this to Form1
    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim Frm As New Form2
            Frm.xName = "yourText"
    
            With Frm.ListView1
                .Items.Clear()
                .Columns.Clear()
                .View = View.Details
                .CheckBoxes = True
                .Columns.Add("Column 0")
                .Columns.Add("Column 1")
                .Columns.Add("Column 2")
                .Columns.Add("Column 3")
                .Columns.Add("Column 4")
    
                For i As Integer = 0 To 10
                    Dim Li As New ListViewItem
                    Li.Text = "Itemljlkjlkjljlkjl " & i.ToString & ".xyz"
                    For j As Integer = 0 To 3
                        Li.SubItems.Add("Item " & i.ToString & "." & (j + 1).ToString)
                        If (j = 1) And ((i Mod 2) = 1) Then
    
                            If j <> Nothing Then
                                Li.Checked = True
                            End If
    
                            Exit For
                        End If
                    Next
                    .Items.Add(Li)
    
                Next
                .Refresh()
            End With
            'MsgBox(Frm.xName)
            Frm.ShowDialog(Me)
            Frm.Dispose()
            Frm = Nothing
        End Sub
    
       
    End Class
    and add 1 Listview with a Textbox to form2
    Code:
    Public Class Form2
    
        'add a Textbox and a Listview
    
    
        Public Property xName() As String
            Get
                xName = TextBox1.Text
            End Get
            Set(ByVal value As String)
                TextBox1.Text = value
            End Set
        End Property
    
        
    End Class
    in Form2 the Listview is Filled and the Value in the Textbox is shown
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  12. #12
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,936

    Re: Update control content in a form inside a tab page from another form

    Quote Originally Posted by mrwengineer View Post
    So how do you keep "track" of the opened form (inside a tabPage) so you can update the content on that form?
    How did you keep track of the forms when they were not in TabPages? Could it be that there was only one form when you did that and the issue is really keeping track of multiple forms and the TabPage is irrelevant? You STILL have not provided us with all the relevant information.

  13. #13
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,460

    Re: Update control content in a form inside a tab page from another form

    Code:
    Public Class Form2
    
       Public Property frmFormToChange As Form1
    
        Public Sub New(ByVal FormToChange As Form1)
            Me.frmFormToChange = FormToChange
        End Sub
    
        Public Sub whatever() 
            Me.frmFormToChange.Label1.Text = "This is how"
        End Sub
    
    End Class
    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal Sender As Object, ByVal e as System.EventArgs) Handles Button1.Click
    
            Dim frm As New Form2(Me)
            frm.show
    
        End Sub
    
    End Class

  14. #14

    Thread Starter
    New Member
    Join Date
    Jun 2019
    Posts
    9

    Re: Update control content in a form inside a tab page from another form

    Thank you ChrisE and .paul. For what can I see in your guesses, if I had understood properly:

    - ChrisE: you declare the form to be updated (form 2) inside the sender form (form 1), and pass the information using a Public Property on the updated form (form 2).
    - .paul.: you declare the form to be updated (form 1 in your code) directly as a Public Property of the sender form (form 2 in your code).

    In my case I think will be tricky to implement, because of the mission of the form to be updated, which is an activity log, and that means that has to be able to be updated also from another sender forms (form 3, 4,...) and not only from the sender form 1 here in the discussion, gathering information from all around the application. So for this reason I think the updated form can't be declared inside the sender form; otherwise when sending information from another forms, I will get somehow a similar trouble as the one where I am now.

    Another reason is that forms are going to be declared on runtime. I think the updated form must be created at design time in order to keep the information gathered, but the sender forms are created and disposed as the application runs.

    Again, thank you for your support, it is very appreciated. If any other suggestions came to you, feel free to share them

  15. #15
    Frenzied Member ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    2,047

    Re: Update control content in a form inside a tab page from another form

    you pass the changed data back to Form1

    Code:
    Public Class Form2
    
        'add a Textbox and a Listview
    
    
        Public Property xName() As String
            Get
                xName = TextBox1.Text
            End Get
            Set(ByVal value As String)
                TextBox1.Text = value
            End Set
        End Property
    
        Private Sub Form2_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
            Form1.TextBox1.Text = xName
        End Sub
    form1:
    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim Frm As New Form2
            Frm.xName = TextBox1.Text
    
            With Frm.ListView1
                .Items.Clear()
                .Columns.Clear()
                .View = View.Details
                .CheckBoxes = True
                .Columns.Add("Column 0")
                .Columns.Add("Column 1")
                .Columns.Add("Column 2")
                .Columns.Add("Column 3")
                .Columns.Add("Column 4")
    
                For i As Integer = 0 To 10
                    Dim Li As New ListViewItem
                    Li.Text = "Itemljlkjlkjljlkjl " & i.ToString & ".xyz"
                    For j As Integer = 0 To 3
                        Li.SubItems.Add("Item " & i.ToString & "." & (j + 1).ToString)
                        If (j = 1) And ((i Mod 2) = 1) Then
    
                            If j <> Nothing Then
                                Li.Checked = True
                            End If
    
                            Exit For
                        End If
                    Next
                    .Items.Add(Li)
    
                Next
                .Refresh()
            End With
            MsgBox(Frm.xName)
            Frm.ShowDialog(Me)
            Frm.Dispose()
            Frm = Nothing
        End Sub
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width