Vb.net ArgumentNullException when removing from dictionary - vb.net

I'm having an issue where I'm trying to delete an element from my dictionary at the selected index in a listbox. I added 3 elements to a SortedDictionary and printing them into a list box. I'm trying to delete the item from the dictionary that is highlighted in the listbox, however when I click remove I get the System.ArgumentNullException: 'Value cannot be null. Parameter name: key' within my Sub btnDeleteLibrary_Click Why is this?
Error occurs at the line Libraries.Remove(lstLibraries.SelectedValue)
Public Class frmManager
Dim Libraries As New SortedDictionary(Of String, String)
Private Sub frmManager_Load(sender As Object, e As EventArgs) Handles Me.Load
Libraries.Add("Zahnow Library", "SVSU")
Libraries.Add("Fleschner Memorial Library", "BR")
Libraries.Add("Scott D. James Technical Repository", "SDJ")
lstLibraries.Items.Clear()
populatelstLibrary()
End Sub
Sub populatelstLibrary()
For Each library In Libraries
lstLibraries.Items.Add(vbCrLf & library.Key & " --- " & library.Value)
Next
End Sub
Private Sub btnDeleteLibrary_Click(sender As Object, e As EventArgs) Handles btnDeleteLibrary.Click
Libraries.Remove(lstLibraries.SelectedValue)
lstLibraries.Items.Clear()
populatelstLibrary()
End Sub
End Class

Since you are constructing a custom display string for your ListBox items it makes it harder to map that directly to the dictionary.
The easiest solution would be to create a custom class that you store in the ListBox and set the ListBox's DisplayMember and ValueMember properties, telling it how to display each item as well as which property it should get from an item when you call SelectedValue.
Class:
Public Class LibraryItem
Public Property Name As String
Public Property Code As String
Public ReadOnly Property DisplayName As String
Get
Return vbCrLf & Me.Name & " --- " & Me.Code
End Get
End Property
Public Sub New()
End Sub
Public Sub New(ByVal Name As String, ByVal Code As String)
Me.Name = Name
Me.Code = Code
End Sub
End Class
Initial setup:
Private Sub frmManager_Load(sender As Object, e As EventArgs) Handles Me.Load
'Tell the ListBox which properties to use for display and value.
lstLibraries.DisplayMember = "DisplayName"
lstLibraries.ValueMember = "Name"
'Your code...
End Sub
Filling the ListBox:
Sub populatelstLibrary()
For Each library In Libraries
lstLibraries.Items.Add(New LibraryItem(library.Key, library.Value))
Next
End Sub
Now SelectedValue will get you the value of the selected item's Name property, which corresponds to the key in the dictionary.

I would change some things in your code. First it seems that you have your sorted dictionary built with the wrong values for Key and Value, change it to
Libraries.Add("SVSU","Zahnow Library")
Libraries.Add("BR", "Fleschner Memorial Library")
Libraries.Add("SDJ", "Scott D. James Technical Repository")
' and call immediately
populatelstLibrary()
Now in populatelstLibrary change the code to
Sub populatelstLibrary()
lstLibraries.DataSource = Nothing
lstLibraries.DisplayMember = "Value"
lstLibraries.ValueMember = "Key"
lstLibraries.DataSource = Libraries.ToList()
End Sub
finally in the button click just check for null and remove the SelectedValue
Private Sub btnDeleteLibrary_Click(sender As Object, e As EventArgs) Handles btnDeleteLibrary.Click
If lstLibraries.SelectedValue IsNot Nothing Then
Libraries.Remove(lstLibraries.SelectedValue)
populatelstLibrary()
End If
End Sub

Related

Removing items in a collection based on listbox string

Having issues when clicking the remove button. If more of my code is needed, let me know. I get this error on the line AddressList.Remove(selectedName):
System.ArgumentException: 'Argument 'Key' is not a valid value.
I've tried many variations but can't figure out why this doesn't work. I think it has something to do with how the strings are concatenated in the listbox. I need to be able to remove entries from the collection and the listbox. Any help would be greatly appreciated.
Module EmailCollection
Public AddressList As New Collection
Public Sub AddRecord(ByVal a As cAddress)
Try
AddressList.Add(a)
Catch ex As Exception
MessageBox.Show("Error: inputs must be characters valid in string format")
End Try
End Sub
End Module
public class form1
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim frmAdd As New AddNewName
frmAdd.ShowDialog()
UpdateListBox()
End Sub
Private Sub UpdateListBox()
lstAddress.Items.Clear()
Dim a As cAddress
For Each a In AddressList
lstAddress.Items.Add(String.Concat(a.strName, a.strEmail, a.strPhone, a.strComment))
Next
If lstAddress.Items.Count > 0 Then
lstAddress.SelectedIndex = 0
End If
End Sub
Private Sub btnRemove_Click(sender As Object, e As EventArgs) Handles btnRemove.Click
Dim selectedName As String
Try
' Get the selected value.
selectedName = lstAddress.SelectedItem.ToString()
' Remove the selected name from the list box and the collection.
If MessageBox.Show("Are you sure?",
"Confirm Deletion",
MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
lstAddress.Items.Remove(selectedName)
AddressList.Remove(selectedName)
End If
Catch ex As NullReferenceException
MessageBox.Show("Select an item to remove.", "Selection Needed")
End Try
End Sub
end class
In your Module I changed AddressList from the old VB6 Collection type to the .net List(Of T). The T stands for Type.
Module EmailCollection
Public AddressList As New List(Of cAddress)
Public Sub AddRecord(ByVal a As cAddress)
AddressList.Add(a)
End Sub
End Module
I guessed that your cAddress class looks something like this. I added a custom .ToString method so the list box will display the data you wish but the item, itself, will be a cAddress object.
Public Class cAddress
Public Property strName As String
Public Property strEmail As String
Public Property strPhone As String
Public Property strComment As String
Public Overrides Function ToString() As String
Return $"{strName}, {strEmail}, {strPhone}, {strComment}"
End Function
End Class
In the Form...
Instead of adding a string to the list box I added the cAddress object. The list box calls .ToString on the object to get the display value.
Private Sub UpdateListBox()
ListBox1.Items.Clear()
For Each a As cAddress In AddressList
ListBox1.Items.Add(a)
Next
If ListBox1.Items.Count > 0 Then
ListBox1.SelectedIndex = 0
End If
End Sub
In the remove button I cast the selected item to its underlying type, cAddress. This is the item removed from the AddressList. Then simply remove the selected item from the list box.
Private Sub btnRemove_Click(sender As Object, e As EventArgs) Handles btnRemove.Click
If MessageBox.Show("Are you sure?",
"Confirm Deletion",
MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
AddressList.Remove(DirectCast(ListBox1.SelectedItem, cAddress))
ListBox1.Items.Remove(ListBox1.SelectedItem)
End If
End Sub
I changed the name of the list box to ListBox1 to match my test project.
Here is something to try, use a BindingSource for setting up the ListBox. In the class override ToString to what is to be shown in the ListBox rather than what you are doing now without a DataSource.
My version of your class has name and property name changes.
Public Class Address
Public Property Name() As String
Public Property Email() As String
Public Property Phone() As String
Public Property Comment() As String
Public Overrides Function ToString() As String
Return $"{Name} {Email} {Phone} {Comment}"
End Function
End Class
Mocked data is used to populate the ListBox
Public Class Form1
Private ReadOnly _bsAddresses As New BindingSource
Private Sub UpdateListBox()
Dim AddressList = New List(Of Address) From
{
New Address() With {
.Name = "John",
.Email = "john#gmail.com",
.Phone = "555-444-3456",
.Comment = "AAA"},
New Address() With {
.Name = "Mary",
.Email = "mary#gmail.com",
.Phone = "888-333-2222",
.Comment = "BBB"},
New Address() With {
.Name = "Bob",
.Email = "bob#gmail.com",
.Phone = "111-555-9999",
.Comment = "CCC"}
}
_bsAddresses.DataSource = AddressList
lstAddress.DataSource = _bsAddresses
lstAddress.SelectedIndex = 0
End Sub
Private Sub Form1_Shown(sender As Object, e As EventArgs) _
Handles Me.Shown
UpdateListBox()
End Sub
Private Sub RemoveButton_Click(sender As Object, e As EventArgs) _
Handles RemoveButton.Click
If lstAddress.Items.Count > 0 AndAlso lstAddress.SelectedItem IsNot Nothing Then
Dim address = CType(_bsAddresses.Current, Address)
If My.Dialogs.Question($"Remove {address.Name}") Then
_bsAddresses.RemoveCurrent()
RemoveButton.Enabled = _bsAddresses.Count > 0
End If
End If
End Sub
End Class
Code module for asking a question
Namespace My
<ComponentModel.EditorBrowsable(ComponentModel.EditorBrowsableState.Never)>
Partial Friend Class _Dialogs
Public Function Question(text As String) As Boolean
Return (MessageBox.Show(
text,
My.Application.Info.Title,
MessageBoxButtons.YesNo,
MessageBoxIcon.Question,
MessageBoxDefaultButton.Button2) = MsgBoxResult.Yes)
End Function
End Class
<HideModuleName()>
Friend Module WinFormsDialogs
Private instance As New ThreadSafeObjectProvider(Of _Dialogs)
ReadOnly Property Dialogs() As _Dialogs
Get
Return instance.GetInstance()
End Get
End Property
End Module
End Namespace
Karen's post seems quite comprehensive. My response is an attempt to focus on your direct question.
I don't see all of the type definitions shown in your code, but in answer to your question, which I believe is "Why am I getting System.ArgumentException: 'Argument 'Key' is not a valid value":
In the offending line of code:
AddressList.Remove(selectedName)
AddressList is a collection. To use .Remove, you must pass in an object of the AddressList collection to remove it. You are passing a simple string, and that is not an AddressList object. You need to create an object based on your string selectedName to pass into .Remove and that line should work. The rest of what you are doing seems more complex.

VB.Net Dictionary inside of a dictionary adding / removing data

I'm creating an application where I have different libraries, books and non-book media stored in dictionaries and displayed in listboxes. The user can add and remove additional dictionaries for any of these 3 elements. My issue lies in bringing up the new form to create a link between a library and it's media.
I have a listbox for "Books at Current library" and "Non-Book Media at Current Library" Which will display the media that is linked to the specific library that is highlighted in the listbox. And the user can freely add and remove different media to the library.
frmManager: https://prnt.sc/mnd8qf
frmAssociationScreen: https://prnt.sc/mnd8sh
I'm trying to create a dictionary within a dictionary that I can manipulate the data with for adding the different media to the individual libraries. But I'm unsure where to go from here, I'd like to start by hard coding a few links to Zahnow Library and add a few books as well as one non-book media.
Public Class frmManager
' Global data structures
Public Libraries As New Dictionary(Of String, String)
Public Books As New Dictionary(Of String, String)
Public nonBookMedia As New Dictionary(Of String, String)
Public EquippedLibrary As New Dictionary(Of String, LibraryWithMedia)
Structure LibraryWithMedia
Dim strLibraryName As String
Dim dicBooks As Dictionary(Of String, String)
Dim nonBookMedia As Dictionary(Of String, String)
End Structure
Private Sub frmManager_Load(sender As Object, e As EventArgs) Handles Me.Load
Libraries.Add("SVSU", "Zahnow Library")
Libraries.Add("BR", "Fleschner Memorial Library")
Libraries.Add("SDJ", "Scott D. James Technical Repository")
Books.Add("104", "Data Structures for Fun and Profit")
Books.Add("103", "Doing More With Less - Naval Lint Art")
Books.Add("102", "Interpretive Klingon Poetry")
Books.Add("105", "Programming with the Bidgoli")
Books.Add("101", "Zen and the Art of Appliance Wiring")
nonBookMedia.Add("201", "CD - IEEE Computer: the Hits")
nonBookMedia.Add("203", "DVD - Databases and You: the Video Experience")
nonBookMedia.Add("202", "DVD - The Pirates of Silicon Valley")
populatelstLibrary()
populatelstBooks()
populatelstBookMedia()
End Sub
Sub populatelstLibrary()
lstLibraries.Items.Clear()
For Each library In Libraries
lstLibraries.Items.Add(library.Value & " --- " & library.Key)
Next
End Sub
How I manipulated the data to delete library dictionary
Private Sub btnDeleteLibrary_Click(sender As Object, e As EventArgs) Handles btnDeleteLibrary.Click
Dim key As String = ""
Dim tmpLibraries As New Dictionary(Of String, String)
' If an index is selected in listbox then continue
' If nothing selected, the button does nothing
If lstLibraries.SelectedIndex > -1 Then
If MsgBox("Are you sure you want to delete this library?", MsgBoxStyle.YesNoCancel, "Delete confirmation") = MsgBoxResult.Yes Then
For Each library In Libraries
If lstLibraries.SelectedItem.Equals(library.Value & " --- " & library.Key) Then
' DoNothing
' the selected item is not added to temp library
Else
' Add all other values to temp library
tmpLibraries.Add(library.Key, library.Value)
End If
Next
lstLibraries.Items.Clear() ' Clear the list box
Libraries = tmpLibraries ' Set dictionary Libraries equal to temp libararies
tmpLibraries = Nothing ' Set temp library back to nothing
populatelstLibrary() ' Repopulate the list box
End If
End If
End Sub
frmAssociationScreen.vb
Public Class frmAssociationScreen
Private Sub frmAssociationScreen_Load(sender As Object, e As EventArgs) Handles Me.Load
lstAllLibraries.Items.Clear()
For Each library In frmManager.Libraries
lstAllLibraries.Items.Add(library.Value & " --- " & library.Key)
Next
For Each book In frmManager.Books
lstAllBooks.Items.Add(book.Value & " --- " & book.Key)
Next
For Each nonBook In frmManager.nonBookMedia
lstAllMedia.Items.Add(nonBook.Value & " --- " & nonBook.Key)
Next
End Sub
Private Sub btnManagerScreen_Click(sender As Object, e As EventArgs) Handles btnManagerScreen.Click
Me.Close() ' Close current form
frmManager.Visible = True ' Make manager form visible
End Sub
Private Sub btnAddBook_Click(sender As Object, e As EventArgs) Handles btnAddBook.Click
End Sub
Private Sub btnRemoveBook_Click(sender As Object, e As EventArgs) Handles btnRemoveBook.Click
End Sub
Private Sub lstAllLibraries_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstAllLibraries.SelectedIndexChanged
End Sub
End Class
Some slight changes in your code as below:
In your structure LibraryWithMedia
We added SUB NEW
' Structure of single library
Structure LibraryWithMedia
'
Dim strLibraryName As String
Dim dicBooks As Dictionary(Of String, String)
Dim nonBookMedia As Dictionary(Of String, String)
'
'new library constructor
Sub New(ByVal LibName As String)
strLibraryName = LibName
dicBooks = New Dictionary(Of String, String)
nonBookMedia = New Dictionary(Of String, String)
End Sub
'
End Structure
In your EquippedLibrary declaration.
The declaration changed from (string, string) to simply LibraryWithMedia
Public EquippedLibrary As List(Of LibraryWithMedia)
At the end/bottom of your Form_Load event
' construct equipped library and define the library names
EquippedLibrary = New List(Of LibraryWithMedia)
' initialise each library with empty books/media dictionaries
populateEquippedLibNames
The PopulateEquippedLibNames Subroutines (this is a new subroutine)
Sub populateEquippedLibNames()
'
Dim Counta As Integer
Dim tmpSingleLib As LibraryWithMedia
'
For Counta = 0 To Libraries.Count - 1
tmpSingleLib = New LibraryWithMedia(Libraries.Values(Counta))
EquippedLibrary.Add(tmpSingleLib)
tmpSingleLib = Nothing
Next
'
End Sub
And then for adding/removing each book to the SELECTED library in the TOP listbox
Private Sub btnAddBook_Click(sender As Object, e As EventArgs) Handles btnAddBook.Click
'
EquippedLibrary(lstLibraries.SelectedIndex).dicBooks.Add(Books.Keys(lstBooks.SelectedIndex), Books.Values(lstBooks.SelectedIndex))
lstSelectedBooks.Items.Add(lstBooks.SelectedItem)
'
End Sub
Private Sub btnRemoveBook_Click(sender As Object, e As EventArgs) Handles btnRemoveBook.Click
'
EquippedLibrary(lstLibraries.SelectedIndex).dicBooks.Remove(Books.Keys(lstBooks.SelectedIndex))
'
End Sub
Note that to add a book/media to a library,
A library MUST BE selected in the TOP listbox
The Book or Media being added must also be selected
No error checking is performed, so you will need to add it (such as a listbox has a selection or not) etc
Update
I have added the libraries on change code for you below
Private Sub lstLibraries_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstLibraries.SelectedIndexChanged
'
Dim Counta As Integer
'
lstSelectedBooks.Items.Clear()
lstSelectedMedia.Items.Clear()
If EquippedLibrary(lstLibraries.SelectedIndex).dicBooks.Count > 0 Then
For Counta = 0 To EquippedLibrary(lstLibraries.SelectedIndex).dicBooks.Count - 1
lstSelectedBooks.Items.Add(EquippedLibrary(lstLibraries.SelectedIndex).dicBooks.Keys(Counta) & " --- " & EquippedLibrary(lstLibraries.SelectedIndex).dicBooks.Values(Counta))
Next
End If
Counta = Nothing
'
End Sub

VB.net ref value seem to not work

I'm passing a value by reference in this code
Private Sub login()
Dim login As New login(lListClients, bConnected)
login.ShowDialog()
login.Dispose()
If (bConnected = True) Then
Console.WriteLine("Mokmeuh")
Button3.Visible = True
Button4.Visible = True
Button7.Visible = True
End If
End Sub
And this is the login form
Public Class login
Private lListClients As List(Of Client)
Private bConnected As Boolean
Sub New(ByRef lListClients As List(Of Client), ByRef bConnected As Boolean)
InitializeComponent()
Me.lListClients = lListClients
Me.bConnected = bConnected
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim sID = TextBox1.Text, sPassword As String = TextBox2.Text
For Each cClient As Client In lListClients
If (Equals(cClient.getID, sID)) Then
If (Equals(cClient.getPassword, sPassword)) Then
bConnected = True
MessageBox.Show("Vous ĂȘtes connectĂ© vous pouvez continuez")
Me.Close()
End If
Else
MessageBox.Show("Votre ID n'existe pas")
TextBox1.Clear()
TextBox2.Clear()
TextBox1.Focus()
End If
Next
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
For Each m As Object In Me.Controls
If TypeOf m Is TextBox Then
CType(m, TextBox).Text = Nothing
End If
Next
Me.Close()
End Sub
Private Sub login_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Select()
End Sub
End Class
Whenever I launch it the bConnected value in form 1 is always false but in the login form it's true upon destruction so I'm really confuse here I've passed the value by reference it should, when set as true in the login.vb, be true as well in the form1.vb but thw condition If (bConnected = True) is never true.
So I need some help thank you
BTW : Sorry for my bad english
Although you can pass parameters by reference, you cannot store these references. If you want to change the parameter's value, you have to do it in the called method. Otherwise, the runtime cannot ensure that the variable is still alive.
A List(Of T) is already a reference type. So it is usually not reasonable to pass this parameter by reference. The variable lListClients holds a reference to the actual list object. When passing the variable by value, this reference is copied and passed to the method, resulting in another reference to the very same object. The only reason why you would want to pass this by reference is to change the variable's value from the called method, i.e. assign a new list.
The solution for your problem is quite simple. Create a public property:
Public Class login
Public Property Connected As Boolean
'...
Connected = True
'...
End Class
And use the login object to check the value:
If login.Connected Then
Of course, you should not dispose of the object until you check the value.

Using the Content of Textbox as a variable in VB (2012)

In the code below b textbox will contain the string "a.text" what I want b textbox to be the evaluation of the content of the string "a.text" which is the word Test. Please don't suggest:
b.text = a.text
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim t As String
a.Text = "Test"
t = "a.text"
b.Text = t
End Sub
End Class
Check out Controls collection of your form. You can find an item based on its name.
Also check out this answer
VB .NET Access a class property by string value
So, you could take your string, split it by the ".", find the control using the Controls Collection, and then get the property using the second half of your string using Reflection.
Of course, if you are just looking for the Text of the textbox, you just need to use the collection and forget the reflection. Like this..
For i As Integer = 1 To 25
.fields("Field" & i).value = Me.Controls("QAR" & i).Text
Next
You can do what you asked for using Reflection ... I'm not an enormous fan of it for something like this, but here's how it would look in your code:
Imports System.Reflection
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
a.Text = "Hello"
Dim t As String = "a.Text"
b.Text = DirectCast(SetValue(t), String)
End Sub
Public Function SetValue(ByVal name As String) As Object
Dim ctrl As Control = Me.Controls(name.Split("."c)(0))
Return ctrl.GetType().GetProperty(name.Split("."c)(1)).GetValue(ctrl, Nothing)
End Function
End Class
This would set textbox a's value to "Hello" and then copy that to textbox b using the reflection method.
Hope this helps!

trying to copy data from 1 listbox to another on a different form

I am having problems trying to copy data from one list box to another using multiple forms an a module for the global variables and arrays. At this point it is telling me that when I select and item form a secondary listbox and try sending it to my main list box it says that I am outside the bounds of the array. I have tried about every different method of doing this and Have not come up with the solution yet. I know it is probably something obvious that just hasn't hit me yet.
This is how I initiate my array to populate my secondary listbox with
Public mp3Albums() As String = {" Tres Hombres ZZ TOP ", " Fandango! ZZ TOP ", " Soul Kiss Tom Duda ", " Instrumental Telepathy Tom Duda ", " Dark Side of the Moon Pink Floyd ", " Seventh Sojourn Moody Blues ", " In Search of the lost Chord Moody Blues "}
the list box in my main form is called lstShoppingCart
This is how I populate and the button click action to try to copy the data to my main listbox. Ignore the call for the 2nd set of data trying to be copied since if I cannot copy just one then I have no business trying to copy 2 sets for a title and a price. unless it's simpler than i thought
Dim frmMain As New MainForm
frmMain.lstShoppingCart.Items.Add("A" & lstVinylAlbum.SelectedIndex & ": " & lstVinylAlbum.SelectedItem.ToString)
Start off with a small class representing an item in a list box:
Public Class ListItem
Private _key As String = String.Empty
Private _value As String = String.Empty
Public Sub New(ByVal key As String, ByVal value As String)
_key = key
_value = value
End Sub
Public ReadOnly Property Key() As String
Get
Return _key
End Get
End Property
Public ReadOnly Property Value() As String
Get
Return _value
End Get
End Property
' Prevents the object type from displaying in the list box.
Public Overrides Function ToString() As String
Return _value
End Function
End Class
Now, let's assume two ListBox objects on one form (lstLeft and lstRight), with two Buttons - one to move an item right and one to move an item left:
Public Class Form1
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
InitData()
End Sub
Private Sub InitData()
With lstLeft.Items
.Add(New ListItem("1", "Tres Hombres ZZ TOP"))
.Add(New ListItem("2", "Fandango! ZZ TOP"))
.Add(New ListItem("3", "Soul Kiss Tom Duda"))
.Add(New ListItem("4", "Instrumental Telepathy Tom Duda"))
End With
End Sub
Private Sub btnMoveRight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMoveRight.Click
If lstLeft.SelectedItem IsNot Nothing Then
Dim selectedItem As ListItem = DirectCast(lstLeft.SelectedItem, ListItem)
lstRight.Items.Add(selectedItem)
lstLeft.Items.Remove(selectedItem)
End If
End Sub
Private Sub btnMoveLeft_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMoveLeft.Click
If lstRight.SelectedItem IsNot Nothing Then
Dim selectedItem As ListItem = DirectCast(lstRight.SelectedItem, ListItem)
lstLeft.Items.Add(selectedItem)
lstRight.Items.Remove(selectedItem)
End If
End Sub
End Class
What this is doing is just adding a reference to the selected ListItem object to the "other" ListBox, and removing the reference to the selected ListItem obect from the current ListBox. If you want to do this between two separate forms, each form will need a Public Property AddToList(item As ListItem) function. First, call the function on the opposite form to add to that form's ListBox, and then remove the ListItem object from the current form.