2015-01-14 15 views
-1

현재 C# .net에서 구성 요소 포커스를 제어하는 ​​데 어려움을 겪고 있습니다. 내 목표는 다른 구성 요소 (예 : 텍스트 상자 B 또는 뭔가)를 "클릭"할 때 텍스트 상자 A의 포커스를 잃지 않도록하는 것입니다. 이 "초점"메커니즘은 텍스트 상자 C 또는 D ...가 클릭된다는 것을 제외하고는 유지됩니다.텍스트 상자의 포커스 손실 방지

enter image description here

이상한 소리를하지만 내 상황이 그 행 A, C의 각, D는입니다 DataRepeater 항목입니다, 그리고 난 단지, 오른쪽 열에서 B를 이동 "초점"수직이 아닌 할 수 있습니다

기둥.

그래서 C#에서이 작업을 수행하거나 데이터 리피터가 아닌 다른 컨트롤을 찾아야합니다. 도움이된다면 감사 할 것입니다.

+0

([한다 질문은 제목에 "태그"를 포함]을 참조 http://meta.stackoverflow.com/questions/19190/should- 질문이 포함 된 제목의 제목 포함), 합의가 "아니오, 그렇지 않아야합니다. – Junaith

+1

TextBox A의 LostFocus 이벤트에 연결하는 경우, 현재 어떤 컨트롤에 포커스가 있는지 확인하고 TextBox B인지 확인하십시오 또는 다른 '허용되지 않는'컨트롤은 단순히 TextBoxA.Focus()를 다시 호출합니까? – Tobias

+0

@ 비싸기 때문에 비용이 많이 드는 원인이 될 수 있습니다. 현재 초점을 맞추고있는 모든 컨트롤을 반복해야합니다. – Kingcesc

답변

0

TextBoxA의 Leave 이벤트는 포커스를 얻을 수있는 컨트롤이 포함 된 목록과 함께 사용할 수 있습니다.

[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Winapi)] 
    internal static extern IntPtr GetFocus(); 

    private Control GetFocusedControl() 
    { 
     Control focusedControl = null; 
     // To get hold of the focused control: 
     IntPtr focusedHandle = GetFocus(); 
     if (focusedHandle != IntPtr.Zero) 
      // Note that if the focused Control is not a .Net control, then this will return null. 
      focusedControl = Control.FromHandle(focusedHandle); 
     return focusedControl; 
    } 

    private void textBox3_Leave(object sender, EventArgs e) 
    { 
     //Any control that is allowed to acquire focus is just added to this array. 
     Control[] allowedToAcquireFocusControls = { 
       textBox1 
      }; 

     Control focusedControl = GetFocusedControl(); 

     if (!allowedToAcquireFocusControls.Contains(focusedControl)) 
     { 
      textBox3.Focus(); 
     } 
    } 

참조 : 내가 제목을 편집 한 What is the preferred way to find focused control in WinForms app?

관련 문제