fedeteka fedeteka - 1 year ago 87
Vb.net Question

3D array loop and get the first dimension value

A 3D array like this

Dim MyFonts =
{"Arial", "arial.ttf", "arialbd.ttf"},
{"Calibri", "calibri.ttf", "calibribd.ttf"},
{"Candara", "Candara.ttf", "Candarab.ttf"},
{"Comic Sans MS", "comic.ttf", "comicbd.ttf"},
{"Consolas", "consola.ttf", "consolab.ttf"},
{"Constantia", "constant.ttf", "constantb.ttf"},
{"Courier New","cour.ttf", "courbd.ttf"},
{"Georgia", "georgia.ttf", "georgiab.ttf"},
{"Impact", "impact.ttf", "impact.ttf"},
{"Palatino Linotype", "pala.ttf", "palab,ttf"},
{"Tahoma", "tahoma.ttf", "tahomabd.ttf"},
{"Times New Roman", "times.ttf", "timesbd.ttf"},
{"Trebuchet MS", "trebuc.ttf", "trebucbd.ttf"},
{"Verdana", "verdana.ttf", "verdanab.ttf"}

Where the first dimension is the font name, the second is the True Type Font file for the normal style, and the third dimension is the True Type Font file for the bold style

I want to populate a combobox with the font names (first dimension)

For index0 = 0 To MyFonts.GetUpperBound(0)
'Add all the Fonts names to a Combobox

I get the error:
"Number of indexes is less than the number of dimensions of the indexed array"

Even if I use


Answer Source

A class will make it easy to display the name, but allow you to get the related TTF or bold file from whatever they select:

Public Class FontItem
    Public Property Name As String
    Public Property TTFile As String
    Public Property TTBoldFile As String
    Public Sub New(n As String, f As String, b As String)
        Name = n
        TTFile = f
        TTBoldFile = b
    End Sub

    Public Overrides Function ToString() As String
        Return Name
    End Function
End Class

This "ties" the name with the 2 files to make it very easy to get the related file. Next, create a list of those things from the data you have:

Dim myFonts As New List(Of FontItem)

Dim data = {{"Arial", "arial.ttf", "arialbd.ttf"},
             ...your long list

For n As Int32 = 0 To data.GetUpperBound(0)
    myFonts.Add(New FontItem(data(n, 0), data(n, 1), data(n, (2))))

cbox1.DataSource = myFonts

I would have built that data differently, but this allows you to use what you have. There is no need to copy to the data into the control, the SelectedItem will be a FontItem (inside an Object). In the SelectedValueChanged event:

Dim item = DirectCast(cbox1.SelectedValue, FontItem)
Console.WriteLine("For {0}, TTF = {1}, bold = {2}", item.Name,

enter image description hereenter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download