오늘은 간단히 프로그래밍에 입문하면 처음 만드어 볼수 있는 프로그램 주제로 넘어가

로또 생성기를 만들어 보겠습니다. 콘솔을 이용한 로또 프로그램보다는 ui 요소가 들어가 있는 부분이 더욱

공부가 됩니다.

윈폼 으로 간단한 로또 생성기를 만들어 보겠습니다. 

 

 

 


Lotto UI 디자인

비주얼 스튜디오로를 실행후 c# winform 프로젝트를 생성 하여 줍니다

윈폼을 생성한뒤 도구상자를 들어가 Label을 7개 생성해줍니다.

도구상자는 <보기>를 클릭 하시면 목록에 도구상자가 있습니다.

 

라벨중 하나를 제목으로 지정하여야 합니다 일단 라벨 하나를 선택하여 속성창을 들어갑니다.

그리고 라벨의 속성중

Name을 LottoTitleLabel ,

Text 을 로또 생성기 ,

font size를 18pt 로 변경하여줍니다.

 

 

 

 

나머지 라벨을 한꺼번에 선택하여 font size 14로 맞춘후 label 정렬하여 줍니다.

그런후 각각 속성 Name 에 LottoNumberLabel1,LottoNumberLabel2, ... 등으로 변경하여 준후 Text를 - 특수 문자로 변경 합니다.

 

 

 

 

 

이렇게 완성된후에 로또를 생성 시킬수 있는 Button을 생성합니다.

그후 Button 속성에 들어가 Name을 NumberCreateButton 으로 바꾸어주고

Text 의 값은 로또 번호 생성으로 바꾸어준후 font size를 적절히 바꾸어줍니다.

 

 

 

버튼을 생성후 

버튼을 더블클릭하여 버튼이 클릭하였을 때 나타나는 함수를 만들어 줍니다.

 

 

 

 

 


 

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 Lotto
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void NumberCreateButton_Click(object sender, EventArgs e)
        {
            Label[] LottoArray = new Label[] { LottoNumberLabel1, LottoNumberLabel2, LottoNumberLabel3,
                LottoNumberLabel4, LottoNumberLabel5, LottoNumberLabel6 };

            Random rand = new Random();

            for (int lottoindex = 0; lottoindex < LottoArray.Length; lottoindex++)
            {
                int lottoCheckIndex = lottoindex;
                int lottonumber = rand.Next(1, 46);
                for (; lottoCheckIndex >= 0; lottoCheckIndex--)
                {
                    if (LottoArray[lottoCheckIndex].Text == lottonumber.ToString())
                    {
                        lottoCheckIndex = lottoindex;
                        lottonumber = rand.Next(1, 46);
                    }
                }
                LottoArray[lottoindex].Text = lottonumber.ToString();

            }
        }
    }
}


코드를 작성하여 줍니다. 

'개발 > c#' 카테고리의 다른 글

C# Chromium 크롬 웹 브라우저 만들기  (0) 2021.09.30
C# WebBrowser 자동 로그인  (4) 2021.04.15
Boxing UnBoxing  (0) 2021.04.14
c# WebBrowser 만들기  (0) 2021.04.12
c# 으로 메모장 만들기  (0) 2021.04.11

+ Recent posts