Help with NULL error, when trying to add from a form, a list, to another form and list.

Hekzdaddy 121 Reputation points
2021-04-06T05:11:01.087+00:00

Public Sub AddPrintBook()
Select Case ShoppingCart.lstCart.Items.Add(lstPrintBooks.SelectedIndex) '"""""Getting a NULL error here. """""

        Case 0
            strSelectedBook = strYourWay
            decSubTotal += decYourWay
            decShippingTotal += decShippingTotal
        Case 1
            strSelectedBook = strHistoryScotland
            decSubTotal += decHistoryScotland
            decShippingTotal += decShippingTotal
        Case 2
            strSelectedBook += decCalculus
            decSubTotal += decHistoryScotland
            decShippingTotal += decShippingTotal
        Case 3
            strSelectedBook = strRelax
            decSubTotal += decRelax
            decShippingTotal += decShippingTotal

    End Select

End Sub
Developer technologies | VB
{count} votes

Answer accepted by question author
  1. Xingyu Zhao-MSFT 5,381 Reputation points
    2021-04-06T09:21:21.217+00:00

    Hi @Hekzdaddy ,
    Take a look at the following example:
    I have listbox1 on form1 and listbox1 on form2.
    Form1:

    Public Class Form1  
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
            Dim frm2 As Form2 = New Form2(Me)  
            frm2.Show()  
        End Sub  
    End Class  
    

    Form2:

    Public Class Form2  
        Private ReadOnly frm1 As Form1  
        Public Sub New(ByVal form1 As Form1)  
            frm1 = form1  
            InitializeComponent()  
        End Sub  
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click  
            ListBox1.Items.Add(frm1.ListBox1.SelectedIndex)  
        End Sub  
    End Class  
    

    Best Regards,
    Xingyu Zhao
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Karen Payne MVP 35,591 Reputation points Volunteer Moderator
    2021-04-06T10:58:09.88+00:00

    The following code sample demonstrates passing information from one form to another form along with using assertion and being pro-active to ensure no runtime issues. I didn't match up your current code, wanted to give you a clear example that is easy to understand and allow anyone to try it out. Feel free to tweak as needed but the idea is to simply learn and adapt to your code.

    Full project source code

    1 is parent form, 2 is the child form where in form2 pressing a button passes the current month name to the list box in the calling form.

    84875-f1.png

    Main form

    Public Class Form1  
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles ShowChildForm.Click  
            Dim childForm As New Form2  
      
            AddHandler Form2.ListBoxSelectionChanged, AddressOf AddMonth  
      
            childForm.ShowDialog()  
      
            RemoveHandler Form2.ListBoxSelectionChanged, AddressOf AddMonth  
        End Sub  
      
        Private Sub AddMonth(monthName As String)  
            If MonthsListBox.FindStringExact(monthName, 0) = -1 Then  
                MonthsListBox.Items.Add(monthName)  
                MonthsListBox.SelectedIndex = MonthsListBox.Items.Count - 1  
            End If  
        End Sub  
      
        Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown  
            MonthsListBox.Sorted = True  
        End Sub  
      
        Private Sub GetCurrentMonthButton_Click(sender As Object, e As EventArgs) Handles GetCurrentMonthButton.Click  
            If MonthsListBox.SelectedIndex > -1 Then  
                MessageBox.Show($"{MonthsListBox.SelectedIndex} -> {MonthsListBox.Text}")  
            End If  
        End Sub  
    End Class  
      
    

    Child form

    Imports System.Globalization  
      
    Public Class Form2  
      
        Public Delegate Sub OnListBoxSelectDelegate(month As String)  
        Public Shared Event ListBoxSelectionChanged As OnListBoxSelectDelegate  
      
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
      
            MonthsListBox.DisplayMember = "MonthName"  
            MonthsListBox.DataSource = Enumerable.Range(1, 12).  
                Select(Function(index)  
                           Return New With {Key .MonthName =  
                            DateTimeFormatInfo.CurrentInfo.GetMonthName(index)}  
                       End Function).ToList()  
      
        End Sub  
        Private Sub SendToParentFormButton_Click(sender As Object, e As EventArgs) _  
            Handles SendToParentFormButton.Click  
      
            If MonthsListBox.SelectedIndex > -1 Then  
                RaiseEvent ListBoxSelectionChanged(MonthsListBox.Text)  
            End If  
        End Sub  
    End Class  
      
    
    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.