본문 바로가기

   
Programming/Winform

윈폼(winform) ModalModeless, Dialog, ProgressBar, Timer 사용예제

반응형

윈폼(winform) ModalModeless, Dialog, ProgressBar, Timer 사용예제

윈폼에서 자식창을 뛰우는 방법은 2가지가 있다. :  모달 창 방식, 모달리스  

Modal :  부모한테 포커스가 간다면 전에 상태 값이 사라지기 때문에 자식편집이 끝날때까지 부모에게 가지 못하게한다.   외동자식
             MessageBox.Show와 같은형태 확인을 누르기전엔 절대 다른곳 접근 불가능, 메시지박스 처럼 사용             
Modeless : 자식편집이 있는 상태에서도 부모창에 포커스가 가능하다. 하나이상의 자식을 가질수 있다.

DialogResult 속성은 모달창에서만 사용하는 속성이다.


using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

namespace Start

{

       public partial class ModalModeless : Form

       {

             public ModalModeless()

             {

                    InitializeComponent();

             }

 

             private void button1_Click(object sender, EventArgs e)

             {

                    //Modal

                    ModalModelessChild child = new ModalModelessChild();//창만들기

                    DialogResult result = child.ShowDialog();

                    MessageBox.Show(result.ToString());

             }

 

             private void button2_Click(object sender, EventArgs e)

             {

                    //Modeless

                    ModalModelessChild child = new ModalModelessChild();//창만들기

                    child.Show();

             }

       }

}

 

Dialog : 색상을 지정하는 컬러 대화상자 모달용으로 만들어논 창(그렇게 설계된것) OS에서 가져온 기능 반드시 확인 OR 취소가 되야 그다음 작업을 할수 있다.
결과값은 무조건 다이얼로그리절트이다. 
 

 

 
 
using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

namespace Start

{

       public partial class Dialog : Form

       {

             public Dialog()

             {

                    InitializeComponent();

             }

 

             private void button1_Click(object sender, EventArgs e)

             {

                    //Modal창으로 사용하려고 만들어놓은 자식창

                    //ModalModelessChild child = new ModalModelessChild();

                    //colorDialog1

                    DialogResult result = colorDialog1.ShowDialog();

 

                    if (result == DialogResult.OK)

                    {

                           //색상을 선택함

                           //사용자가 선택한 색상을 폼의 배경색으로 지정

                           //사용자가 선택한 색상이 몬데?(대화상자가 지정) 메모리에 계속 상주해 있는중

                           this.BackColor = colorDialog1.Color;

                    }

 

                    else

                    {

                           //색상 선택을 포기함

                    }

             }

 

             private void Dialog_Load(object sender, EventArgs e)

             {

                    //대화상자의 초기화

                    //colorDialog1.Color = Color.White;

                    colorDialog1.Color = this.BackColor;

             }

       }

}

 
열기 대화상자
 

 






using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.IO;

 

namespace Start

{

       public partial class Dialog02 : Form

       {

             public Dialog02()

             {

                    InitializeComponent();

             }

 

             private void button2_Click(object sender, EventArgs e)

             {

                    DialogResult result = openFileDialog1.ShowDialog();

 

                    if (result == DialogResult.OK)

                    {

                           //파일 열기 -> 선택한 파일의 경로와 이름만 알려줌

                           MessageBox.Show(openFileDialog1.FileName);

 

                           StreamReader reader = new StreamReader(openFileDialog1.FileName);

 

                           textBox1.Text = reader.ReadToEnd();

                           reader.Close();

                    }

             }

 

             private void button3_Click(object sender, EventArgs e)

             {

                    //저장

                    if (saveFileDialog1.ShowDialog() == DialogResult.OK)

                    {

                           //사용자가 어떤이름으로 어느위치에 저장할건지 전체 경로만 반환

                           StreamWriter writer = new StreamWriter(saveFileDialog1.FileName);

                           writer.Write(textBox1.Text);

                           writer.Close();

                    }

             }

 

             private void button4_Click(object sender, EventArgs e)

             {

                    //폴더선택

                    if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)

                    {

                           textBox1.Text = folderBrowserDialog1.SelectedPath;

                    }

             }

 

             private void button5_Click(object sender, EventArgs e)

             {

                    //폰트 선택

                    fontDialog1.Font = textBox1.Font;//기본값

 

                    if (fontDialog1.ShowDialog() == DialogResult.OK)

                    {

                           textBox1.Font = fontDialog1.Font;

                    }

             }

       }

}

 

 


using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Threading;

 

namespace Start

{

       public partial class ProgressBar : Form

       {

             public ProgressBar()

             {

                    InitializeComponent();

             }

 

             private void button1_Click(object sender, EventArgs e)

             {

                    //시작

                    //trackBar1.Value = 5;

                    //progressBar1.Value = 50;

 

                    //1. 장시간 제어를 붙잡고 놓지않는 코드가 있을때 그 코드를 메서드로 만든다.

                    //2. 그 메서드를 독립적으로 실행하기 위한 공간을 만든다. : 그 공간을 스레드라고 한다.

                    //AddValue를 사용하기위한 새로운 공간을 만들고

                    Thread th = new Thread(new ThreadStart(AddValue));

 

                    th.IsBackground = true;

 

                    //3. 해당 공간안에서 AddValue()메서드를 실행

                    th.Start();

 

                    //4. AddValue()가 다 실행되고 나면(10초 소요) th 스레드(공간)는 자동으로 소멸

             }

 

             private void AddValue()

             {

                    //10초동안 이일을 잡고 있어서 다른 작업을 할수 없다.

                    for (int i = 0; i < 100; i++)

                    {

                           progressBar1.Value = i;

                           progressBar2.Value = i;

                           Thread.Sleep(50);

                    }

             }

       }

}

 

 

mso�7s-�06ly:"맑은 고딕";mso-hansi-theme-font:minor-latin;mso-bidi-font-family: "Bitstream Vera Sans Mono";mso-font-kerning:0pt'>                    }

             }

 

             private void button5_Click(object sender, EventArgs e)

             {

                    //폰트 선택

                    fontDialog1.Font = textBox1.Font;//기본값

 

                    if (fontDialog1.ShowDialog() == DialogResult.OK)

                    {

                           textBox1.Font = fontDialog1.Font;

                    }

             }

       }

}

 

 
Web 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

namespace Start

{

       public partial class Web : Form

       {

             public Web()

             {

                    InitializeComponent();

             }

 

             private void toolStripButton1_Click(object sender, EventArgs e)

             {

                    //URL 이동

                    webBrowser1.Navigate(toolStripTextBox1.Text);

             }

 

             private void toolStripButton2_Click(object sender, EventArgs e)

             {

                    //뒤로가기

                    webBrowser1.GoBack();

             }

 

             private void toolStripButton3_Click(object sender, EventArgs e)

             {

                    //앞으로가기 - 다음페이지

                    webBrowser1.GoForward();

             }

 

             private void toolStripButton4_Click(object sender, EventArgs e)

             {

                    //홈페이지 - 시작 페이지

                    webBrowser1.GoHome();

             }

 

             private void toolStripButton5_Click(object sender, EventArgs e)

             {

                    webBrowser1.Stop();

             }

 

             private void toolStripButton6_Click(object sender, EventArgs e)

             {

                    webBrowser1.Refresh();

             }

 

             private void toolStripButton7_Click(object sender, EventArgs e)

             {

                    //테스트

                    //웹브라우저 컨트롤 용도 : 로드된 페이지의 내용을 접근할 수  있음!!

 

                    HtmlElement txt = webBrowser1.Document.GetElementById("query");

 

                    txt.SetAttribute("value", "윈폼");

             }

       }

}

 

 
Timer (스레드 대신 사용 하면 좋다)
 


using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Threading;

 

namespace Start

{

       public partial class Timer : Form

       {

             public Timer()

             {

                    InitializeComponent();

             }

 

             private void button1_Click(object sender, EventArgs e)

             {

                    Thread th = new Thread(new ThreadStart(Time));

                    th.IsBackground = true;

                    th.Start();

             }

 

             private void Time()

             {

                    for (; ; )

                    {

                           label1.Text = DateTime.Now.ToLongTimeString();

                           Thread.Sleep(1000);

                    }

             }

 

             //이 메서드는 별도로 스레드처리가 된다.

             private void timer1_Tick(object sender, EventArgs e)

             {

                    label2.Text = DateTime.Now.ToLongTimeString();

             }

 

             private void button2_Click(object sender, EventArgs e)

             {

                    //timer1.Start();

                    timer1.Enabled = true;

             }

 

             private void button3_Click(object sender, EventArgs e)

             {

                    //timer1.Stop();

                    timer1.Enabled = false;

             }

       }

}

 

 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

namespace Start

{

       public partial class Time02 : Form

       {

 

             private string direction;

 

             public Time02()

             {

                    InitializeComponent();

                    direction = "right";

             }

      

             private void button5_Click(object sender, EventArgs e)

             {

                    //시작

                    if (button5.Text == "시작")

                    {

                           button5.Text = "멈춤";

                           timer1.Enabled = true;

                    }

                    else

                    {

                           button5.Text = "시작";

                           timer1.Enabled = false;

                    }

             }

 

             private void timer1_Tick(object sender, EventArgs e)

             {

                    //1초에 한번씩 호출 timer1.Enabled = true일때

                    int x = 0, y = 0;

 

                    if (direction == "up")

                    {

                           x = 0;

                           y = -10;

                    }

                    else if (direction == "left")

                    {

                           x = -10;

                           y = 0;

                    }

                    else if (direction == "down")

                    {

                           x = 0;

                           y = 10;

                    }

                    else if (direction == "right")

                    {

                           x = 10;

                           y = 0;

                    }

 

                    Point p = new Point(pictureBox1.Location.X + x, pictureBox1.Location.Y + y);

 

                    pictureBox1.Location = p;

             }

 

             private void button1_Click(object sender, EventArgs e)

             {

                    direction = "up";

             }

 

             private void button2_Click(object sender, EventArgs e)

             {

                    direction = "left";

             }

 

             private void button3_Click(object sender, EventArgs e)

             {

                    direction = "right";

             }

 

             private void button4_Click(object sender, EventArgs e)

             {

                    direction = "down";

             }

 

             private void trackBar1_Scroll(object sender, EventArgs e)

             {

                    if (trackBar1.Value == 1)

                    {

                           timer1.Interval = 1000;

                    }

                    else if (trackBar1.Value == 2)

                    {

                           timer1.Interval = 500;

                    }

                    else if (trackBar1.Value == 3)

                    {

                           timer1.Interval = 100;

                    }

             }

       }

}

 

 

반응형