IT/C#

[C#] ListBox Items or SelectedItems to String Array (Items.OfType<string>().ToArray())

Ella.J 2024. 11. 7. 13:06
728x90
반응형

 

 

실제 데이터 타입은 보통 컴포넌트 타입을 따라가거나 Object 형식으로 되어있음.

  • checkedListBox1.CheckedItems : CheckedListBox.CheckedItemCollection
  • listBox1.Items : ListBox.ObjectCollection
  • listBox1.SelectedItems : ListBox.SelectedObjectCollection

 

원하는 ObjectCollection을 .OfType<string>().ToArray()를 이용하여

string array로 변경할 수 있다.

 

1
2
3
4
5
6
7
8
9
10
string[] checkBoxCheckedData = checkedListBox1.CheckedItems.OfType<string>().ToArray();
//checkBoxCheckedData[0] = "Red"
//checkBoxCheckedData[1] = "Blue"
string[] listBoxData = listBox1.Items.OfType<string>().ToArray();
//listBoxData[0] = "Black"
//listBoxData[1] = "White"
//listBoxData[2] = "Pink"
//listBoxData[3] = "Purple"
string[] listBoxSelectedData = listBox1.SelectedItems.OfType<string>().ToArray();
//listBoxSelectedData[0] = "Black"
cs

 

 

 

Enumerable.OfType<TResult>(IEnumerable) 메서드 (System.Linq)

지정된 형식에 따라 IEnumerable의 요소를 필터링합니다.

learn.microsoft.com

 

 

728x90
반응형