본문 바로가기

Programming/C & C++

C# Windows Forms Application GUI 기본

[1] 기본 GUI 세팅 및 씬 넘기기

[2] Mysql 연동 로그인

[3] Mysql txt 전송



원래는 C++로 프로젝트를 진행하려고 했으나 윈도우 로그를 가져오는 방식에 걸림돌이 많아서


C#으로 언어를 바꾼다! 이오스를 잘 알기전에는 C++을 써야할수도 있어서 C++을 사용했었는데,


윈도우 앱에서는 이오스를 건드릴 일이 없기에 프로젝트가 편해졌다.





[1] 기본 GUI 세팅 및 씬 넘기기

[2] Mysql 연동 로그인


C++에서 했던 방식과 비슷해서 이 부분은 쉬웠다. 기존에 C++에서 했던것처럼 GUI 도구 상자를 이용해서 붙이기만하면 됐다.



우선 Windows Forms 앱(.NET Framework)로 프로젝트를 생성하면 Forms1.cs[디자인] 이 바로 띄워질 것이다.



이후에는 도구 상자 (없다면 '보기' 옵션에서 찾아서 띄우면된다)에서 본인이 원하는 도구들을 선택해서 옮겨 붙이면 된다.


**강의 : https://www.youtube.com/watch?v=xpXtHgzOniU&index=3&list=PLFucTHV5G6cKNb5F-5k_D6nXSld0tUAt1


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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.Common;
using MySql.Data.MySqlClient;
 
namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            PWD_txt.PasswordChar = '*';
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
 
        }
 
        private void Login_btn_Click(object sender, EventArgs e)
        {
            MySqlConnection conn;
            string strconn = "datasource=IP;port=3306;username=ID;password=pwd;SslMode=none";
            conn = new MySqlConnection(strconn);
            conn.Open();
 
            if (ID_txt.Text != "" && PWD_txt.Text != "")
            {
                string sql = "select id, password from dbname.table where id= '" + ID_txt.Text + "'";
                var Command0 = new MySqlCommand(sql, conn);
                var Read0 = Command0.ExecuteReader();
 
                if (Read0.HasRows && Read0.Read())
                {
                    if (Read0["password"].ToString() == PWD_txt.Text)
                    {
                        MessageBox.Show("로그인에 성공했습니다.");
                        this.Hide();
                        Form2 f2 = new Form2();
                        f2.ShowDialog();
                    }
                    else
                    {
                        MessageBox.Show("정보가 일치하지않습니다.");
                    }
                }
                else
                {
                    MessageBox.Show("정보가 일치하지않습니다.");
                }
 
            }
            else
            {
                MessageBox.Show("정보가 일치하지않습니다.");
            }
 
 
 
        }
    }
}
 
cs


위 코드를 따라하면 텍스트 박스에 있는 텍스트를 불러와서 Mysql에 저장된 데이터와 비교해 로그인 시스템을 제작할 수 있다.






[3] Mysql txt 전송


이것도 C++에서 했던 것과 동일한 형식으로 쿼리문만 보내주면 됐기 때문에 어렵지 않았다.


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
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;
using MySql.Data.Common;
using MySql.Data.MySqlClient;
 
namespace WindowsFormsApp2
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
 
        private void Logout_btn_Click(object sender, EventArgs e)
        {
            MessageBox.Show("로그아웃합니다.");
            this.Hide();
            Form1 f1 = new Form1();
            f1.ShowDialog();
        }
 
        private void Func_btn_Click(object sender, EventArgs e)
        {
            MySqlConnection conn;
            string strconn = "datasource=IP;port=3306;username=id;password=pwd;SslMode=none";
            conn = new MySqlConnection(strconn);
            conn.Open();
 
            string sql = "insert into vrshop.DataStorage (user_logData) values('" + "테스트중입니다." + "') ;";
            var Command0 = new MySqlCommand(sql, conn);
            int check = Command0.ExecuteNonQuery();
 
            if (check == 1)
            {
                MessageBox.Show("전송에 성공했습니다.");
            }
            else
            {
                MessageBox.Show("전송에 실패했습니다.");
            }
        }
    }
}
 
cs


설명을 적게 남기는 이유는 기본적인 코딩 지식이 있으면 쉽게 할 수 있기 때문이다!

(어렵다면 C++로 만들어놓은 게시글을 참고하길 바란다, 거의 동일한 내용이다)





이제는 로그 데이터를 불러와서 선별하는 작업을 해야한다. 화이팅!

'Programming > C & C++' 카테고리의 다른 글

EC++ Study_3  (0) 2019.09.01
EC++ Study_2  (0) 2019.08.11
EC++ Study_1  (0) 2019.08.07
Visual C++ Windows forms [1]  (0) 2018.07.06
Visual C++ 학습  (0) 2018.07.05