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.).
Related
I am trying to upload a csv file that has two records in it.
The below code executed for two times and third time I got this exception----Conversion of type string " " to double not valid
I put a debugger where I found the values of two colums of excel sheet but third time I am getting this exception. Your help is highly appreciated.
Below is the code.
Public Function GetLocationInformation(stream As Stream) As List(Of CsvPropLocation) _
Implements ICsvHandling.GetLocationInformation
If stream Is Nothing Then Return Nothing
Dim locations = New List(Of CsvPropLocation)
Using reader = New StreamReader(stream)
Dim config = New CsvConfiguration(CultureInfo.InvariantCulture) With {
.HasHeaderRecord = True,
.IgnoreBlankLines = False
}
Using csv = New CsvReader(reader, config)
Using dataReader As New CsvDataReader(csv)
Dim dt = New DataTable()
dt.Load(dataReader)
For Each row As DataRow In dt.Rows
Dim propLocation As CsvPropLocation
'find or create a propLocation
If locations.Any(Function(x) x.nLocationNumber = row("LocNum")) Then ######Got exception here ######
propLocation = locations.First(Function(x) x.nLocationNumber = row("LocNum"))
Else
propLocation = New CsvPropLocation(row("LocNum"))
locations.Add(propLocation)
End If
'do building stuff.
Dim building = ParseRowIntoBuilding(row)
propLocation.AddBuilding(building)
Next
End Using
End Using
End Using
Return locations
End Function
Change your line to
Dim number As Double
If Double.TryParse(row("LocNum"), number ) AndAlso locations.Any(Function(x) x.nLocationNumber = number )
This way you make sure number will be evaluated only if it gets a valid value from row("LocNum")
Also keep in mind the Else part must be controlled as New CsvPropLocation(row("LocNum")) probably is expecting a valid Double which isnt inside locations so, change to:
Else If Double.TryParse(row("LocNum"), number ) 'You can check against number = 0
'if zero isn't a valid value for number
'(or initialize number to a known invalid value and check against it)
'if yuo didn't want a double try parse
propLocation = New CsvPropLocation(number)
locations.Add(propLocation)
End If
I am in need of assistance... i am trying to create a textfile with links in it. the code i have..
dim domain as string = "http://www.mywebsite/"
dim name as string = "username"
Dim link As String = New String("domain" & "name")
TextBox1.AppendText(link & Environment.NewLine)
Msgbox(textBox1.lines(0))
The problem is that MsgBox only shows up as "http://www.mywebsite/". the textbox does show "http://www.mywebsite/username" but when copied to text document it is:
Line0: http://www.mywebsite/
Line1:username
any ideas... tried using
Dim link As String = String.Join(domain & name) but that doesnt work nor does
Dim link As String = new String.Join(domain & name)
i need
Msgbox(textBox1.lines(0)) to display "http://www.mywebsite/username" not one or the other.
That was quick got a message saying to use. Dim link As String = String.Concat(domain & name)
i think you should move to StringBuilder first import Imports System.Text
'create a string with multiple lines
Dim a As New StringBuilder
a.AppendLine("hi")
a.AppendLine("there")
a.AppendLine("this")
a.AppendLine("is")
a.AppendLine("a")
a.AppendLine("test")
'read will makes read line by line
Dim read As String() = a.ToString.Split(vbNewLine)
'count has number of lines
Dim count As Integer = a.ToString().Split(vbNewLine).Length - 1
'lines will be added to combobox one by one
For i As Integer = 0 To count - 1
ComboBox1.Items.Add(read(i))
Next
you just should edit it to suits your needs i didnt understand what you needed exactly
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)
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.
I am currently trying to read in an XML node from a filepath which I pass to my method.
Public Function ReadXMLForIsite(xmlFileName As String)
Dim IsitePath As String
Dim doc As New XPathDocument(xmlFileName)
Dim nav As XPathNavigator
Dim iter As XPathNodeIterator
nav = doc.CreateNavigator
iter = nav.Select("GovTalkMessage/Header") 'Node name
'Loop through the records in that node
While iter.MoveNext
Dim lstNav As XPathNavigator
'Get the data we need from the node
Dim iterNews As XPathNodeIterator
lstNav = iter.Current
iterNews = lstNav.SelectDescendants(XPathNodeType.Element, False)
'Loop through the child nodes
While iterNews.MoveNext
Debug.WriteLine(iterNews.Current.Name & ": " & iterNews.Current.Value)
End While
End While
Return IsitePath
End Function
Every time i run this method (even with different node names) the variable 'iter' states that 'debugger display proxy is a type and cannot be used as an expression'. This occurs just before the while statement, therefore does not go in. Any help would be much appreciated. Thanks!
Try to load your Xml into an XDocument.
Dim xdoc As XDocument = XDocument.Load("X:\Jacob.Freeman\RTI\TestXML\0001R.xml")
Dim headerNode = xdoc.Descendants.First(Function(x) x.Name.LocalName = "Header")
For Each desc In headerNode.Descendants()
Console.WriteLine(desc.Name.LocalName + ":" + desc.Value)
Next
Console.ReadLine()