2008-08-18 6 views
5

Windows Forms에서 객관식 옵션을 구현하는 가장 좋은 방법은 무엇입니까? 기본 값으로 시작하여 목록에서 단일 선택을 적용하려고합니다.WinForms 다중 선택

ComboBox가 좋은 선택 인 것처럼 보이지만 비어 있지 않은 기본값을 지정하는 방법이 있습니까?
코드에서 적절한 초기화 포인트를 설정할 수는 있지만 뭔가 빠져있는 것처럼 느껴집니다.

답변

8

그룹에서 하나의 답변 만 원하는 경우 RadioButton 컨트롤이 가장 적합하거나 많은 옵션이있는 경우 ComboBox를 사용할 수 있습니다. 기본값을 설정하려면 ComboBox의 컬렉션에 항목을 추가하고 SelectedIndex 또는 SelectedItem을 해당 항목으로 설정하기 만하면됩니다.

보고있는 옵션의 수에 따라 SelectionMode 속성이 MultiSimple로 설정된 ListBox를 사용할 수 있습니다 (다중 선택이거나 CheckBox 컨트롤을 사용할 수있는 경우).

2

사용 항목 후 ComboBox.SelectedItem 또는 SelectedIndex 속성이 기본 항목을 선택하기 위해 삽입되었다.

RadioButton 컨트롤을 사용하여 단일 옵션을 선택하는 것도 고려해 볼 수 있습니다.

2

DropDownStyle 속성이 DropDownList 및 SelectedIndex로 설정된 ComboBox를 0 (또는 기본 항목이 무엇이든)으로 사용할 수 있습니다. 이렇게하면 목록의 항목을 항상 선택해야합니다. 그렇게하는 것을 잊어 버리면 사용자는 편집 상자 부분에 다른 내용을 입력 할 수 있습니다. 이는 잘못된 것입니다.

1

작은 선택 목록을 제공하는 경우 라디오 버튼을 계속 사용하십시오. 그러나 동적 또는 긴 목록에 콤보 상자를 사용하려는 경우 스타일을 DropDownList로 설정하십시오.

private sub populateList(items as List(of UserChoices)) 
    dim choices as UserChoices 
    dim defaultChoice as UserChoices 

    for each choice in items 
     cboList.items.add(choice) 
     '-- you could do user specific check or base it on some other 
     '---- setting to find the default choice here 
     if choice.state = _user.State or choice.state = _settings.defaultState then 
      defaultChoice = choice 
     end if 
    next 
    '-- you chould select the first one 
    if cboList.items.count > 0 then 
     cboList.SelectedItem = cboList.item(0) 
    end if 

    '-- continuation of hte default choice 
    cboList.SelectedItem = defaultChoice 

end sub