How To Add Selected Items From A Listbox To Another Listbox Vba
- Remove From My Forums

-
Question
-
Tried to use this lawmaking to add the selected item from ListBox1 to ListBox2:
ListBox2.Items.Add(ListBox1.SelectedItem)
Merely information technology didn't work - nil happens.
Can anyone assistance me fix it?
Eddie
All replies
-
Hi
Endeavor this (SelectedItem is an Object so catechumen to string)
ListBox2.Items.Add(ListBox1.SelectedItem.ToString)
Regards Les, Livingston, Scotland
- Proposed as answer past Thursday, February 20, 2020 6:42 PM
-
If both ListBoxes are populated with strings
Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click If ListBox1.SelectedIndex > -1 Then ListBox2.Items.Add(ListBox1.Text) Stop If Finish Sub End Course
Delight remember to mark the replies equally answers if they aid and unmarked them if they provide no assist, this volition help others who are looking for solutions to the same or similar trouble. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile merely volition not answer coding question on either.
NuGet BaseConnectionLibrary for database connections.
StackOverFlow
-
Here is an instance if the ListBoxes have their data source set, in this instance to a List(Of Person).
Imports Arrangement.ComponentModel Imports Organisation.Runtime.CompilerServices Public Form Person Implements INotifyPropertyChanged Individual _firstName1 As String Private _lastName1 Equally Cord Public Property FirstName() As String Become Return _firstName1 End Get Set _firstName1 = Value OnPropertyChanged() End Set End Property Public Property LastName() Every bit String Go Return _lastName1 Finish Get Set _lastName1 = Value OnPropertyChanged() Terminate Set End Holding Public Overrides Part ToString() As String Return $"{FirstName} {LastName}" End Part Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged Protected Overridable Sub OnPropertyChanged(<CallerMemberName> Optional ByVal propertyName Every bit String = Nothing) PropertyChangedEvent?.Invoke(Me, New PropertyChangedEventArgs(propertyName)) End Sub End Class
Quick and dirty implementation significant it can be cleaned up but works as is
Imports System.ComponentModel Public Class Form1 Private _bindingListForListBox1 As New BindingList(Of Person)() Private ReadOnly _bindingListForListBox2 Every bit New BindingList(Of Person)() Private Sub Button1_Click(sender Equally Object, e As EventArgs) Handles Button1.Click If ListBox1.SelectedIndex > -one And then _bindingListForListBox2.Add(CType(ListBox1.SelectedItem, Person)) Cease If End Sub Individual Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim people As New Listing(Of Person) From { New Person() With {.FirstName = "Karen", .LastName = "Payne"}, New Person() With {.FirstName = "Anne", .LastName = "Jones"}, New Person() With {.FirstName = "Bob", .LastName = "Smith"}} _bindingListForListBox1 = New BindingList(Of Person)(people) ListBox1.DataSource = people ListBox2.DataSource = _bindingListForListBox2 Finish Sub End Course
Please remember to marker the replies as answers if they assistance and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar trouble. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN contour but will not answer coding question on either.
NuGet BaseConnectionLibrary for database connections.
StackOverFlow
-
Sorry, yes, the one I want to re-create from has a DataSet connected, the one I want to copy to has not. Is at that place a simplified version of how I tin can go this work, every bit I am a beginner? Should I make the one I am a copying to have a dataset connexion as well, so I can use the lawmaking yous gave me for dataset connections?
Thank you for the help so far,
Eddie
- Edited by EddiesTech Thursday, Feb twenty, 2020 vi:56 PM
-
Pitiful, yes, the one I desire to copy from has a DataSet continued, the one I want to copy to has not. Is there a simplified version of how I tin can get this work, equally I am a beginner? Should I make the one I am a copying to have a dataset connection too, so I can utilise the code you gave me for dataset connections?
Thank you for the aid so far,
Eddie
In the futurity be more specific as the post-obit works with a DataTable to perform the same operation as with a list but is handled differently.
Public Class Form1 Individual Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click If ListBox1.SelectedIndex > -one Then CType(ListBox2.DataSource, DataTable). Rows.Add(CType(ListBox1.SelectedItem, DataRowView). Row.ItemArray) End If End Sub Private Sub Form1_Load(sender Every bit Object, e As EventArgs) Handles MyBase.Load Dim dt = DataOperations.GetMockedData Dim dt1 = dt.Clone() ListBox1.DataSource = dt ListBox1.DisplayMember = "FullName" ListBox2.DataSource = dt1 ListBox2.DisplayMember = "FullName" End Sub Cease Course ''' <summary> ''' Imagine this data came from a database tabular array. ''' Nosotros use DataTable object as there is zero reason for a DataSet ''' </summary> Public Class DataOperations Public Shared Office GetMockedData() As DataTable Dim dt As New DataTable dt.Columns.Add together(New DataColumn() With {.ColumnName = "FirstName", .DataType = GetType(Cord)}) dt.Columns.Add(New DataColumn() With {.ColumnName = "LastName", .DataType = GetType(String)}) dt.Columns.Add(New DataColumn() With {.ColumnName = "FullName", .DataType = GetType(String), .Expression = "FirstName + ' ' + LastName"}) dt.Rows.Add(New Object() {"Karen", "Payne"}) dt.Rows.Add together(New Object() {"Anne", "Jones"}) dt.Rows.Add together(New Object() {"Bob", "Smith"}) Return dt Stop Role End Class
Please call up to marker the replies every bit answers if they help and unmarked them if they provide no assist, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
NuGet BaseConnectionLibrary for database connections.
StackOverFlow
- Edited by Kareninstructor MVP Thursday, February twenty, 2020 viii:07 PM
-
Hi EddiesTech,
Did y'all solve your problem? If your question has been answered then please click the "Mark equally Answer" Link at the bottom of the correct post(south), so that information technology will help other members to find the solution rapidly if they face a like issue.
Best Regards,
Xingyu Zhao
MSDN Community Support
Please call up to click "Mark equally Answer" the responses that resolved your issue, and to click "Unmark every bit Reply" if not. This tin be beneficial to other community members reading this thread. If you take any compliments or complaints to MSDN Support, feel gratuitous to contact MSDNFSF@microsoft.com.
How To Add Selected Items From A Listbox To Another Listbox Vba,
Source: https://social.msdn.microsoft.com/Forums/en-US/5d1f5e58-244a-4775-a96b-2e1c7bd7fdfa/how-to-copy-a-selected-item-from-one-listbox-to-another-vbnet?forum=vbgeneral
Posted by: ackermanpubleausing1955.blogspot.com
0 Response to "How To Add Selected Items From A Listbox To Another Listbox Vba"
Post a Comment