본문 바로가기

WinForms17

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.
Winforms ] ToopStrip, MenuStrip 속성 ToolStrip 화살표클릭 - Dock 을 통해 ToolStrip의 위치를 정할 수 있으며 ToolStrip size는 Form의 size에 맞게 자동 조정된다. 그리고 아래 버튼을 클릭해 다양한 컨트롤을 ToolStrip 안에 추가할 수 있다. ToolStrip 을 선택하며 속성을 변경할 수 있고, ToolStrip 내의 컨트롤을 클릭하여 각자 속성을 변경할 수도 있다. ToolStrip 내의 컨트롤을 우클릭하여 주요 속성을 변경할 수도 있는데 ToolStripButton 의 경우 DisplayStyle 을 Text/Image/ImageAndText 중에서 선택할 수 있다. AutoSize 를 False로 하면 ToolStrip의 크기를 임의로 조절할 수 있다. 우클릭 - 이미지 설정 또는 속성의 I.. 2023. 5. 14.
WinForms ] 콘솔창 출력 솔루션 우클릭 Properties - Application 탭 - Output type : Console Application 선택 이제 Console.WriteLine();으로 출력할 수 있다. 2023. 5. 14.
WinForms ] ImageList와 ColorDepth 속성 ImageList는 이미지를 저장하고 관리하는 데 사용되는 컨트롤인데, ImageList를 사용하면 다른 컨트롤에서 이미지를 쉽게 가져올 수 있으며, 동일한 이미지를 여러 컨트롤에서 사용할 수 있다. 도구상자에서 imageList를 추가하면 하단에 imageList가 나타난다. 우클릭 - 이미지 선택 아래 이미지 컬렉션 편집기에서 imageList의 멤버를 추가할 수있다. 0번 인덱스부터 추가되며 순서 변경이 가능하다. 이렇게 추가된 이미지 리스트를 Button, CheckBox, ComboBox, Label, ListBox, ListView, MenuStrip, PictureBox, RadioButton, TabControl, ToolBar 컨트롤 등 다양한 컨트롤에서 불러다 쓸 수 있다. 사용시 주의.. 2023. 5. 7.
WinForms ] Label vs TextBox 컨트롤 결론 : 사용자가 편집할 수 있게끔 하려면 TextBox, 아닌 경우 Label 사용 추천 There are a few pro's and con's to both. Label Pro's: Text is not copy able Cursor does not change Sets size based on text (if autosize is on, I think its on by default) Option to align text to the right (autosize off) Con's: Text is not selectable/copy able Text might outgrow form/parent with autosize TextBox Pro's: Text is copy able Fixed size.. 2023. 4. 9.
C#, WinForms ] label의 너비를 초과하는 글자 자르기 Graphics 객체를 사용해 사이즈를 구하고, 레이블 너비를 초과한 경우 Substring 메서드 사용해서 마지막 글자 지워버림 Substring 첫번째 매개변수는 추출할 문자열의 시작 인덱스, 두번째 매개변수는 추출할 문자열의 길이임. 초과되지 않게 주의 Graphics g = this.label1.CreateGraphics(); SizeF textSize = g.MeasureString(this.label1.Text, this.label1.Font); if(this.label1.Width < textSize.Width) { this.label1.Text = this.label1.Text.Substring(0, this.label1.Text.Length - 1); } 2023. 2. 25.
C#, WinForms ] MessageBox 속성 private void button2_Click(object sender, EventArgs e) { MessageBox.Show("Do you want to delete?", "DELETE", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1); } 2023. 2. 25.