locked
Contents of ListView won't appear when control on Child Form but will when on Main Form RRS feed

  • Question

  • Good evening,

    I'm experiencing some strange behavior in my VB.Net application and am hopeful somebody can guide me in the right direction. I am coding a WinForms application with .Net Framework 4.7.1 as the Target Framework in MS VS Community 2017 version 15.9.4.

    To date everything I have done has been on the Main Form and has worked well.  Today I added a Child Form by right-clicking on the Project in the Solution Explorer, selecting Add | Windows Form from the menu and then Windows Form from the "Add New Item" dialog.  I called it "frmNewProposal".

    I placed a ListView control on the new form along with logic to populate it with multiple rows of data in 4 columns.  A button on the Main Form launches the Child Form.

    I added "Friend mNewProposal As frmNewProposal" at the top of frmMain.vb:

    Namespace ProposalBuilder
        Friend Class frmMain
            Friend mNewProposal As frmNewProposal

    I then have the following in frmMain to open the Child Form:

    Private Sub btnProposalNew_Click(sender As Object, e As EventArgs) Handles btnProposalNew.Click
        CreateNewProposal()
    End Sub
    
    Public Sub CreateNewProposal()
        mNewProposal = New frmNewProposal(Me)
        mNewProposal.Show()
    End Sub

    frmNewProposal.vb contains the following code:

    Friend Class frmNewProposal
    #Region "Constructors"
        Private _appMain As frmMain = Nothing
    
        Public Sub New(ByVal oAppMain As frmMain)
            MyClass.New()
            _appMain = oAppMain
    
            Init()
        End Sub
    
        Public Sub New()
            Init()
        End Sub
    
        Private Sub Init()
            InitializeComponent()
        End Sub
    #End Region
    
        Private Sub frmNewProposal_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            LoadListView()
        End Sub
    
        Private Sub LoadListView()
            With lsvData.Items.Add("Alabama")
                .SubItems.Add("AL")
                .SubItems.Add("4,447,100")
                .SubItems.Add("52,423 sq miles")
            End With
    
            With lsvData.Items.Add("Alaska")
                .SubItems.Add("AK")
                .SubItems.Add("626,932")
                .SubItems.Add("656,425 sq miles")
            End With
    
            With lsvData.Items.Add("Arizona")
                .SubItems.Add("AZ")
                .SubItems.Add("5,130,632")
                .SubItems.Add("114,006 sq miles")
            End With
        End Sub
    End Class

    When the application is executed and the Child Form appears the ListView control is empty.  However, if I inspect the Object I can see that:

    • lsvData.Items.Count = 3
    • lsvData.Items -> Results View:
    •     (0) = "Alabama"
    •     (1) = "Alaska"
    •     (2) = "Arizona"


    I defined the 4 Columns of the ListView control in the Resource Properties and these appear when the application is running.

    I found that if I copy the Control from the Child Form to the Main Form and duplicate the above code to add the items they appear in the control.

    I'm guessing there's something I've done incorrectly regarding the Child Form but cannot figure out what it is.

    Thank you in advance for any assistance.

    Jason.

    Sunday, March 3, 2019 2:30 AM

Answers

  • Hi,
    in your constructor you must at first execute InitializeComponent to instantiate the ListView.

    #Region "Constructors"
      Private _appMain As frmMain = Nothing
    
      Public Sub New(ByVal oAppMain As frmMain)
        Init()
        _appMain = oAppMain
      End Sub
    
      Public Sub New()
        Init()
      End Sub
    
      Private Sub Init()
        InitializeComponent()
      End Sub
    #End Region



    --
    Viele Grüsse
    Peter Fleischer (ehem. MVP für Developer Technologies)
    Meine Homepage mit Tipps und Tricks

    • Proposed as answer by Peter Fleischer Sunday, March 3, 2019 1:20 PM
    • Marked as answer by JasonUSA Sunday, March 3, 2019 7:21 PM
    Sunday, March 3, 2019 5:03 AM

All replies

  • Hi,
    in your constructor you must at first execute InitializeComponent to instantiate the ListView.

    #Region "Constructors"
      Private _appMain As frmMain = Nothing
    
      Public Sub New(ByVal oAppMain As frmMain)
        Init()
        _appMain = oAppMain
      End Sub
    
      Public Sub New()
        Init()
      End Sub
    
      Private Sub Init()
        InitializeComponent()
      End Sub
    #End Region



    --
    Viele Grüsse
    Peter Fleischer (ehem. MVP für Developer Technologies)
    Meine Homepage mit Tipps und Tricks

    • Proposed as answer by Peter Fleischer Sunday, March 3, 2019 1:20 PM
    • Marked as answer by JasonUSA Sunday, March 3, 2019 7:21 PM
    Sunday, March 3, 2019 5:03 AM
  • Hi Viele,

    Thank you very much for your response.  I was already including a call to InitializeComponent() in "Private Sub Init()" which was called from both the Constructors in the Class.

    Based on your response I tried both of the following but the result was the same: empty ListView object:

    Friend Class frmNewProposal
    #Region "Constructors"
        Private _appMain As frmMain = Nothing
    
        Public Sub New(ByVal oAppMain As frmMain)
            MyClass.New()
    
            InitializeComponent()
    
            _appMain = oAppMain
        End Sub
    
        Public Sub New()
            
        End Sub
    #End Region

    Friend Class frmNewProposal
    #Region "Constructors"
        Private _appMain As frmMain = Nothing
    
        Public Sub New(ByVal oAppMain As frmMain)
            MyClass.New()
    
            InitializeComponent()
    
            _appMain = oAppMain
        End Sub
    
        Public Sub New()
            InitializeComponent()
        End Sub
    #End Region

    Do I need to include an override of the InitializeComponent() function in my class or is it sufficient to simply call it?

    Thanks again,

    Jason.

    Sunday, March 3, 2019 11:34 AM
  • Hi Jason,
    use my code. Which result?

    --
    Viele Grüsse
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Sunday, March 3, 2019 11:41 AM
  • Hi Jason,
    use my code. Which result?

    --
    Viele Grüsse
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Hi Viele,

    My apologies.  I looked at the code you provided a couple of times but incorrectly thought you'd copied & pasted my code into your response!  I'm very sorry.  Once I saw that you'd put the call to InitializeComponent() ahead of _appMain = oAppMain I made the same change in my code and the ListView control is now being populated as desired.

    Thank you very much :)

    Regards,

    Jason.

    Sunday, March 3, 2019 7:21 PM
  • Peter,

    Sorry for calling you Viele.  Evidently my Deutsche is failing me just as much as my VB.Net!

    Vielen dank,

    Jason.

    Sunday, March 3, 2019 7:25 PM