프로그래밍/C# (WinForms)42 C#, WinForms ] tabControl의 탭 부분 색상 변경 tabControl의 DrawMode 속성을 OwnerDrawFixed로 설정하고 DrawItem 이벤트 핸들러를 연결 private void Form1_Load(object sender, EventArgs e) { //... tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed; tabControl1.DrawItem += new DrawItemEventHandler(tabControl1_DrawItem); //... } 이 부분은 GUI를 통해서도 가능하다. DrawItem 이벤트 핸들러는 각 탭 버튼을 그릴 때 호출된다. e.Index는 현재 탭 버튼의 인덱스로 현재 그려지고 있는 탭 버튼이 어떤 탭 페이지에 해당하는지 확인할 수 있다. e.Bounds는 현재 탭버.. 2023. 11. 7. C# ] 명령프롬프트, 시스템 명령어 실행 using System; using System.Diagnostics; class Program { static void Main(string[] args) { string Command = "dir"; ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = "cmd.exe"; startInfo.Arguments = "/C " + Command; Process process = new Process(); process.StartInfo = startInfo; process.Start(); } } C++에서 system("") 함수를 사용하여 시스템 명령어를 실행할 때는 내부적으로 명령 프롬프트를 호출하는 작업이 숨겨져 있어서 간.. 2023. 7. 11. C#, WinForms ] 자식 폼에서 부모 폼으로 데이터 전달 방법, 이벤트 / 프로퍼티 자식폼에서 dataGridView의 selectedIndex 전달 받기 1. 이벤트 활용 방법 자식폼 public partial class DeviceListForm : Form { // ... public event EventHandler OnItemSelected; // OK 버튼 클릭 시 private void buttonOk_Click(object sender, EventArgs e) { if(deviceList.SelectedIndex != -1) { OnItemSelected?.Invoke(this, deviceList.SelectedIndex); } } // ListBox 더블클릭 시 private void deviceList_MouseDoubleClick(object sender, Mous.. 2023. 5. 14. WinForms ] Anchor, Dock properties Anchor 속성의 default 값은 Top, Left인데 이 값을 변경하면 bold 체로 바뀐다. Anchor 속성 미사용시 Anchor 속성 사용 Dock 속성 default 값은 None이다. toolStrip은 top에 dock하고 statusStrip은 bottom에 dock할 수 있다. 여러 컨트롤을 같은 방향에 dock할 수 있고 어떤걸 먼저 dock하느냐에 따라 화면에 표시되는게 달라진다. textBox의 multiLine 속성을 true로하고 dock 속성을 fill로 하면 마치 메모장같은 GUI를 볼 수 있다. splitContainer를 먼저 만들고 그 안에서 컨트롤을 dock하는 것도 가능하다. 2023. 5. 14. C#, WinForms ] Event 와 delegate C#에서 이벤트를 사용하면 프로그램의 느슨한 결합(loose coupling)을 통해 객체 간 상호작용을 구현할 수 있다. 이벤트는 객체가 발생시키는 특정한 동작을 나타내며, 이벤트를 구독하는 다른 객체는 해당 이벤트를 수신하고 이벤트 처리기를 호출한다. 이를 통해 클래스 내부에서 발생한 동작을 외부에서 알림으로써 더욱 유연하고 효율적인 프로그래밍을 구현할 수 있다. 이벤트를 발생시키는 쪽이 Publisher이고 이벤트를 수신하고 처리하는 쪽이 Subscriber가 된다. 1. delegate 정의 및 이벤트 선언하기 Publisher 쪽에서 메서드를 참조하는 형식인 delegate를 사용하여 이벤트를 선언하면 된다. 직접 delegate 정의를 할 수도 있고 system namespace에 이미 정의된.. 2023. 5. 14. C#, WinForms ] CRC Checker private void button1_Click(object sender, EventArgs e) { // 초기 디렉토리 설정 openFileDialog1.InitialDirectory = "C:\\"; // 파일 필터 설정 openFileDialog1.Filter = "모든 파일 (*.*)|*.*|텍스트 파일 (*.txt)|*.txt|바이너리 파일 (*.bin)|*.bin|데이터 파일 (*.dat)|*.dat"; // 마지막으로 파일 대화 상자에서 선택한 디렉토리를 기억하고, 다음번 파일 대화 상자를 열 때에는 이전에 선택한 디렉토리를 자동으로 선택 openFileDialog1.RestoreDirectory = true; if (openFileDialog1.ShowDialog() == DialogRes.. 2023. 5. 14. C#, WinForms ] Drag & Drop 으로 파일 경로 얻기 파일 시스템에 액세스하기위해 using System.IO; 선언 Form의 AllowDrop 속성 true 일반적으로 Form이나 Control에는 파일을 Drop할 수 없기 때문에, 해당 Form이나 Control에 AllowDrop 속성을 true로 설정해주어야 한다. this.AllowDrop = true; DragEnter, DragDrop 이벤트 핸들러 추가 this.DragDrop += new System.Windows.Forms.DragEventHandler(this.Form1_DragDrop); this.DragEnter += new System.Windows.Forms.DragEventHandler(this.Form1_DragEnter); DragEnter 이벤트 : 사용자가 무언인가를 .. 2023. 5. 14. C# ] delegate C#의 delegate는 메서드를 참조하는 객체로 C/C++의 함수포인터와 비슷한 개념이다. C#의 메서드포인터라고 보면 된다. 이 Delegate는 이벤트 핸들러나 콜백 함수를 구현하거나, 대리자 메서드를 호출하는 등 용도로 사용할 수 있다. using System; namespace ConsoleApp2 { class Program { static void Main(string[] args) { MyClass mc = new MyClass(); mc.Perform(); } } class MyClass { // 1. delegate 선언 private delegate void printInt2Consol(int i); public void Perform() { // 2. delegate 인스턴스 생성... 2023. 5. 14. C#, WinForms ] 폼 시작시, 종료시 이벤트 호출 순서 HTML 삽입 미리보기할 수 없는 소스 폼이 Show() 메소드로 호출되면 다음과 같은 순서로 이벤트가 발생한다. 1. 폼의 생성자(Constructor)가 호출된다. 2. 폼의 Load 이벤트(이벤트 핸들러)가 발생한다. 3. 폼의 Shown 이벤트(이벤트 핸들러)가 발생한다. 생성자는 Form 객체를 생성할 때 호출되며, Load 이벤트는 Form이 로드될 때 발생하고, Shown 이벤트는 폼이 처음 화면에 표시될 때 발생한다. 폼 Load 이벤트 호출 시점에는 폼과 관련된 컨트롤들이 메모리에 로드되고 초기화되지만, 아직 폼이 화면에 보이지는 않는 상태이다. 따라서 이 때 폼의 크기나 위치를 변경하거나, 다른 컨트롤의 위치나 크기를 폼에 맞게 조정할 수 있다. 또한 Form이 화면에 나타나기 전이므로.. 2023. 5. 14. C#, WinForms ] program.cs C# 프로그램 진입점 using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; namespace Appname1 { internal static class Program { /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } } using 문을 .. 2023. 5. 14. 이전 1 2 3 4 5 다음