bool배열이름= Enumerable.Repeat(true or false, 배열 수).ToArray();

블로그 이미지

SherryBirkin

,

이해하기 쉽게 말하면

DataTable은 리스트뷰 (Detail) 이라고 생각하면 이해가 쉽다.


DataTable tb  = new DataTable();


리스트뷰(Detail)에서 필요한 부분을 생각해보면


ColumnHeader, Rows, Columns가 필요할거고 각 항목별 값이 필요할것이다.


이해하기 쉽도록 풀어서 설명하는것..



 컬럼헤더1

 컬럼헤더2

 일

 1 

 이

 2 

 삼

 3 


위와 같은 형태로 만들고 싶다라고 한다면


row중 첫번째는 string이고, 그 다음은 int이므로 지정한다.

tb.Columns.Add("컬럼헤더1" , typeof(string));

tb.Columns.Add("컬럼헤더2", typeof(int));



DataRow dr = dt.NewRow();

dt에서 새로울 열을 만들것이기 때문에 dt로 만들어 주고


dr["컬럼헤더1"] = "일";

dr["컬럼헤더2"] = 1;

위와같이 지정하면


dr에 두개의 값이 저장된다

dr에 필요한것이 아니고 dt에 필요하므로


dt.Rows.Add(dr);

열을 추가해주면, DataTable에 저장된것이다.


저장된 DataTable(tb)를 ComboBox나 DataSource를 사용하는 곳에 쓸 수 있다.

블로그 이미지

SherryBirkin

,

ComboBox에서 각 항목별로 Tag를 등록해서 보여지는 값이 아닌

특정된 값을 가지고 오고 싶을때 사용 하는 방법


Ex) 일

     이

     삼

     사

     오


위와같이 

콤보박스에 보여지는데

선택했을때 1,2,3,4,5를 가지고 오는 방법..


Items.Add로 추가하지말고


DataTable을 이용해서 DataBinding시켜야 한다.


DataTable은 이해하기 쉽게 말하면 엑셀이나, 리스트라고 생각하면 쉽다


DataTable dt = new DataTable();


//

//    DataTable에 추가될 내용

//


필요한 데이터를 추가 한 후에


아래와 같이 코드를 작성하면


comboBox1.DataSource = dt;

comboBox1.ValueMember = "숨겨놓을 DataTable의 값";

comboBox1.DisplayMember = "보여질 DataTable의 컬럼헤더명";


보여질 값만 콤보박스에 보이고


선택한 항목에서 값을 가지고 오려면


comboBox1.SelectedItem.value로 가지고 오면 1,2,3,4,5를 가지고 올 수 있다.

'Programming > C#' 카테고리의 다른 글

bool 배열 한번에 초기화 하기  (0) 2016.11.23
DataTable 사용방법  (0) 2016.08.14
xml Element  (0) 2016.08.02
TrayIcon 좌클릭, 우클릭 메뉴 가능하게 하기  (0) 2016.04.25
메시지박스 자동 종료하기  (0) 2016.04.24
블로그 이미지

SherryBirkin

,

xml Element

Programming/C# 2016. 8. 2. 16:50

xml 내가사용했던것만.. 더 있지만 간단한것부터..


<1>                           <----------   Element

<2></2>               <----------- Element 1의  Child

</1>


// 소스는 아니니 참고하여 파일에서 가져오는지 아닌지 확인하고 만들것.


Element 객체를 만들고 CreateElement 1(<1>을 사용했기때문)

elementName.AppendChild(CreateNode 2)

하면 위와 같은 형태로 저장 할 수 있다.


elementName.AppendChild(CreateNode 3)을 하나 더 썻다면.

<1>                           <----------   Element

<2></2>               <----------- Element 1의  Child

<3></3>

</1>

위와 같은 형태가 된다.


<1>

<2>

<3>

</3>

</2>

</1>


위와 같은 형태로 만들고 싶다면


Element 객체를 3개를 만들어야 하며,  InnerText 를 이용하여

하위 Element를 포함해주어야 한다.

블로그 이미지

SherryBirkin

,

TrayIcon에서 


우클릭했을떄랑, 좌클릭했을때 ContextMenu를 다르게 보는 방법


private void notifyIcon1_MouseClick(object senderMouseEventArgs e)

        {

            if (e.Button == MouseButtons.Left)

            {

                notifyIcon1.ContextMenuStrip = this.contextMenuStrip1;

                System.Reflection.MethodInfo methodInfo = 

                       typeof(NotifyIcon).GetMethod("ShowContextMenu"

                       System.Reflection.BindingFlags.Instance | 

System.Reflection.BindingFlags.NonPublic);

                methodInfo.Invoke(notifyIcon1null);

            }

            else if (e.Button == MouseButtons.Right)

            {

                notifyIcon1.ContextMenuStrip = contextMenuStrip2;

                System.Reflection.MethodInfo methodInfo = 

                       typeof(NotifyIcon).GetMethod("ShowContextMenu",

                        System.Reflection.BindingFlags.Instance |

                           System.Reflection.BindingFlags.NonPublic);

                methodInfo.Invoke(notifyIcon1null);

            }

        }




코드에 관한 설명 - 질문자 우시와카는 본인임.


http://www.hoons.net/Board/QACSHAP/Content/31613?Key=Name&Value=%EC%9A%B0%EC%8B%9C%EC%99%80%EC%B9%B4

'Programming > C#' 카테고리의 다른 글

DataTable 사용방법  (0) 2016.08.14
ComboBox에 항목별로 Tag를 사용하는 방법 (DataTable)  (0) 2016.08.14
xml Element  (0) 2016.08.02
메시지박스 자동 종료하기  (0) 2016.04.24
DataGridView RowHeader에 숫자 넣는법  (0) 2016.02.26
블로그 이미지

SherryBirkin

,

종료버튼 눌렀을 때 메시지 박스가 뜨면,


1000ms(1s)후에 자동 종료되는 형태이다.


다른 방법도 있지만 Action으로 사용 하는 방법


private void MessageBoxFormClosing(Form fm)
{
    if (fm.InvokeRequired)
    {
        Action<Form> closeform = new Action<Form>(MessageBoxFormClosing);
        this.Invoke(closeform, fm);
    }
    else
    {
        if (fm != null)
            fm.Close();
    }
}
 
private void button1_Click(object sender, EventArgs e)
{
    Form msgfm = new Form();
    Action clse = new Action(() = >
    {
        while (true)
        {
            System.Threading.Thread.Sleep(1000);
            break;
        }
        MessageBoxFormClosing(msgfm);
    });
    clse.BeginInvoke(ir = > clse.EndInvoke(ir), null);
    MessageBox.Show(msgfm, "닫혀~");
}


블로그 이미지

SherryBirkin

,

DataGridView 이벤트에서 RowPostPaint를 추가하고


아래와 같이 코드를 넣으면 된다.

// 주석은 16진수 (4자리)


private void Gridview_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
            DataGridView grid = sender as DataGridView;
            //String rowIdx = (e.RowIndex + 1).ToString("X4");
            String rowIdx = (e.RowIndex + 1).ToString();
 
            StringFormat centerFormat = new StringFormat()
            {
                Alignment = StringAlignment.Center,
                LineAlignment = StringAlignment.Center
            };
 
            Rectangle headerBounds = new Rectangle(e.RowBounds.Left, e.RowBounds.Top,
                                                    grid.RowHeadersWidth, e.RowBounds.Height);
            e.Graphics.DrawString(rowIdx, this.Font, SystemBrushes.ControlText,
                                  headerBounds,centerFormat);
}


블로그 이미지

SherryBirkin

,