質問編集履歴

2

改善

2018/06/01 06:03

投稿

xlostdjx
xlostdjx

スコア39

test CHANGED
File without changes
test CHANGED
@@ -38,116 +38,94 @@
38
38
 
39
39
  {
40
40
 
41
+ public class ItemSet
42
+
43
+ {
44
+
45
+ // DisplayMemberとValueMemberにはプロパティで指定する仕組み
46
+
47
+ public String ItemDisp { get; set; }
48
+
49
+ public int ItemValue { get; set; }
50
+
51
+
52
+
53
+ // プロパティをコンストラクタでセット
54
+
55
+ public ItemSet(int v, String s)
56
+
57
+ {
58
+
59
+ ItemDisp = s;
60
+
61
+ ItemValue = v;
62
+
63
+ }
64
+
65
+ }
66
+
67
+
68
+
41
69
  public Form1()
42
70
 
43
71
  {
44
72
 
45
73
  InitializeComponent();
46
74
 
75
+
76
+
77
+ // ComboBox用データ作成 //ListでOK //IList インターフェイスまたは
78
+
79
+ //IListSource インターフェイスを実装する、DataSet または Array などのオブジェクト。
80
+
81
+
82
+
83
+ List<ItemSet> src = new List<ItemSet>();
84
+
85
+ src.Add(new ItemSet(1, "Number1"));/// 1つでItem1つ分となる
86
+
87
+ src.Add(new ItemSet(2, "Number2"));
88
+
89
+ src.Add(new ItemSet(3, "Number3"));
90
+
91
+
92
+
93
+ // ComboBoxに表示と値をセット
94
+
95
+ comboBox1.DataSource = src;
96
+
97
+ comboBox1.DisplayMember = "ItemDisp";
98
+
99
+ comboBox1.ValueMember = "ItemValue";
100
+
101
+
102
+
103
+ // 初期値セット
104
+
105
+ comboBox1.SelectedIndex = 0;
106
+
107
+ comboBox1_SelectedIndexChanged(null, null);
108
+
47
109
  }
48
110
 
49
111
 
50
112
 
113
+
114
+
51
- private void Form1_Load(object sender, EventArgs e)
115
+ private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
52
116
 
53
117
  {
118
+
119
+ // labelに現在コンボ選択の内容を表示
120
+
121
+ ItemSet tmp = ((ItemSet)comboBox1.SelectedItem);//表示名はキャストして取りだす
122
+
123
+ labelDisplay.Text = tmp.ItemDisp;
124
+
125
+ labelValue.Text = comboBox1.SelectedValue.ToString();//値はそのまま取りだせる
54
126
 
55
127
 
56
128
 
57
129
  }
58
130
 
59
- }
60
-
61
-
62
-
63
- class Sample
64
-
65
- {
66
-
67
- [STAThread]
68
-
69
- public static void Main()
70
-
71
- {
72
-
73
- Application.Run(new MainForm());
74
-
75
- }
76
-
77
- }
78
-
79
-
80
-
81
- class MainForm : Form
82
-
83
- {
84
-
85
- private ComboBox comboBox = new ComboBox();//コンボボックス
86
-
87
- private Label lb = new Label(); //ラベル
88
-
89
- private Button bt = new Button(); //ボタン
90
-
91
-
92
-
93
- public MainForm()
94
-
95
- {
96
-
97
- //値の設定
98
-
99
- comboBox.Items.Add("晴れ");
100
-
101
- comboBox.Items.Add("曇り");
102
-
103
- comboBox.Items.Add("雨");
104
-
105
-
106
-
107
- lb.Text = "";
108
-
109
- lb.AutoSize = true;
110
-
111
- lb.Top = comboBox.Bottom;
112
-
113
-
114
-
115
- bt.Text = "値を取得";
116
-
117
- bt.AutoSize = true;
118
-
119
- bt.Top = lb.Bottom;
120
-
121
-
122
-
123
- this.Controls.Add(comboBox);
124
-
125
- this.Controls.Add(lb);
126
-
127
- this.Controls.Add(bt);
128
-
129
-
130
-
131
- bt.Click += new EventHandler(bt_Click);
132
-
133
- }
134
-
135
- public void bt_Click(Object sender, EventArgs e)
136
-
137
- {
138
-
139
- lb.Text = "";
140
-
141
- //値を取得
142
-
143
- lb.Text += comboBox.SelectedItem.ToString();
144
-
145
- }
146
-
147
- }
148
-
149
-
150
-
151
- }
152
-
153
131
  ```

1

編集

2018/06/01 06:03

投稿

xlostdjx
xlostdjx

スコア39

test CHANGED
File without changes
test CHANGED
@@ -1,14 +1,12 @@
1
- ```C#
1
+ C#
2
2
 
3
3
 
4
4
 
5
5
  このようにコード書いてみたのですが動きません。
6
6
 
7
-  
7
+  ```
8
8
 
9
9
  コード
10
-
11
- ```
12
10
 
13
11
 
14
12
 
@@ -151,3 +149,5 @@
151
149
 
152
150
 
153
151
  }
152
+
153
+ ```