
디자인에서 생성할 수도 있지만,
form.cs 에서도 생성할 수 있다.
테스트해본 결과, 생성해도 디자인에는 기록이 되지 않는 것 같다
안드로이드스튜디오는 생성이 되는 것으로 알고 있다.
이렇게 동적으로 직접 생성하는 것의 장점은
반복문, if문등의 일괄처리가 가능하다는 것이다 .
그 예로 코드를 작성했다.
줄맞추는 코드는 연습을 해야겟다.
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 | 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 WindowsFormsApp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); myButton.Text = "change in code"; myButton.Width = 150; // 직접 버튼을 생성하는 부분 Button dbutton = new Button(); // 버튼 인스턴스 생성. Controls.Add(dbutton); //직접 생성하니까 desine 에 생성 x dbutton.Location = new Point(200, 40); // 디자인에서 생성안하면 if문, 반복문 등의 처리를 한번에 일괄처리 가능. for(int a = 0; a<5; a++) { Button button = new Button(); Controls.Add(button); button.Location = new Point(13, 13 + (23 + 3) * a); button.Text = "동적 생성" + a + "번째"; button.Width = 100; } } private void Form1_Load(object sender, EventArgs e) { } } } | cs |
'C# > C# Concept' 카테고리의 다른 글
[C#] switch 문 (0) | 2018.03.15 |
---|---|
[C#] IF 문 (0) | 2018.03.15 |
[C#] partial 클래스 (0) | 2018.03.14 |
[Java] finalize 메소드 (0) | 2018.03.08 |
[Java] throws, throw (0) | 2018.03.08 |