2011-12-02 2 views
1

부모 폼에서로드하는 자식 폼이 있습니다. 부모 폼은 MDI 부모가 아닙니다. 다음 작업을 수행하고 싶습니다.C# Window Forms 컨트롤 스타일, 스타일을 통해 포커스 제거 변경

포커스가있는 컨트롤, 특히 Buttons 및 RadioButtons를 중심으로 점선/점선 사각형을 비활성화하고 싶습니다.

현재 나는 다음과 같은 코드를 사용하고 : 내하는 SetStyle 방법은 말할

public static void SetStyle(System.Windows.Forms.Control control, ControlStyles styles, 
    bool newValue) 
{ 
    // .. set control styles for the form 
    object[] args = { styles, newValue }; 

    typeof(System.Windows.Forms.Control).InvokeMember("SetStyle", 
     BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod, 
     null, control, args); 
} 

없이 어디

foreach (System.Windows.Forms.Control control in this.Controls) 
{ 
    // Prevent button(s) and RadioButtons getting focus 
    if (control is Button | control is RadioButton) 
    { 
     HelperFunctions.SetStyle(control, ControlStyles.Selectable, false); 
    } 
} 

는,이 작동하지 않습니다. 내가 여기서 무엇을 놓치고 있는지 잘 모르겠습니다. 모든 제안 및/또는 조언을 크게 주시면 감사하겠습니다.

답변

2

음, 나는 해결책의 종류를 발견했다고 생각합니다. 이 솔루션은 초점 사각형 문제를 해결하지만 게시 된 원본 코드의 문제점에 대해서는 대답하지 않습니다. 이것은 직사각형 문제에 대한 나의 이해입니다 ... 제가이 잘못을 저 지르면 저를 바로 잡으십시오.

대시/점선 사각형에는 컨트롤에 포커스가있는 것을 나타내는 사각형과 컨트롤이 기본 컨트롤임을 나타내는 사각형이있는 것처럼 보입니다. 이 두 상황 모두 컨트롤의 사용자 지정 클래스를 만들어야합니다. 제 경우에는 관심 컨트롤이 RadioButton입니다.

public class NoFocusRadioButton: RadioButton 
{ 
    // ---> If you DO NOT want to allow focus to the control, and want to get rid of 
    // the default focus rectangle around the control, use the following ... 

    // ... constructor 
    public NoFocusRadioButton() 
    { 
     // ... removes the focus rectangle surrounding active/focused radio buttons 
     this.SetStyle(ControlStyles.Selectable, false); 
    } 
} 

또는 다음을 사용하십시오 : I 입력 포커스를 허용하지 첫 번째 (생성자 코드) 방식을 사용하고

public class NoFocusRadioButton: RadioButton 
{ 
    // ---> If you DO want to allow focus to the control, and want to get rid of the 
    // default focus rectangle around the control, use the following ... 

    protected override bool ShowFocusCues 
    { 
     get 
     { 
      return false; 
     } 
    } 
} 

그래서 여기에 코드입니다.

이 모든 것이 사각형 문제를 해결하는 데 효과적이지만 초기 코드가 작동하지 않는 이유는 여전히 이해할 수 없습니다.

관련 문제