question

Hekzdaddy-3952 avatar image
0 Votes"
Hekzdaddy-3952 asked Hekzdaddy-3952 commented

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

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
dotnet-visual-basic
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @Hekzdaddy-3952 ,
May I know whether your issue has been solved or not? If not, please share it in here.

0 Votes 0 ·

Yes it was solved

0 Votes 0 ·
XingyuZhao-MSFT avatar image
1 Vote"
XingyuZhao-MSFT answered

Hi @Hekzdaddy-3952 ,
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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered

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



f1.png (15.3 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.