|
■No90527 (aa さん) に返信 > にした場合、元 list.add("あ1",0)の0も追加したいんです。
あ1〜い3 まで、すべて「0」が設定されていたようですが、それで構わないのですね?
'=== 案1 === Private list As New Dictionary(Of String, (Number As Integer, Picture As Image))() Private Sub _07_shouhin_Load(sender As Object, e As EventArgs) Handles MyBase.Load TextBox1.Enabled = False list.Add("あ1", (0, My.Resources.A00)) list.Add("あ2", (0, My.Resources.A01)) list.Add("あ3", (0, My.Resources.A02)) list.Add("い1", (0, My.Resources.A03)) list.Add("い2", (0, My.Resources.A04)) list.Add("い3", (0, My.Resources.A05)) ComboBox1.DisplayMember = "Key" ComboBox1.ValueMember = "Value" ComboBox1.DataSource = New BindingSource(list, Nothing) End Sub Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged TextBox1.Text = ComboBox1.GetItemText(ComboBox1.SelectedItem) 'Dim selectedValue = DirectCast(ComboBox1.SelectedValue, (Number As Integer, Picture As Image)) 'Label1.Text = CStr(selectedValue.Number) 'PictureBox2.Image = selectedValue.Picture End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click PictureBox1.Image = DirectCast(ComboBox1.SelectedValue, (Number As Integer, Picture As Image)).Picture End Sub
'=== 案2 === Private list As Dictionary(Of String, Integer) Private images As Dictionary(Of String, Image)
Private Sub _07_shouhin_Load(sender As Object, e As EventArgs) Handles MyBase.Load TextBox1.Enabled = False list = New Dictionary(Of String, Integer)() From { {"あ1", 0}, {"あ2", 0}, {"あ3", 0}, {"い1", 0}, {"い2", 0}, {"い3", 0} } images = New Dictionary(Of String, Image)() From { {"あ1", My.Resources.A00}, {"あ2", My.Resources.A01}, {"あ3", My.Resources.A02}, {"い1", My.Resources.A03}, {"い2", My.Resources.A04}, {"い3", My.Resources.A05} } ComboBox1.DisplayMember = "Key" ComboBox1.ValueMember = "Value" ComboBox1.DataSource = New BindingSource(list, Nothing) End Sub Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged Dim selectedKey As String = ComboBox1.GetItemText(ComboBox1.SelectedItem) 'Dim selectedNumber As Integer = CInt(ComboBox1.SelectedValue) 'Dim selectedImage As Image = images(selectedKey) TextBox1.Text = selectedKey 'Label1.Text = CStr(selectedNumber) 'PictureBox2.Image = selectedImage End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click images.TryGetValue(ComboBox1.GetItemText(ComboBox1.SelectedItem), PictureBox1.Image) End Sub
|