1

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
    myCombobox.Items.Add(MyFonts(index0))
Next

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

Even if I use

myCombobox.Items.Add(MyFonts(index0,,))
4
  • 1
    MyFonts(index0,0,0) ? Commented Dec 1, 2016 at 17:57
  • 2
    You have a bidimensional array! try: MyFonts(0,0) or MyFonts(0,1) Or MyFonts(0,2) AND MyFonts(1,0) or MyFonts(1,1) Or MyFonts(1,2) and see. Commented Dec 1, 2016 at 18:00
  • @genespos Why bidimensional if I have 3 members on each declaration? {"Arial", "arial.ttf", "arialbd.ttf"} Commented Dec 1, 2016 at 18:46
  • 1
    There are 2 dimensions: 0-13 "vertical" and 0-2 "horizontal". Verdolino has added a good explanation for you. Commented Dec 1, 2016 at 19:44

2 Answers 2

5

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))))
Next

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,
                        item.TTFile,
                        item.TTBoldFile)

enter image description hereenter image description here

Sign up to request clarification or add additional context in comments.

5 Comments

You're solution works fine. Thanks. But I need to populate 3 combobox with the same data. No problem, the 3 combobox has the data but now when I change the selection of one combobox the other 2 change the selection too. Why this happens? The 3 shared the same datasource cbox1.DataSource = myFonts cbox2.DataSource = myFonts cbox3.DataSource = myFonts but I need independient selection on each one
In that case use cbox1.DataSource = myFonts.ToArray() it creates a copy so that they arent locked in synch
Perfect as allways. You are not a case of "if all you have is a hammer, everything looks like a nail" like I am :)
Works on my machine :D
@Plutonix Now I'm adapting the class to show the image of the TTF on the combobox instead of the name, here's the link to the new post stackoverflow.com/questions/41408165/…
4

You have a 2D array. The array is indexed like this

enter image description here

and when indexed, the value returned is a string.

Consider the following array, which is 3D

enter image description here

Your original information is really only 1D because each element corresponds to a single font. Now, if you had duplicates of the same font i.e. Arial twice, but with different ttf files, then you had two logical dimensions of information.

You can test these different dimensions with the following code. The indexers are at the end

Sub Main()
    Dim MyFonts2D =
    {
        {"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"}
    }

    Dim MyFonts3D =
    {
        {
            {"Arial", "arial1.ttf", "arialbd1.ttf"},
            {"Arial", "arial2.ttf", "arialbd2.ttf"},
            {"Arial", "arial3.ttf", "arialbd3.ttf"}
        },
        {
            {"Calibri", "Calibri1.ttf", "Calibribd1.ttf"},
            {"Calibri", "Calibri2.ttf", "Calibribd2.ttf"},
            {"Calibri", "Calibri3.ttf", "Calibribd3.ttf"}
        },
        {
            {"Candara", "Candara1.ttf", "Candarabd1.ttf"},
            {"Candara", "Candara2.ttf", "Candarabd2.ttf"},
            {"Candara", "Candara3.ttf", "Candarabd3.ttf"}
        }
    }

    Console.WriteLine("Enter first index")
    Dim i = Integer.Parse(Console.ReadLine())
    Console.WriteLine("Enter second index")
    Dim j = Integer.Parse(Console.ReadLine())
    Console.WriteLine("Enter third index")
    Dim k = Integer.Parse(Console.ReadLine())
    Dim a = MyFonts2D(i, j)
    Console.WriteLine(a)
    Dim b = MyFonts3D(i, j, k)
    Console.WriteLine(b)
    Console.ReadLine()
End Sub

1 Comment

Thanks a lot for the complete explanation. I'm sure it will be useful for others

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.