* 윈폼템플릿 없이 윈폼 프로그램 만들기
- 소스코드
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _2020._08._19_001
{
class Program
{
static void Main(string[] args)
{
CreateMyForm();
}
/// <summary>
/// CreateMyForm 메서드 생성
/// </summary>
private static void CreateMyForm()
{ // MainForm 객체 생성
Form MainForm = new Form();
// button1,button2 객체 생성
Button button1 = new Button();
Button button2 = new Button();
// button1의 text는 OK, 위치는 10,10
button1.Text = "OK";
button1.Location = new Point(10, 10);
// button2의 text는 Cancel, 위치는 x좌표 : button1의 왼쪽, y좌표 : button1의 높이 + button1의 top + 10
button2.Text = "Cancel";
button2.Location = new Point(button1.Left, button1.Height + button1.Top + 10);
// button1을 click했을 때 메서드 생성
button1.Click += Button1_Click;
// button2을 click했을 때 메서드 생성
button2.Click += Button2_Click;
// MainForm의 style = FixedDialog
MainForm.FormBorderStyle = FormBorderStyle.FixedDialog;
// MainForm의 StartPosition = CenterScreen
MainForm.StartPosition = FormStartPosition.CenterScreen;
// MainForm에 button1과 button2을 띄워라
MainForm.Controls.Add(button1);
MainForm.Controls.Add(button2);
// MainForm을 보여줘라
MainForm.ShowDialog();
}
// Button2을 클릭했을 때 발생하는 메서드(이벤트) > 종료코드 입력했음
private static void Button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
// Button1을 클릭했을 때 발생하는 메서드(이벤트) > OK Button Clicked이라는 메세지박스가 보여진다.
private static void Button1_Click(object sender, EventArgs e)
{
MessageBox.Show("OK Button Clicked!");
}
}
}
- 결과
* 두 개의 Form 띄우기
Windows Forms Application 프로젝트 사용
- Form2 생성하기
-Form1.cs
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 _2020._08._19_002
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.ClientSize = new Size(500, 500);
//this.Text = "폼 클래스";
//this.FormBorderStyle = FormBorderStyle.FixedDialog;
//this.StartPosition = FormStartPosition.CenterScreen;
// Form2 클래스의 객체 f2를 생성
Form f2 = new Form2();
this.AddOwnedForm(f2);
// f2를 띄워라.
f2.Show();
}
}
}
-Form2.cs
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 _2020._08._19_002
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
// Form2의 사이즈 조절
this.ClientSize = new Size(300, 200);
}
private void Form2_Load(object sender, EventArgs e)
{
// Form2의 위치를 부모의 중앙에 위치시킨다.
CenterToParent();
}
}
}
- 결과
* 메세지 박스
- Form1.cs
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 _2020._08._19_002
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.ClientSize = new Size(500, 500);
//this.Text = "폼 클래스";
//this.FormBorderStyle = FormBorderStyle.FixedDialog;
this.StartPosition = FormStartPosition.CenterScreen;
// Form2 클래스의 객체 f2를 생성
Form f2 = new Form2();
this.AddOwnedForm(f2);
// f2를 띄워라.
f2.Show();
}
private void Form1_Load(object sender, EventArgs e)
{
MessageBox.Show("가장 간단한 메세지 박스");
MessageBox.Show("타이틀을 갖는 메세지 박스", "메세지 박스 제목");
MessageBox.Show("느낌표와 알람 메세지 박스", "느낌표와 알람소리", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
DialogResult result1 = MessageBox.Show(
"두개의 버튼을 갖는 메세지", "Question", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
DialogResult result2 = MessageBox.Show(
"세 개의 버튼과 물음표 아이콘을 보여주는 메세지 박스", "Question",
MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
DialogResult result3 = MessageBox.Show(
"디폴트 버튼을 두 번째 버튼으로\n지정한 메세지 박스", "Question",
MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question,
MessageBoxDefaultButton.Button2);
string msg = string.Format("당신의 선택은? : {0} {1} {2}",
result1.ToString(), result2.ToString(), result3.ToString());
MessageBox.Show(msg, "너의 선택은?");
}
}
}
- 결과
* 텍스트박스, 레이블, 버튼 컨트롤
- Form1.cs
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 _2020._08._19_003
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if(textBox1.Text == "")
{
MessageBox.Show("이름을 입력하세요!", "Warning");
}
else
{
label2.Text = textBox1.Text + "님! 안녕하세요!";
}
}
}
}
-Form1.cs[디자인]
- 결과
* 레이블에서 여러 줄의 문자열 표시하기
- Form1.cs[디자인]
- Form1.cs
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 _2020._08._19_004
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
label1.Text = "";
label2.Text = "";
}
private void button1_Click(object sender, EventArgs e)
{
string raffaello = "라파엘로 산치오, 이탈리아, 르네상스 3대 거장, 1483~1520";
string schoolOfAthens = "라파엘로는" + "동시대의 대가인 미켈란젤로, 레오나르도 다빈치와 함께 3대 거장으로 불린다."
+ "\n\n유명한 작품으로는 바티칸궁에 있는" + "프레스코 벽화 입니다.";
label1.Text = raffaello;
label2.Text = schoolOfAthens;
}
}
}
- 결과
* flag 설정
flag란 ? 버튼을 한 번 클릭 했을 때는 내용을 보여주고 한번 더 클릭하면 내용이 없어지게 하는 동작
-Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Text;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _2020._08._19_004
{
public partial class Form1 : Form
{
private bool flag;
public Form1()
{
InitializeComponent();
label1.Text = "";
label2.Text = "";
}
private void button1_Click(object sender, EventArgs e)
{
if (flag == false)
{
raffaelloStory();
flag = true;
}
else
{
label1.Text = "";
label2.Text = "";
flag = false;
}
}
private void raffaelloStory()
{
string raffaello = "라파엘로 산치오, 이탈리아, 르네상스 3대 거장, 1483~1520";
string schoolOfAthens = "라파엘로는" + "동시대의 대가인 미켈란젤로, 레오나르도 다빈치와 함께 3대 거장으로 불린다."
+ "\n\n유명한 작품으로는 바티칸쿵에 있는" + "프레스코 벽화 입니다.";
label1.Text = raffaello;
label2.Text = schoolOfAthens;
}
}
}
* CheckBox 만들기
- Form1.cs[디자인]
-Form1.cs
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 _2020._08._19_005
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string checkStates = "";
CheckBox[] cBox = { checkBox1, checkBox2, checkBox3, checkBox4, checkBox5 };
//for (int i = 0; i < cBox.Length; ++i)
//{
// checkStates += cBox[i].Text + " : " + cBox[i].Checked + "\n";
//}
foreach (CheckBox Temp in cBox)
{
checkStates += Temp.Text + " : " + Temp.Checked + "\n";
}
MessageBox.Show(checkStates, "체크박스 상태");
string summary = string.Format("좋아하는 과일은 : ");
foreach (var item in cBox)
{
if(item.Checked == true)
{
summary += item.Text + " ";
}
}
MessageBox.Show(summary, "summary");
}
}
}
- 결과
* 라디오 버튼
- Form1.cs[디자인]
- Form1.cs
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 _2020._08._19_006
{
public partial class Form1 : Form
{
//private RadioButton checkedRB;
private string gender = "미입력";
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string result = "국적 : ";
/* string result = "";
* if (radioButton1.Checked)
{
result += "국적 : " + radioButton1.Text + "\n";
}
else if (radioButton2.Checked)
{
result += "국적 : " + radioButton2.Text + "\n";
}
else if (radioButton3.Checked)
{
result += "국적 : " + radioButton3.Text + "\n";
}
else if (radioButton4.Checked)
{
result += "국적 : " + radioButton4.Text + "\n";
}*/
for (var Count = 0; Count < groupBox1.Controls.Count; ++Count)
{ // 캐스팅
RadioButton Temp = (RadioButton)groupBox1.Controls[Count];
if(Temp.Checked)
{
result += Temp.Text + "\n";
break;
}
}
/*if (checkedRB == radioButton5)
{
result += "성별 : " + radioButton5.Text;
}
else if(checkedRB == radioButton6)
{
result += "성별 : " + radioButton6.Text;
}*/
result += "성별 : " + gender;
MessageBox.Show(result, "결과");
}
private void radioButton5_CheckedChanged(object sender, EventArgs e)
{
//checkedRB = radioButton5;
gender = "남성";
}
private void radioButton6_CheckedChanged(object sender, EventArgs e)
{
//checkedRB = radioButton6;
gender = "여성";
}
}
}
* 로그인 창 만들기
- Form1.cs[디자인]
- Form1.cs
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 _2020._08._19_007
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if(textBox1.Text == "장국빈" && textBox2.Text == "1234")
{
textBox3.Text = "로그인성공";
}
else
{
textBox3.Text = "로그인실패";
MessageBox.Show("로그인 실패", "경고");
}
}
}
}
- 결과
* 성적계산기
- Form1.cs[디자인]
- Form1.cs
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 _2020._08._19_008
{
public partial class 성적계산기 : Form
{
public 성적계산기()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// convert.Todouble() > string을 double형으로 바꿔줌
double sum = Convert.ToDouble(textBox1.Text) +
Convert.ToDouble(textBox2.Text) + Convert.ToDouble(textBox3.Text);
textBox4.Text = sum.ToString();
double avg = sum / 3;
textBox5.Text = avg.ToString("0.0");
}
}
}
- 결과
'C#' 카테고리의 다른 글
2020.08.21(금) - C# 윈폼 & C# (0) | 2020.08.21 |
---|---|
2020.08.20(목) - C# 윈폼 & C# (0) | 2020.08.20 |
2020.08.18(화) - C# 메서드/클래스 제너릭 & C# 윈폼만들기 (0) | 2020.08.18 |
2020.08.13(목) - HMI (0) | 2020.08.13 |
2020.07.31(금) - 라즈베리파이 & C# (0) | 2020.07.31 |