C#

Study/C#, Winform

[C#] 바이트 배열 다루기 : BitConverter

C#을 다루면서 비트를 다룰 때 생각보다 많이 사용하게되는 BitConverter입니다. 데이터를 바이트 배열로 변환하거나 바이트 배열을 데이터로 변환하여 전달할 때 많이 쓰입니다. BitConverter Class // // 요약: // Converts base data types to an array of bytes, and an array of bytes to base // data types. public static class BitConverter BitConverter는 static class의 형태로 내부의 함수들 또한 static 함수로 이루어져 있습니다. BitConverter에서 자주 쓰이는 함수 (데이터를 Byte 배열로 변환) // bool 값을 byte[]로 변환 public s..

Study/C#, Winform

[C#] 바이트 배열을 이용한 비트 다루기 : BitArray

C#을 다루면서 비트를 다룰 때 생각보다 많이 사용하게되는 BitArray입니다. 바이트 배열을 넣어서 사용하기도 하고 비트의 개수를 넣어서 사용하기도 합니다. BitArray 생성자 using System.Collections; // BitArray를 사용하기 위함 // length 만큼의 길이를 가진 비트배열을 만듬 (모든 비트는 0) public BitArray(int length); // length 만큼의 길이를 가진 비트배열을 만들고 기본값을 defaultValue로 설정 public BitArray(int length, bool defaultValue) // 바이트 배열을 비트배열로 만듬 // 예) bytes = byte[2] -> BitArray를 16칸 자리를 만듬 // (BitArray..

Study/C#, Winform

[C# Winform] PropertyGrid 에서 DisplayName 우선순위

class PropData { [Category("Cell Information")] [DisplayName("Width")] public int Width { get; set; } = 0; [Category("Cell Information")] [DisplayName("Height")] public int Height { get; set; } = 0; [Category("Cell Information")] [DisplayName("Target X")] public double TargetX { get; set; } = 0.0; [Category("Cell Information")] [DisplayName("Target Y")] public double TargetY { get; set; } = 0.0;..

Study/C#, Winform

[C# Winform] DataGridView Column Header Drag (컬럼 헤더 드래그)

Winform 에서 데이터를 표로 나타낼때 주로 DataGridView를 사용합니다. 여기서 Column의 Header를 Drag 할 수 있게 하는 옵션이 존재합니다. dataGridView1.AllowUserToOrderColumns = true; 바로 AllowUserToOrderColumns 라는 옵션입니다. 이렇게 UI 컨트롤 속성에서도 제공하고 있는 옵션입니다. 이 옵션이 true인 경우 사용자가 Column의 Header를 Click한 채로 Drag하면 해당 Column이 옮겨집니다. public partial class Form1 : Form { public Form1() { InitializeComponent(); // DataGridView의 Name은 dataGridView1 dataG..

Study/C#, Winform

[C#] DateTime 시간 자르기, 반올림, 반내림, 가까운 값

C#에서 날짜 관련하여 DateTime을 많이 사용합니다. 이 DateTime을 자르거나 반올림, 반내림, 가까운 값을 자동으로 찾는 법을 알아봅시다. DateTime 자르기 /// /// 시간을 원하는 구간(timeSpan)으로 자르는 함수 /// 출처 : https://stackoverflow.com/a/1005222 /// /// 시간 /// 자를 TimeSpan /// public static DateTime Truncate(this DateTime dateTime, TimeSpan timeSpan) { if (timeSpan == TimeSpan.Zero) return dateTime; if (dateTime == DateTime.MinValue || dateTime == DateTime.Max..

Study/C#, Winform

[C#] Enumerable 에서 중복 데이터 삭제 (Distinct)

C#에서 Enumerable 형식의 데이터에서 중복을 없앨 때는 Distinct를 사용합니다. public static System.Collections.Generic.IEnumerable Distinct (this System.Collections.Generic.IEnumerable source); 원형은 위와 같습니다. 이것을 사용해보겠습니다. List list = new List { 1, 2, 3, 4, 4, 4, 5, 6 }; list.Distinct().ToList().ForEach(num => { System.Console.Write($"{num} "); }); System.Console.WriteLine(); double[] dArr = new double[] { 1.2, 3.3, 3.3, ..

Study/C#, Winform

[C# Winform] 스크롤바 크기 (넓이)

스크롤바 크기를 구하는 법은 다음과 같다. // 세로 스크롤바의 가로 넓이 int nVerticalWidth = System.Windows.Forms.SystemInformation.VerticalScrollBarWidth; // 가로 스크롤바의 세로 넓이 int nHorizontalWidth = System.Windows.Forms.SystemInformation.HorizontalScrollBarHeight;

Study/C#, Winform

[C# Winform] Context Menu를 Button 옆에 띄우기

Context Menu라는 것이 있습니다. 이런 메뉴를 Context Menu라고 합니다. 이 Context Menu를 버튼 옆에 띄워봅시다. 버튼을 하나 생성하고 이름(Name)은 btnTest라고 했습니다. 그리고 일반 Context Menu가 아닌 Context Menu Strip을 사용하겠습니다. 버튼을 더블 클릭하여 버튼 클릭 이벤트를 하나 만들어봅니다. private void btnTest_Click(object sender, EventArgs e) { // Context Menu Strip 생성 ContextMenuStrip contextMenuStrip = new ContextMenuStrip(); // 메뉴 아이템 1 ToolStripMenuItem menuItem = new ToolSt..

Study/C#, Winform

[C#] MemoryStream, StreamWriter, StreamReader, BinaryWriter, BinaryReader

MemoryStream MemoryStream은 메모리에 Byte 데이터를 순서대로 읽고 쓰는 작업을 수행합니다. // MemoryStream MemoryStream memoryStream = new MemoryStream(); // MemoryStream에 넣을 byte 데이터 byte[] byArrData1 = BitConverter.GetBytes(1234); // 1234는 int byte[] byArrData2 = BitConverter.GetBytes(5678); // 5678은 int // MemoryStream에 byArrData1을 적재 memoryStream.Write(byArrData1, 0, byArrData1.Length); // MemoryStream에 byArrData2을 적재..

Study/C#, Winform

[C#] 시간 관련 (DateTime, TimeSpan, Stopwatch)

DateTime (System.DateTime) C#에서 자주 사용하게 되는 시간 관련 구조체(Struct) 입니다. public DateTime(long ticks) public DateTime(long ticks, DateTimeKind kind) public DateTime(int year, int month, int day) public DateTime(int year, int month, int day, Calendar calendar) public DateTime(int year, int month, int day, int hour, int minute, int second) public DateTime(int year, int month, int day, int hour, int minute..

Eskeptor
'C#' 태그의 글 목록