passing all the items of listbox in the richtextbox - vb.net

how can I pass the items of listbox to richtextbox?. I have multiple items and I want it to display in the richtextbox. this items is like a receipt. it display the items along with its amount.

This is just a way to do it, using LINQ:
Dim Items As String() =
From s As String In ListBox1.Items
RichTextBox1.Text = String.Join(Environment.NewLine, Items)

One of the problems doing this with a listbox, is that listboxes store the items as objects and uses the ToString method of each object to display a representation of that object.
Something like this should work:
RichTextBox1.Lines = (From o In ListBox1.Items
Let ostr = o.ToString
Select ostr).ToArray
This way if you use objects that can't be implicitly cast as string it will still work. Or, if you use custom objects all that's required is a ToString override in the class.

Related

Value of type List(Of Long) cannot be converted to List(Of ListItem)

I am filling data from removeActiveService (list of listItem) into a new list listRemove. However listRemove is turning into a list of Long.
Dim listRemove = removeActiveService.Select(Function(item) item.Text.Replace(serviceRemove, "") And item.Text.Split("-"c)(0).Trim And item.Value.Split("-"c)(1).Trim).ToList()
If I change it to Dim listRemove As List(Of ListItem), it results in the error
Value of type List(Of Long) cannot be converted to List(Of ListItem)
I need to perform replace and split on the text and value (see my code). What is the correct syntax here so that it can be a List(Of ListItem)?
EDIT
Starting with checkboxlist items in removeActiveService
Copy that into a brand new list of items called listRemove.
I need to perform these on the TEXT of the list items in listRemove
item.Text.Replace(serviceRemove, "")
item.Text.Split("-"c)(0).Trim
And I need to perform this on the VALUE of the list items in listRemove
item.Value.Split("-"c)(1).Trim
If you expect to output a List(Of ListItem) then you need to create ListItem objects somewhere, which you're not doing now. You're performing operations on the input data but you're not doing anything useful with the results. And doesn't do anything that could be considered useful there. If you expect to output a list of ListItem objects containing the results of those operations then you actually have to create ListItem objects containing the result of those operations. As far as I can tell, this would be the way to go:
Dim listRemove = removeActiveService.Select(Function(item) New ListItem With
{
.Text = item.Text.Replace(serviceRemove, "").Split("-"c)(0).Trim(),
.Value = item.Value.Split("-"c)(1).Trim()
}).
ToList()

How to get selected items from listbox for SQL where statement

How do you get the selected items from the listbox so that I can use it on my SQL where statement... selected items wont work because it shows datarowview.. I've tried using
Dim Studente As String = ListBox1.GetItemText(ListBox1.SelectedItems())
But it didn't do anything
To be specific:
Dim students As String() = ListBox1.SelectedItems.
Cast(Of Object)().
Select(Functions(item) ListBox1.GetItemText(item)).
ToArray()
Without LINQ:
Dim students As New List(Of String)
For Each item In ListBox1.SelectedItems
students.Add(ListBox1.GetItemText(item))
Next
You can then call ToArray on that List if you specifically need an array.

Cannot use AddRange to add a list of items to another list

In my main method I have this and a few more hard coded ones:
Dim dropdownItems = New ListItemCollection
dropdownItems.Add(New ListItem With {.Value = ""})
Then in that method I call another method like this which I call in my main method. If it is not returning null then I want to do this:
Dim items = Me.GetMoreStuff()
If items IsNot Nothing Then
dropdownItems.AddRange(items)
End Id
And that other method that I am calling it something like this:
Private Function GetMoreStuff() As ListItemCollection
' some more of those items.
Return dropdownItems
End Function
But I am getting a Cannot Covert Type error. Why?
Based on the documentation, AddRange takes an array of ListItem, not a ListItemCollection. You'll have to convert your ListItemCollection to an array or just return an array (or a list) from GetMoreStuff.
You can also convert using
items.Cast(Of ListItem).ToArray()

Possible to add some sort of identification onto list items?

I'm just curious if it's possible to gather a list of data and put it into a list or array and easily pull them out using some variable(like a tag property on controls) on each item in the list rather than using the index. An example would be if i have many controls on a form and would like to populate the values using the list but instead of having to cross check a lot of different indicies(would take a lot of time) i could just assign the label to some 'tag like' variable for the list item.
Is this possible?
Lists in .NET, such as the List(Of T) class, only support storing one object per item. However, the beauty of them is, you can store any type of object that you want. If you need to store metadata with your data, simply create a new class that holds all the data for each item. For instance:
Public Class MyControlData
Public Property LabelText As String
Public Property Value As String
End Class
Then you can add the items to the list like this:
Dim dataList As New List(Of MyControlData)()
Dim item As New MyControlData()
item.LabelText = "Name"
item.Value = "Bob"
dataList.Add(item)
And you can read the data from the list like this:
For Each i As MyControlData in dataList
Label1.Text = i.LabelText
TextBox1.Text = i.Value
Next
I would use a Dictionary object so you can use a key to reference your data easily.
You can use LINQ:
Dim myTagControls = From ctrl In Me.Controls.Cast(Of Control)()
Where "your-tag".Equals(ctrl.Tag)
For Each ctrl In myTagControls
Console.WriteLine("Tag:{0} Name:{1}", ctrl.Tag, ctrl.Name)
Next

Cast(Of ?) from UIElement

In silverlight my custom controls are in theUIElementCollection of my StackPanel. I want to get a list of them by a specific value. There only DivElements in the container. It returns Nothing when I know I have one or more. I know I can make a simple loop and cast types inline, but I want to get better with LINQ and Cast(Of TResult). My attempt at casting:
Dim myList = TryCast(spDivs.Children.Where(Function(o) DirectCast(o, DivElement).ElementParent Is bComm).Cast(Of DivElement)(), List(Of DivElement))
The problem is you can't cast into a List(Of DivElement). The collection is a UIElementCollection, not a List(Of T).
You could build a new list, though. This can also be simplified by using OfType instead of casting manually:
Dim myList = spDivs.Children.OfType(Of DivElement)()
.Where(Function(o) o.ElementParent Is bComm)
.ToList()