3D数组循环并获取第一维值 [英] 3D array loop and get the first dimension value
问题描述
像这样的3D阵列
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"}
}
第一个尺寸是字体名称,第二个尺寸是字体名称真正的T ype是普通样式的字体文件,第三个维度是粗体样式的True Type字体文件
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"
即使我使用
myCombobox.Items.Add(MyFonts(index0,,))
推荐答案
使用一个类可以轻松显示名称,但允许您从他们选择的任何内容中获取相关的TTF或粗体文件:
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
此将名称与2个文件联系在一起,这样就很容易获得相关文件。接下来,从您拥有的数据中创建这些东西的列表:
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
数据有所不同,但这使您可以使用已有的数据。无需将数据复制到控件中, SelectedItem
将是 FontItem
(在 Object
)。在 SelectedValueChanged
事件中:
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)
这篇关于3D数组循环并获取第一维值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!