[c#] comboBox practice
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//*******************************************************************
//Add ComboBox Items
comboBox1.Items.Add("Item1");
comboBox1.Items.Add("Item2");
comboBox1.Items.Add("Item3");
//첫번째 항목을 선택한다
comboBox1.SelectedIndex = 0;
//comboBox에 설정한 item 이외에 다른값을 입력 못하도록 설정하는 방법은
//DropDownStyle을 DropDownList 로 하면 된다
comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
//1. 콤보박스에서 다른 Item을 선택한 경우 이벤트등록
comboBox1.SelectedIndexChanged += comboBox1_SelectedIndexChanged;
//2. 콤보박스에서 다른 Item을 선택한 경우 이벤트등록
comboBox1.SelectedValueChanged += comboBox1_SelectedValueChanged;
//*******************************************************************
//List를 comboBox에 item으로 추가
var myItems = new List<string>();
myItems.Add("CAR1");
myItems.Add("CAR2");
myItems.Add("CAR3");
foreach (string item in myItems)
{
comboBox2.Items.Add(item);
}
//두번째 항목을 선택한다
comboBox2.SelectedIndex = 1;
//comboBox에 설정한 item 이외에 다른값을 입력할 수 있다
comboBox2.DropDownStyle = ComboBoxStyle.DropDown;
Console.WriteLine("comboBox2-Items CanSelect {0}", comboBox2.CanSelect );
//*******************************************************************
int[] yourItems = { 100, 200, 300, 400, 500 };
foreach (int items in yourItems)
{
comboBox3.Items.Add(items);
}
Console.WriteLine("comboBox3-Items Count {0}",comboBox3.Items.Count);
}
void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Console.WriteLine("SelectedIndexChanged >>>> {0}", ((ComboBox)sender).SelectedItem);
}
void comboBox1_SelectedValueChanged(object sender, EventArgs e)
{
Console.WriteLine("SelectedValueChanged >>>> {0}", ((ComboBox)sender).SelectedItem);
}
}
}
| cs |
댓글
댓글 쓰기