유용한 정보

[C#] 유용한 기능

DevReff 2024. 12. 25. 11:09
728x90
SMALL
반응형

1. DataGridViewComboBoxCell의 Value값 변경하기
2. 진행율을 표시하는 클래스   
3. 폼의 중복 방지
4. 열려있는 폼 닫기
5. DataGridView 에서 두개의 행으로 제목 만들기

[ProgressStatus.cs]

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;

/// <summary>
/// 윈도우의 컨트롤 관련된 클래스
/// </summary>
namespace WooriUtils.Controls
{
 /// <summary>
 /// 대기상태의 진행상황을 보여준다.
 /// </summary>
 public partial class ProgressStatus : Form
 {
  DateTime StartTime = DateTime.Now; /// 시작시간
  object Data = null; /// 대기 상태를 보여주는 데이터
  //public bool IsPause = false;

  /// <summary>
  /// 생성자
  /// </summary>
  /// <param name="whyWeAreWaiting">제목(기다려야 하는 이유)</param>
  /// <param name="work">백그라운드 작업자</param>
  /// <param name="data">대기상태를 보여주는 데이터</param>
  public ProgressStatus(string whyWeAreWaiting, DoWorkEventHandler work, object data)
  {
   InitializeComponent();


   this.StartTime = DateTime.Now;
   this.ED_TIME.Text = string.Format("{0}", this.StartTime.ToString("yyyy-MM-dd HH:mm:ss"));
   this.Data = data;
   this.ST_MSG.Text = whyWeAreWaiting;
   this.BGW_WORKER.DoWork += work;
  }

  /// <summary>
  /// 대기상태 시작
  /// </summary>
  /// <param name="maxvalue">최대값</param>
  /// <param name="initvalue">초기값</param>
  public void Start(double maxvalue, double initvalue = 0)
  {
   this.PB_STATUS.Minimum = (int)initvalue;
   this.PB_STATUS.Maximum = (int)maxvalue;
   this.PB_STATUS.Value = (int)initvalue;
  }
  /// <summary>
  /// 대기상태 종료
  /// </summary>
  public void Finished()
  {
   try
   {
    this.ED_TIME.Text = string.Format("{0}", (DateTime.Now - this.StartTime).ToString("yyyy-MM-dd HH:mm:ss"));
    this.PB_STATUS.Value = PB_STATUS.Maximum;
   }
   catch (Exception ex)
   {
    WooriUtils.Log.WriteLine(ex.Message);
   }
  }

  /// <summary>
  /// 진행상황 데이터를 로딩한다.
  /// </summary>
  /// <param name="sender">대기상태 호출자</param>
  /// <param name="e">이벤트</param>
  private void ProgressStatus_Load(object sender, EventArgs e)
  {
   ///// 투명하게 하기 위해 라벨컨트롤이 얹혀있는 상위 컨트롤 이름
   //ST_STATUS.Parent = this.PB_STATUS;
   ///// 진행율을 표시하는 문자의 배경을 투명하게 한다.
   //ST_STATUS.BackColor = Color.Transparent;
   //PB_STATUS.CreateGraphics().DrawString(percent.ToString() + "%"
   // , new Font("Arial", (float)8.25, FontStyle.Regular), Brushes.Black, new PointF(PB_STATUS.Width / 2 - 10, PB_STATUS.Height / 2 - 7));

   this.BGW_WORKER.RunWorkerAsync(this.Data);
  }

  /// <summary>
  /// 진행상황 데이터를 보여준다.
  /// </summary>
  /// <param name="sender">대기상태 호출자</param>
  /// <param name="e">이벤트</param>
  private void BGW_WORKER_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  {
   this.DialogResult = System.Windows.Forms.DialogResult.OK;
   Close();
  }

  /// <summary>
  /// 진행상황이 변경되었을 때 발생하는 이벤트 함수
  /// </summary>
  /// <param name="sender">대기상태 호출자</param>
  /// <param name="e">이벤트</param>
  private void BGW_WORKER_ProgressChanged(object sender, ProgressChangedEventArgs e)
  {
   try
   {
    this.PB_STATUS.Value = e.ProgressPercentage;

    if (e.UserState is string)
     ST_STATUS.Text = e.UserState as string;
    else if (e.UserState is object[])
    {
     object[] msg = e.UserState as object[];
     if (msg.Count() > 0)
      ST_STATUS.Text = msg[0] as string;
     if (msg.Count() > 1)
      this.ED_SUBMSG.Text = msg[1] as string;
     if (msg.Count() > 2)
      this.ST_MSG.Text = msg[2] as string;
    }
    this.ED_TIME.Text = string.Format("{0} => {1}"
     , this.StartTime.ToString("yyyy-MM-dd HH:mm:ss"), (DateTime.Now - this.StartTime).ToString());
   }
   catch (Exception ex)
   {
    WooriUtils.Log.WriteLine(ex.Message);
    this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
    Close();
   }
  }

  /// <summary>
  /// 대기상태를 종료한다.
  /// </summary>
  /// <param name="sender">대기상태 호출자</param>
  /// <param name="e">이벤트</param>
  private void BTN_CANCEL_Click(object sender, EventArgs e)
  {
   try
   {
    this.ST_MSG.Text = "취소중입니다.";

    this.BGW_WORKER.CancelAsync();

    this.BTN_CANCEL.Enabled = false;

    this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
   }
   catch (Exception ex)
   {
    WooriUtils.Log.WriteLine(ex.Message);
   }
  }

  /// <summary>
  /// 대기상태를 일시정지한다.
  /// </summary>
  /// <param name="sender">대기상태 호출자</param>
  /// <param name="e">이벤트</param>
  private void BTN_PAUSE_Click(object sender, EventArgs e)
  {
   //if (IsPause)
   //{
   // this.ST_MSG.Text = "진행합니다.";

   // this.BTN_PAUSE.Text = "시작";
   // IsPause = false;
   //}
   //else
   //{
   // this.ST_MSG.Text = "정지중입니다.";

   // this.BTN_PAUSE.Text = "진행";
   // IsPause = true;
   //}
  }
 }
}

[ProgressStatus.Designer.cs]

/// <summary>
/// 윈도우의 컨트롤 관련된 클래스
/// </summary>
namespace WooriUtils.Controls
{
 partial class ProgressStatus
 {
  /// <summary>
  /// Required designer variable.
  /// </summary>
  private System.ComponentModel.IContainer components = null;

  /// <summary>
  /// Clean up any resources being used.
  /// </summary>
  /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  protected override void Dispose(bool disposing)
  {
   if (disposing && (components != null))
   {
    components.Dispose();
   }
   base.Dispose(disposing);
  }

  #region Windows Form Designer generated code

  /// <summary>
  /// Required method for Designer support - do not modify
  /// the contents of this method with the code editor.
  /// </summary>
  private void InitializeComponent()
  {
   this.PB_STATUS = new System.Windows.Forms.ProgressBar();
   this.ST_STATUS = new System.Windows.Forms.Label();
   this.ST_MSG = new System.Windows.Forms.Label();
   this.BGW_WORKER = new System.ComponentModel.BackgroundWorker();
   this.BTN_CANCEL = new System.Windows.Forms.Button();
   this.BTN_PAUSE = new System.Windows.Forms.Button();
   this.ED_SUBMSG = new System.Windows.Forms.TextBox();
   this.ED_TIME = new System.Windows.Forms.TextBox();
   this.PB_STATUS.SuspendLayout();
   this.SuspendLayout();
   //
   // PB_STATUS
   //
   this.PB_STATUS.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
            | System.Windows.Forms.AnchorStyles.Right)));
   this.PB_STATUS.Controls.Add(this.ST_STATUS);
   this.PB_STATUS.Location = new System.Drawing.Point(12, 81);
   this.PB_STATUS.Name = "PB_STATUS";
   this.PB_STATUS.Size = new System.Drawing.Size(708, 26);
   this.PB_STATUS.TabIndex = 0;
   //
   // ST_STATUS
   //
   this.ST_STATUS.BackColor = System.Drawing.SystemColors.ControlLight;
   this.ST_STATUS.ForeColor = System.Drawing.SystemColors.ControlText;
   this.ST_STATUS.Location = new System.Drawing.Point(318, 4);
   this.ST_STATUS.Name = "ST_STATUS";
   this.ST_STATUS.Size = new System.Drawing.Size(73, 18);
   this.ST_STATUS.TabIndex = 3;
   this.ST_STATUS.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
   //
   // ST_MSG
   //
   this.ST_MSG.Location = new System.Drawing.Point(12, 13);
   this.ST_MSG.Name = "ST_MSG";
   this.ST_MSG.Size = new System.Drawing.Size(582, 18);
   this.ST_MSG.TabIndex = 1;
   this.ST_MSG.Text = "잠시만 기다려주세요!!!";
   //
   // BGW_WORKER
   //
   this.BGW_WORKER.WorkerReportsProgress = true;
   this.BGW_WORKER.WorkerSupportsCancellation = true;
   this.BGW_WORKER.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(this.BGW_WORKER_ProgressChanged);
   this.BGW_WORKER.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.BGW_WORKER_RunWorkerCompleted);
   //
   // BTN_CANCEL
   //
   this.BTN_CANCEL.Location = new System.Drawing.Point(663, 8);
   this.BTN_CANCEL.Name = "BTN_CANCEL";
   this.BTN_CANCEL.Size = new System.Drawing.Size(57, 23);
   this.BTN_CANCEL.TabIndex = 2;
   this.BTN_CANCEL.Text = "취소";
   this.BTN_CANCEL.UseVisualStyleBackColor = true;
   this.BTN_CANCEL.Click += new System.EventHandler(this.BTN_CANCEL_Click);
   //
   // BTN_PAUSE
   //
   this.BTN_PAUSE.Location = new System.Drawing.Point(600, 8);
   this.BTN_PAUSE.Name = "BTN_PAUSE";
   this.BTN_PAUSE.Size = new System.Drawing.Size(57, 23);
   this.BTN_PAUSE.TabIndex = 2;
   this.BTN_PAUSE.Text = "정지";
   this.BTN_PAUSE.UseVisualStyleBackColor = true;
   this.BTN_PAUSE.Visible = false;
   this.BTN_PAUSE.Click += new System.EventHandler(this.BTN_PAUSE_Click);
   //
   // ED_SUBMSG
   //
   this.ED_SUBMSG.BorderStyle = System.Windows.Forms.BorderStyle.None;
   this.ED_SUBMSG.Location = new System.Drawing.Point(292, 35);
   this.ED_SUBMSG.Multiline = true;
   this.ED_SUBMSG.Name = "ED_SUBMSG";
   this.ED_SUBMSG.ReadOnly = true;
   this.ED_SUBMSG.Size = new System.Drawing.Size(428, 43);
   this.ED_SUBMSG.TabIndex = 6;
   //
   // ED_TIME
   //
   this.ED_TIME.BorderStyle = System.Windows.Forms.BorderStyle.None;
   this.ED_TIME.Location = new System.Drawing.Point(12, 35);
   this.ED_TIME.Multiline = true;
   this.ED_TIME.Name = "ED_TIME";
   this.ED_TIME.ReadOnly = true;
   this.ED_TIME.Size = new System.Drawing.Size(274, 43);
   this.ED_TIME.TabIndex = 6;
   //
   // ProgressStatus
   //
   this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 12F);
   this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
   this.ClientSize = new System.Drawing.Size(732, 119);
   this.ControlBox = false;
   this.Controls.Add(this.ED_TIME);
   this.Controls.Add(this.ED_SUBMSG);
   this.Controls.Add(this.PB_STATUS);
   this.Controls.Add(this.BTN_PAUSE);
   this.Controls.Add(this.BTN_CANCEL);
   this.Controls.Add(this.ST_MSG);
   this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
   this.MaximizeBox = false;
   this.MinimizeBox = false;
   this.Name = "ProgressStatus";
   this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
   this.Text = "진행상태";
   this.Load += new System.EventHandler(this.ProgressStatus_Load);
   this.PB_STATUS.ResumeLayout(false);
   this.ResumeLayout(false);
   this.PerformLayout();

  }

  #endregion

  private System.Windows.Forms.ProgressBar PB_STATUS;
  private System.Windows.Forms.Label ST_MSG;
  private System.ComponentModel.BackgroundWorker BGW_WORKER;
  private System.Windows.Forms.Button BTN_CANCEL;
  private System.Windows.Forms.Label ST_STATUS;
  private System.Windows.Forms.Button BTN_PAUSE;
  private System.Windows.Forms.TextBox ED_SUBMSG;
  private System.Windows.Forms.TextBox ED_TIME;
 }
}

 

[ProgressStatus.resx]

<?xml version="1.0" encoding="utf-8"?>
<root>
  <!--
    Microsoft ResX Schema
   
    Version 2.0
   
    The primary goals of this format is to allow a simple XML format
    that is mostly human readable. The generation and parsing of the
    various data types are done through the TypeConverter classes
    associated with the data types.
   
    Example:
   
    ... ado.net/XML headers & schema ...
    <resheader name="resmimetype">text/microsoft-resx</resheader>
    <resheader name="version">2.0</resheader>
    <resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
    <resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
    <data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
    <data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
    <data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
        <value>[base64 mime encoded serialized .NET Framework object]</value>
    </data>
    <data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
        <value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
        <comment>This is a comment</comment>
    </data>
               
    There are any number of "resheader" rows that contain simple
    name/value pairs.
   
    Each data row contains a name, and value. The row also contains a
    type or mimetype. Type corresponds to a .NET class that support
    text/value conversion through the TypeConverter architecture.
    Classes that don't support this are serialized and stored with the
    mimetype set.
   
    The mimetype is used for serialized objects, and tells the
    ResXResourceReader how to depersist the object. This is currently not
    extensible. For a given mimetype the value must be set accordingly:
   
    Note - application/x-microsoft.net.object.binary.base64 is the format
    that the ResXResourceWriter will generate, however the reader can
    read any of the formats listed below.
   
    mimetype: application/x-microsoft.net.object.binary.base64
    value   : The object must be serialized with
            : System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
            : and then encoded with base64 encoding.
   
    mimetype: application/x-microsoft.net.object.soap.base64
    value   : The object must be serialized with
            : System.Runtime.Serialization.Formatters.Soap.SoapFormatter
            : and then encoded with base64 encoding.

    mimetype: application/x-microsoft.net.object.bytearray.base64
    value   : The object must be serialized into a byte array
            : using a System.ComponentModel.TypeConverter
            : and then encoded with base64 encoding.
    -->
  <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
    <xsd:element name="root" msdata:IsDataSet="true">
      <xsd:complexType>
        <xsd:choice maxOccurs="unbounded">
          <xsd:element name="metadata">
            <xsd:complexType>
              <xsd:sequence>
                <xsd:element name="value" type="xsd:string" minOccurs="0" />
              </xsd:sequence>
              <xsd:attribute name="name" use="required" type="xsd:string" />
              <xsd:attribute name="type" type="xsd:string" />
              <xsd:attribute name="mimetype" type="xsd:string" />
              <xsd:attribute ref="xml:space" />
            </xsd:complexType>
          </xsd:element>
          <xsd:element name="assembly">
            <xsd:complexType>
              <xsd:attribute name="alias" type="xsd:string" />
              <xsd:attribute name="name" type="xsd:string" />
            </xsd:complexType>
          </xsd:element>
          <xsd:element name="data">
            <xsd:complexType>
              <xsd:sequence>
                <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
                <xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
              </xsd:sequence>
              <xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
              <xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
              <xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
              <xsd:attribute ref="xml:space" />
            </xsd:complexType>
          </xsd:element>
          <xsd:element name="resheader">
            <xsd:complexType>
              <xsd:sequence>
                <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
              </xsd:sequence>
              <xsd:attribute name="name" type="xsd:string" use="required" />
            </xsd:complexType>
          </xsd:element>
        </xsd:choice>
      </xsd:complexType>
    </xsd:element>
  </xsd:schema>
  <resheader name="resmimetype">
    <value>text/microsoft-resx</value>
  </resheader>
  <resheader name="version">
    <value>2.0</value>
  </resheader>
  <resheader name="reader">
    <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
  </resheader>
  <resheader name="writer">
    <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
  </resheader>
  <metadata name="BGW_WORKER.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
    <value>17, 17</value>
  </metadata>
</root>

            






출처: https://use1348.tistory.com/64 [유용한 정보:티스토리]