I have this structure, with an array of the structure type.
Structure CustomerAccountsRec
Dim strFirstName As String
Dim strLastName As String
Dim intAge As Integer
Dim strAddress As String
Dim strTown As String
Dim strPostcode As String
Dim strCusNum As String
End Structure
Public strCusArray() As CustomerAccountsRec
I want to be able to take the strCusNum of the array and populate a combobox with it but can't figure out how. Any help?
You can also override the ToString Method in your Structure as mentioned. I also created a List(Of CustomerAccountsRec) that makes it a bit easier to add values and then I bound the list to the ComboBox's DataSource
Public Class Form1
Structure CustomerAccountsRec
Dim strFirstName As String
Dim strLastName As String
Dim intAge As Integer
Dim strAddress As String
Dim strTown As String
Dim strPostcode As String
Dim strCusNum As String
Public Overrides Function ToString() As String
Return strCusNum
End Function
End Structure
Public strCusArray As List(Of CustomerAccountsRec) = New List(Of CustomerAccountsRec)
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Dim carec As CustomerAccountsRec = New CustomerAccountsRec
carec.strFirstName = "Hello"
carec.strLastName = "World"
carec.strCusNum = "Hello World"
carec.strTown = "AnyTown"
carec.strAddress = "AnyStreet"
carec.strCusNum = "12345678"
strCusArray.Add(carec)
ComboBox1.DataSource = strCusArray
End Sub
End Class
You can add the items using the ComboBox.Items.Add method, and for the structure to be properly displayed, you have to override it's ToString method.
See:
ComboBox: Adding Text and Value to an Item (no Binding Source)
how to add value to combobox item
You can use LINQ to get an array containing the items you want to display, and then bind that array to the ComboBox.
Dim combo as New ComboBox
combo.DataSource = strCusArray.Select(Function(f) f.strCusNum).ToArray()
If I understand your question.
The actual result of Public strCusArray() As CustomerAccountsRec is null, so we can't use this to add the all items from CustomerAccountsRec to ComboBox
To List all the item from your Structure we need to use the System.Reflection Namespace
Structure CustomerAccountsRec
Dim strFirstName As String
Dim strLastName As String
Dim intAge As Integer
Dim strAddress As String
Dim strTown As String
Dim strPostcode As String
Dim strCusNum As String
End Structure
Dim fi As FieldInfo() = GetType(CustomerAccountsRec).GetFields(BindingFlags.[Public] Or BindingFlags.Instance)
For Each info As FieldInfo In fi
ComboBox2.Items.Add(info.Name)
Next
Source: C# version
Related
On executing the below code, I am successfully able to insert an array values into a Textbox AutoCompleteCustomSource.
But I need to be able to read all data back from the AutoCompleteCustomSource too, and put it into an array.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.AutoCompleteMode = AutoCompleteMode.Suggest
TextBox1.AutoCompleteSource = AutoCompleteSource.CustomSource
Dim Arr1 As String() = {"Hello", "How", "Are", "You"}
'Below line of code puts all the array values into TxtBox.AutoCompleteCustomSource
TextBox1.AutoCompleteCustomSource.AddRange(Arr1)
'-------------Need Help on Below-------
'How to read all data from TextBox1.AutoCompleteCustomSource and bring it into an array
Dim MyArr1
MyArr1 = TextBox1.AutoCompleteCustomSource??????????????????????????????????????
End Sub
You can throw a bit of LINQ at it:
Dim items = TextBox1.AutoCompleteCustomSource.Cast(Of String)().ToArray()
The AutoCompleteCustomSource is type AutoCompleteStringCollection, which implements IEnumerable but not IEnumerable(Of T), although every item is guaranteed to be a String. That means that you can call the Cast(Of String) extension method to get an IEnumerable(Of String) and then call ToArray on that to get a String array.
Other options include this:
Dim source = TextBox1.AutoCompleteCustomSource
Dim items(source.Count - 1) As String
source.CopyTo(items, 0)
or you can go really old-school with this:
Dim source = TextBox1.AutoCompleteCustomSource
Dim upperBound = source.Count - 1
Dim items(upperBound) As String
For i = 0 To upperBound
items(i) = source(i)
Next
I have a structure that has a string and 2 dictionary variable. I don't know how to insert data into these dictionaries.
Public Structure librariesWithMedia
Dim strLibraryName As String
Dim dicBooksMedia As SortedDictionary(Of String, String)
Dim dicNonBooksMedia As SortedDictionary(Of String, String)
End Structure
Dim libraryMediaEntry As librariesWithMedia
This is my structure and this is how I'm storing the values.
libraryMediaEntry.dicBooksMedia.Add(key, value)
This gives me a null reference exception error. Can anyone help me understand and how I would have to take the data?
Your Structure MUST initialise/instantiate the dictionaries before you can use them.
Public Structure librariesWithMedia
'
Dim strLibraryName As String
Dim dicBooksMedia As SortedDictionary(Of String, String)
Dim dicNonBooksMedia As SortedDictionary(Of String, String)
'
Sub New(LibName As String)
strLibraryName = LibName
dicBooksMedia = New SortedDictionary(Of String, String)
dicNonBooksMedia = New SortedDictionary(Of String, String)
End Sub
'
End Structure
'
Dim libraryMediaEntry As librariesWithMedia
And then in your code, for example
Sub DoSomethingProcess()
'
libraryMediaEntry = New librariesWithMedia("Featured_Books")
libraryMediaEntry.dicBooksMedia.Add("James A Michener", "Chesapeake")
'
End Sub
I have a populated listbox. Each item has a string of data with id's and values. How would i search for the id and receive the vale?
If i search for 'itemColor' i would like it to return each boot color in a new msgbox.
itemName="boots" itemCost="$39" itemColor="red"
itemName="boots" itemCost="$39" itemColor="green"
itemName="boots" itemCost="$39" itemColor="blue"
itemName="boots" itemCost="$39" itemColor="yellow"
I understand there are different and easier ways to do this but i need to do it this way.
Thanks!
Here's one way to do it involving parsing the text as XML:
' Here's Some Sample Text
Dim listboxText As String = "itemName=""boots"" itemCost=""$39"" itemColor=""red"""
' Load XML and Convert to an Object
Dim xmlDocument As New System.Xml.XmlDocument
xmlDocument.LoadXml("<item " & listboxText & "></item>")
Dim item = New With {.ItemName = xmlDocument.DocumentElement.Attributes("itemName").Value,
.ItemCost = xmlDocument.DocumentElement.Attributes("itemCost").Value,
.ItemColor = xmlDocument.DocumentElement.Attributes("itemColor").Value}
' Write It Out as a Test
Console.WriteLine(item.ItemName & " " & item.ItemCost & item.ItemColor)
Console.Read()
Create a class (or structure), the appropriate properties, a default constructor, a parametized constructor and an Override of .ToString.
Public Class Item
Public Property Name As String
Public Property Cost As String
Public Property Color As String
Public Sub New()
End Sub
Public Sub New(sName As String, sCost As String, sColor As String)
Name = sName
Cost = sCost
Color = sColor
End Sub
Public Overrides Function ToString() As String
Return $"{Name} - {Cost} - {Color}"
End Function
End Class
Item objects are added to the list box calling the parameterized constructor. The list box calls .ToString on the objects to determine what to display.
Private Sub FillList()
ListBox3.Items.Add(New Item("boots", "$39", "red"))
ListBox3.Items.Add(New Item("boots", "$39", "green"))
ListBox3.Items.Add(New Item("boots", "$39", "blue"))
ListBox3.Items.Add(New Item("boots", "$39", "yellow"))
End Sub
Since we added Item objects to the list box, we can cast each list item back to the Item type and access its properties.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim sb As New StringBuilder
For Each i In ListBox3.Items
sb.AppendLine(DirectCast(i, Item).Color)
Next
MessageBox.Show(sb.ToString)
End Sub
I have created a DLL file in VB.NET and I want use it in Excel VBA. When I use it like a function it is working perfect but when I use sub with a ByRef variable it does not work and Excel restarts with an error.
The code in VB.NET is:
Public Function distinctArr(ByVal arr As String()) As String()
Return arr.ToList.Distinct.ToArray
End Function
Public Sub sortArr(ByVal arr As String(), ByRef a As String())
Dim tolist As List(Of String) = arr.ToList
tolist.Sort()
a = tolist.ToArray
End Sub
This is the code in VBA:
Dim objMda As Excelcode.mda
Set objMda = New Excelcode.mda
Dim distinc_Item() As String
Dim all_Items() As String
all_Items = rng_to_string(rng_rizmetre)
distinc_Item = objMda.distinctArr(all_Items) '''This line is working perfect
Dim Sorted_Item() As String
objMda.sortArr distinc_Item, Sorted_Item
What is wrong with the code?
Finally i can find my answer.
code in vb.net
Public Class MainClass
Sub sortArr(ByVal arr As String(), ByRef sortedarr As String())
sortedarr = arr
Array.Sort(sortedarr)
End Sub
End Class
and code in excel vba:
Sub aaa()
Dim Mycode As excelcode.MainClass
Set Mycode = New excelcode.MainClass
Dim arr(2) As String
arr(0) = "m"
arr(1) = "a"
arr(2) = "d"
Dim sortedArr() As String
ReDim sortedArr(0)
Mycode.sortArr arr, sortedArr
End Sub
by this code i can pass array byval to vb.net dll then vb.net pass sorted array.
I'm new to custom classes. Here is my class definition:
Public Class game
Private strName As String()
Property name As String()
Get
Return strName
End Get
Set(ByVal Value As String())
strName = Value
End Set
End Property
End Class
And here is my code to read from a file and create an instance of "game"
Public Sub loadGames()
Dim game As New game
Dim dir As New IO.DirectoryInfo(gameFolder)
Dim fs As IO.FileInfo() = dir.GetFiles("*.gemui")
Dim f As IO.FileInfo
For Each f In fs
Dim path As String = f.FullName
Dim fi As New FileInfo(path)
Dim sr As StreamReader = fi.OpenText()
Dim s As String = ""
While sr.EndOfStream = False
game.name = sr.ReadLine() '"Value of type 'String' cannot be converted to '1-dimensional array of String'."
MsgBox(sr.ReadLine()) 'shows a message box with exactly what I expect to see
End While
sr.Close()
Next
End Sub
game.name = sr.ReadLine() is the problem. "Value of type 'String' cannot be converted to '1-dimensional array of String'."
Your problem is that in your class definition you are not declaring strings, you are declaring arrays of strings. Corrected code is:
Public Class game
Private strName As String
Property name As String
Get
Return strName
End Get
Set(ByVal Value As String)
strName = Value
End Set
End Property
End Class
or more simply in .net 4.0+
Public Class game
Property name As String
End Class
in this case the private variable is called _name