how to populate listbox with array items - vb.net

Im trying to populate a listbox with items inside the array, i have declared an array and assigned strings which it holds but im not sure if i even done that correctly, i want to use that strings that are in the array to populate a list box, here is the code i have already done, how do i do that, could anyone give me code that i could use to populate listbox with those strings.
Dim NewDefinition As String
NewDefinition = InputBox(" Please enter definition in the box and click OK. " & " The definition entered will be added to the list. ", " Add Definition")
lstDefinitions.Items.Add(NewDefinition)
Dim NewDefinition1 As String = lstDefinitions.Items(0).ToString
Dim NewDefinition2 As String = lstDefinitions.Items(1).ToString
Dim NewDefinition3 As String = lstDefinitions.Items(2).ToString
Dim NewDefinition4 As String = lstDefinitions.Items(3).ToString
Dim NewDefinition5 As String = lstDefinitions.Items(4).ToString
Dim NewDefinition6 As String = lstDefinitions.Items(5).ToString
Dim NewDefinition7 As String = lstDefinitions.Items(6).ToString
Dim NewDefinition8 As String = lstDefinitions.Items(7).ToString
Dim NewDefinition9 As String = lstDefinitions.Items(8).ToString
Dim NewDefinition10 As String = lstDefinitions.Items(9).ToString
Dim NewDefinitions(10) As String
NewDefinitions(0) = NewDefinition1
NewDefinitions(1) = NewDefinition2
NewDefinitions(2) = NewDefinition3
NewDefinitions(3) = NewDefinition4
NewDefinitions(4) = NewDefinition5
NewDefinitions(5) = NewDefinition6
NewDefinitions(6) = NewDefinition7
NewDefinitions(7) = NewDefinition8
NewDefinitions(8) = NewDefinition9
NewDefinitions(9) = NewDefinition10

Listbox will accept any object and will display whatever the objects ToString method will display. Therefore you can populate the listbox with the objects directly. See if this works for you:
ListBox1.DataSource = lstDefinitions.Items
If the objects in question are from a custom class you can override the ToString method to display the information you desire.

Related

About Casting in a Class property in VB.Net

Is it possible to cast a sting in a class property?
what i want to do is that from 'My' class i can fetch the Form's Text properties. but for every form i have to provide the form name statically. I want to do it dyanamically. What i tried is here
Dim frmName As New Object
frmName = Name.ToString()
Dim frmProperty As String
frmProperty = "My.Forms." & frmName & ".Text"
frmNameLabelControl.Text = frmProperty
but at the Front-End it displays 'My.Forms.Form1.Text'
I think that all you need is the following code. But you need to create the equivalent of frmNameLabelControl in the same order in every form. For example to be the last label you created in every form
Dim FormsCount As Integer = My.Application.OpenForms.Count
Dim frmProperty As String
Dim lblContrIndex As Integer = 0 ' Number of label creation in descending order
For i As Integer = 0 To FormsCount - 1
frmProperty = My.Application.OpenForms.Item(i).Text
My.Application.OpenForms.Item(i).Controls.OfType(Of Label).ElementAt(lblContrIndex).Text = frmProperty
Next

Getting data from dataset

I have got a problem, I have got a filled dataset, but now i need to get a Column value from it and store it into a variable. On Button 2 click i fill it with this:
tbaGridview.Fill(BlGridview1.vwBLcontainerCargo, Bookingnumber)
Now i want to get the data from it that is inside. I have got a for loop:
For i As Integer = 0 To GridView1.DataRowCount - 1
Dim OriginSealNumber As String = BlGridview1.vwBLcontainerCargo.Tables("SEALNUMBER").Rows(i).Item(0)
Next i
But it says Tables is not a member of windowsapplication1.blgridView.vwBLContainerCargoDataTable. How do i get the data for each column??
To reference the GridView's table use this sintax:
((DataRowView)GridView1.Rows[0].DataBoundItem).DataView.Table
But I recommend that you use a BindingSource object and iterate through it.
I fixed it myself by using this:
For i As Integer = 0 To GridView1.DataRowCount - 1
Dim OriginSealNumber As String = BlGridview1.Tables(0).Rows(i)("SEALNUMBER").ToString()
Dim OriginContainerNumber As String = BlGridview1.Tables(0).Rows(i)("CONTAINERNUMBER").ToString()
Dim OriginContainerType As String = BlGridview1.Tables(0).Rows(i)("CONTAINERTYPE").ToString()
Dim OriginQuantity As String = BlGridview1.Tables(0).Rows(i)("QUANTITY").ToString()
Dim OriginPackageType As String = BlGridview1.Tables(0).Rows(i)("PACKAGETYPE").ToString()
Dim OriginDescription As String = BlGridview1.Tables(0).Rows(i)("DESCRIPTION").ToString()
Dim OriginWeight As String = BlGridview1.Tables(0).Rows(i)("WEIGHT").ToString()
Next i

Get ValueMember of Selected item in ListBox

I've seen a couple of posts asking a similar question but I have not been able to duplicate the answers in my code successfully.
The following code adds items and their value member to a list box.
Public Shared Sub ListFiles(hTab As Hashtable)
Debug.Print("create file and key" & Now)
Dim Enumerator As IDictionaryEnumerator
Enumerator = hTab.GetEnumerator()
Dim MyKeys As ICollection
Dim Key As Object
MyKeys = hTab.Keys()
If (hTab.Count > 0) Then
For Each Key In MyKeys
Dim sfileName As String = hTab(Key)
Dim first As Integer = sfileName.IndexOf("_")
Dim last As Integer = sfileName.LastIndexOfAny("_")
Dim first2 = (first + 1)
Dim splitFile = sfileName.Substring(first2)
frmViewFiles.ListBox1.Items.Add(splitFile)
frmViewFiles.ListBox1.ValueMember = Key
frmViewFiles.ListBox1.SelectedValue = Key
Next
End If
End Sub
When I run my code to get the selected items value member
Dim file = ListBox1.ValueMember.ToString()
I can acess the first item I choose but subsequent selections dont change the value member to that of the selected item.
Please direct me.
Thank you for your answers. this is my new code:
Public Shared Sub runListFiles(CustomerId As String)
Dim cfp As New CloudFilesProvider(cloudId)
Dim containerObjectList As IEnumerable(Of ContainerObject) = cfp.ListObjects(container:="EstherTest", identity:=cloudId, prefix:=CustomerId & "_")
For Each file As ContainerObject In containerObjectList
Dim sFullFileName As String = file.Name
Dim first As Integer = sFullFileName.IndexOf("_")
Dim first2 = (first + 1)
Dim splitFile = sFullFileName.Substring(first2)
'frmViewFiles.ListBox1.Items.Add(splitFile)
'frmViewFiles.ListBox1.ValueMember = sFullFileName
Dim fb = New myFile
fb.FileName = splitFile
fb.FullPath = sFullFileName
frmViewFiles.ListBox1.Items.Add(fb)
frmViewFiles.ListBox1.DisplayMember = fb.FileName
frmViewFiles.ListBox1.ValueMember = fb.FullPath
This is my class:
Public Class myFile
Public Property FileName As String
Public Property FullPath As String
Public Sub New(f As String, b As String)
FileName = f
FullPath = b
End Sub
End Class
Please see my comment below and assist
ValueMember is supposed to indicate the property name of an object added to the Items collection: the property to use as the actual value for the items in the ListControl.
You are not adding objects to the control, so Key from the hashtable is meaningless as the ValueMember. Your post references a file variable in passing, so I will assume this revolves around showing the filename while wanting to get the full pathname when selected/clicked. WebForms/Winforms/WPF was not indicated, I am assuming WinForms:
Public Class myFile
Public Property FileName As String
Public Property FullPath As String
Public Property FileSize As Int64 ' just so there is something else
Public Sub New(f as String, p as String, s as Int64)
FileName = f
FullPath = b
FileSize = s
End Sub
End Class
Lets say we want to add some of these to a ListBox, for each item added we want FileName to display as the text, but want to get them back by FullPath:
Dim f As myFile
' assume these come from a fileinfo
For Each fi as FileInfo in DirectoryInfo.GetFiles(searchFor)
f = New myFile
f.FileName = fi.Name
f.FullPath = fi.FullPath
f.FileSize = fi.Length
' myFile accepts all the prop values in the constructor
' so creating a new one could also be written as:
' f = New myFile(fi.Name, fi.FullPath, fi.Length)
myListBox.Items.Add(f)
Next n
If the myFile objects were stored to a List(of myFile) rather than adding them to the control, we can bind the List as the DataSource and not have to iterate or copy:
mylistBox.DataSource = myFileList
Either way, Display- and ValueMember refer to the property names we wish to use:
myListBox.DisplayMember = "FileName" ' indicate property name of obj to SHOW
myListBox.ValueMember = "FullPath" ' prop name of object to return
When you select a listbox item, myListBox.SelectedValue would refer to the FullPath of the myFile object clicked on. The SelectedIndex would still refer to the index of the item in the list.
tl;dr
ValueMember and DisplayMember refers to the Property Names of Objects represented in the list.
Note:
I know this is many years later, but still relevant information.
It took me a while to parse what was said above, until I grokked it fully, so I thought it might help if I restated it slightly.
When you select a listbox item,
myListBox.SelectedValue is the contents of the field, myListBox.ValueMember. ValueMember contains the Field Name, SelectedValue contains the contents of the field.
myListBox.SelectedItem is the contents of the field myListBox.DisplayMember. DisplayMember contains the field name and SelectedItem contains the value of the field.
The SelectedIndex refers to the index of the item in the list. To see which item is selected, reference myListBox.SelectedIndex. You can, for example, change the selection to the last item in the list by using myListBox.SelectedIndex = myListBox.Items.Count - 1
If you want to display the values, then
Console.WriteLine("The value of {0} is {1}",myListBoxDisplayMember,myListBox.SelectedItem)
Console.WriteLine("The Value of {0} is {1}",myListBox.ValueMember,myListBox.SelectedValue)

Adding multiple strings to a listbox from text

I have a text file I need to get multiple strings from, I can sort of do it but it only shows the first string in the listbox
When we tried with XML reader it was collecting everything in the XML that was tagged Object Identifier along with the cameras
I need to look for lines similar to the following, there could be any amount
Object Identifier="./Cameras/MyCamera" Label="Standard Camera" Name="MyCamera" Type="Camera"
key identifiers:
./Cameras/
Label="Standard Camera"
Type="Camera"
I could use "MyCamera" after ./Cameras/ or Name="MyCamera" both of these are common in each occurrence of the lines
in my example below it has the file I wish to read it should list 3 cameras
https://www.dropbox.com/s/dy7r2auf9vv0m7g/testvb.zip
The XML is generated by Thea render, its the scene file with the model, lights etc taken out so it just leaves cameras and some core settings
Thanks to varocarbas, this is the code that solves my problem:
Dim path As String = "C:\Users\jen\Desktop\test\temp.xml"
Dim settings As System.Xml.XmlReaderSettings = New System.Xml.XmlReaderSettings()
settings.ConformanceLevel = System.Xml.ConformanceLevel.Fragment
Using reader As System.Xml.XmlReader = System.Xml.XmlReader.Create(path)
While (reader.Read())
If (reader.NodeType = System.Xml.XmlNodeType.Element) Then
If (reader.Name = "Object") Then
'Object Identifier="./Cameras/MyCamera" Label="Standard Camera" Name="MyCamera" Type="Camera"
Dim Identifier As String = reader.GetAttribute("Identifier") '"./Cameras/MyCamera"
Dim Label As String = reader.GetAttribute("Label") '"Standard Camera"
Dim Name As String = reader.GetAttribute("Name") '"MyCamera"
Dim Type As String = reader.GetAttribute("Type") '"Camera"
Dim wholeString As String = Name 'WHOLE STRING TO BE ADDED TO THE LISTBOX
'Adding the string to ListBox1
If (wholeString.Trim.Length > 0) And Type = "Camera" Then
ListBox1.Items.Add(wholeString)
End If
End If
End If
End While
End Using
You can use the XMLReader I proposed in another answer and do the following modifications on it:
Dim path As String = "temp.txt"
Dim settings As System.Xml.XmlReaderSettings = New System.Xml.XmlReaderSettings()
settings.ConformanceLevel = System.Xml.ConformanceLevel.Fragment
Using reader As System.Xml.XmlReader = System.Xml.XmlReader.Create(path)
While (reader.Read())
If (reader.NodeType = System.Xml.XmlNodeType.Element) Then
If (reader.Name = "Object") Then
'Object Identifier="./Cameras/MyCamera" Label="Standard Camera" Name="MyCamera" Type="Camera"
Dim Identifier As String = reader.GetAttribute("Identifier") '"./Cameras/MyCamera"
Dim Label As String = reader.GetAttribute("Label") '"Standard Camera"
Dim Name As String = reader.GetAttribute("Name") '"MyCamera"
Dim Type As String = reader.GetAttribute("Type") '"Camera"
Dim wholeString As String = Identifier & " - " & Label & " - " & Name & " - " & Type 'WHOLE STRING TO BE ADDED TO THE LISTBOX
'Adding the string to ListBox1
If (wholeString.Trim.Length > 0) Then
ListBox1.Items.Add(wholeString)
End If
End If
End If
End While
End Using
This code retrieves all the information you want and stores it in LisBox1 by putting " - " to separate each element. This is information more than enough and you should be the one performing any further change, for example: converting "./Cameras/MyCamera" into "./Cameras/" (there is an indication of how to do that in my previous code); or change the way the different items are displayed in the listBox (or perhaps you want to include one listBox per element: one for identifiers, another for labels, etc.).

VB.net Dictionary loop

Hey all i am in need of some help looping thru my Dictionary list. I can not seem to find the correct syntax in order to do so.
Here is my code:
Dim all = New Dictionary(Of String, Object)()
Dim info = New Dictionary(Of String, Object)()
Dim theShows As String = String.Empty
info!Logo = channel.SelectSingleNode(".//img").Attributes("src").Value
info!Channel = .SelectSingleNode("channel.//span[#class='channel']").ChildNodes(1).ChildNodes(0).InnerText
info!Station = .SelectSingleNode("channel.//span[#class='channel']").ChildNodes(1).ChildNodes(2).InnerText
info!Shows = From tag In channel.SelectNodes(".//a[#class='thickbox']")
Select New With {channel.Show = tag.Attributes("title").Value, channel.Link = tag.Attributes("href").Value}
all.Add(info!Station, info.Item("Shows"))
theShows = all.Item("Shows") '<--Doesnt work...
I just want to extract whatever is in "Shows" from the all dictionary.
Your code,
all.Add(info!Station, info.Item("Shows"))
theShows = all.Item("Shows")
The value of info!Station is being used as the KEY value in the all dictionary. Then you attempt to access the value using the constant string "Shows". I'm not sure what your intention was but
theShows = all.Item(info!Station)
should return the value of Shows that was stored using the Key info!Station.
If you want the list of shows as a string, you can do this,
Dim Shows as String = ""
For Each item in theShows
Shows &= item.Show & vbNewLine
Next
You can loop like this
For Each pair As KeyValuePair(Of String, String) In dict
MsgBox(pair.Key & " - " & pair.Value)
Next
source : VB.Net Dictionary
Winston