반응형
블로그 이미지
sense.45

공지사항

최근에 올라온 글

최근에 달린 댓글

글 보관함

calendar

1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28

C# ListBox에 색깔넣기

2018. 4. 6. 19:14 | Posted by sense.45
반응형

C# ListBox에 색을 넣으면 가독성이 좋아집니다.


색을 넣어 보겠습니다.



끝.




ㅎㅎ





1. Class를 하나 만듭니다.

public class MyListBoxItem

{

        public MyListBoxItem(Color c, string m)

        {

            ItemColor = c;

            Message = m;

        }

        public Color ItemColor { get; set; }

        public string Message { get; set; }

}



2. 리스트박스의 DrawItem 이벤트를 추가합니다.



        private void listBox1_DrawItem(object sender, DrawItemEventArgs e)

        {

            if (e.Index == -1)   return;    //아이템이 없는 경우 는 할 일이 없습니다.


            MyListBoxItem item = listBox1.Items[e.Index] as MyListBoxItem; 

            if (item != null)

            {

                e.Graphics.DrawString( 

                    item.Message, 

                    listBox1.Font, 

                    new SolidBrush(item.ItemColor), 

                    0, 

                    e.Index * listBox1.ItemHeight 

                );

            }

            else

            {

                // The item isn't a MyListBoxItem, do something about it

            }


        }



3. 이제 버턴이벤트나 기타 모듈에서 아이템을 추가합니다.


listBox1.Items.Add(new MyListBoxItem(Color.Red, "빨간색 ==== Red Color"));

listBox1.Items.Add(new MyListBoxItem(Color.Blue, "파란색 ==== Blue Color"));



이상태에서 실행하면 



이렇게 나옵니다......ㅠㅠ





DrawMode 를o OwnerDrawFixed로 변경하여야 합니다.

(Normal만 아니면 됩니다.)




출처

https://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.drawitem(v=vs.110).aspx


https://stackoverflow.com/questions/6896151/c-sharp-change-listbox-items-color


https://stackoverflow.com/questions/29173517/winforms-simplest-way-to-change-listbox-text-color-on-the-fly



반응형

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

c# List FindIndex  (0) 2018.04.04
C#ListBox에서 아래로 스크롤 하기  (0) 2018.04.02
c#배열에 SqlDataReader 결과를 넣는 방법  (0) 2018.03.30
DLL 버전이 알고 싶은 경우  (0) 2018.03.22

c# List FindIndex

2018. 4. 4. 12:53 | Posted by sense.45
반응형

c#에서 List를 사용하면 다양한 기능이 많아 코드량이 상당히 줄어듭니다.


 List <int> i = new List<i>;


i.add(1);


int j = i,findindex(1);


j는 당연히 0입니다.


findindex는 도움이 많이 될듯한데...


문제는 int가 아니고 class를 넣는 경우는 문제가 다름니다.


class c

{

int i;

string s;

}



List<c>lc = new List<c>;


lc.Add(new c());

lc.Add(new c());


lc[0].i=100;

lc[1].i=200;



이때 100이 있는 list를 찿고 싶을때


방법이 3가지입니다.

1. for문을 사용하는 경우

2. lambdas를 사용하지 않는경우

3. lambdas를 사용하는경우



1.번은 생략.


2.lambdas를 사용하지 않는경우


   int index = lc.FindIndex(

                delegate(c c1)

                {

                    return c1.i(100);

                });





3. lambdas를 사용하는경우

int  index = lc.FindIndex(r => r.i(200));




출처 : https://stackoverflow.com/questions/1568593/how-to-use-indexof-method-of-listobject


http://www.csharp-examples.net/list/



반응형

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

C# ListBox에 색깔넣기  (0) 2018.04.06
C#ListBox에서 아래로 스크롤 하기  (0) 2018.04.02
c#배열에 SqlDataReader 결과를 넣는 방법  (0) 2018.03.30
DLL 버전이 알고 싶은 경우  (0) 2018.03.22

C#ListBox에서 아래로 스크롤 하기

2018. 4. 2. 15:35 | Posted by sense.45
반응형

C# 리스트박스에서 아이템을 계속 추가하면


디폴트상태에서는 화면 아래로 보이지 않는 상태에서 계속 add됨니다.


이때 화면 아래로 계속 스크롤 되면서 추가 하고 싶을때는 다음과 같이 하면 됩니다.


int visibleItems = listBox.ClientSize.Height / listBox.ItemHeight;
listBox.TopIndex = Math.Max(listBox.Items.Count - visibleItems + 1, 0);


출처 https://stackoverflow.com/questions/8796747/how-to-scroll-to-bottom-of-listbox




반응형

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

C# ListBox에 색깔넣기  (0) 2018.04.06
c# List FindIndex  (0) 2018.04.04
c#배열에 SqlDataReader 결과를 넣는 방법  (0) 2018.03.30
DLL 버전이 알고 싶은 경우  (0) 2018.03.22

c#배열에 SqlDataReader 결과를 넣는 방법

2018. 3. 30. 19:36 | Posted by sense.45
반응형
DataTable dt = new DataTable();
using (SqlConnection conn = new SqlConnection(this.ConnectionString))
{
    SqlCommand sc = new SqlCommand("select * from whatever", conn);
    conn.Open();
    SqlDataReader dr = sc.ExecuteReader();
    dt.Load(dr);
    conn.Close();
}
    
int[] arrInt = dt.AsEnumerable().Select(row => row.Field<int>("X")).ToArray();


DB조회 결과를 배열에 넣을려고 시도하려면


배열이 초기화되지 않으면 오류발생한다.


이때 select count(*) from ... 하여 두번 조회하라는 답도 있더라....만.


위의 예제는 DataTable에 값을 받아 바로 배열에 넣는 방법을 사용하고 있슴다.


상당히 도움되는 예제였습니다.


원본 https://social.msdn.microsoft.com/Forums/vstudio/en-US/22ee355d-f260-46c7-adf2-16ca60086174/datareader-to-array?forum=csharpgeneral



반응형

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

C# ListBox에 색깔넣기  (0) 2018.04.06
c# List FindIndex  (0) 2018.04.04
C#ListBox에서 아래로 스크롤 하기  (0) 2018.04.02
DLL 버전이 알고 싶은 경우  (0) 2018.03.22

DLL 버전이 알고 싶은 경우

2018. 3. 22. 14:01 | Posted by sense.45
반응형
Assembly assembly = Assembly.LoadFrom("MyAssembly.dll");
Version ver = assembly.GetName().Version;


출처 : https://stackoverflow.com/questions/1755504/programmatically-get-the-version-number-of-a-dll

반응형

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

C# ListBox에 색깔넣기  (0) 2018.04.06
c# List FindIndex  (0) 2018.04.04
C#ListBox에서 아래로 스크롤 하기  (0) 2018.04.02
c#배열에 SqlDataReader 결과를 넣는 방법  (0) 2018.03.30
이전 1 다음