2009-03-18 21 views

उत्तर

27

Form.ActiveControl जो भी आप चाहते हैं हो सकता है। इन पंक्तियों के साथ

+0

बस का कहना है कि यदि आप (उदाहरण के लिए एक .text गुण सेट) कुछ बदल गया है यह नियंत्रण है कि आप अंतिम बार उपयोग वापस आ जाएगी चाहते हैं। जिसमें चीजों को सेट करना शामिल है। टेक्स्ट को हमेशा ध्यान न दें जिस पर ध्यान केंद्रित किया गया है – PsychoData

-2

कुछ:

Protected Function GetFocusControl() As Control 
    Dim focusControl As Control = Nothing 

    ' Use this to get the Focused Control: 
    Dim focusHandle As IntPtr = GetFocus() 
    If IntPtr.Zero.Equals(focusHandle) Then   
     focusControl = Control.FromHandle(focusHandle) 
    End If 

    ' Note that it returns NOTHING if there is not a .NET control with focus 
    Return focusControl 
End Function 

मैं इस कोड windowsclient.net से आया लगता है, लेकिन यह कुछ समय तो हो गया है ... सी # में

+2

कृपया इसे पूरा करने तक मुझे जवाब को कम करने दें। 'गेटफोकस()' का कॉल कहीं भी नहीं है और इस फ़ॉर्म में उत्तर बस काम नहीं करेगा। – miroxlav

3

मैं यह कर:

 if (txtModelPN != this.ActiveControl) 
      txtModelPN.BackColor = Color.White; 

txtModelPN एक टेक्स्टबॉक्स है जिसे मैं एंटर और माउस पर हाइलाइट कर रहा हूं, छोड़ें, माउसलेव पर एंटर और डी-हाइलाइटिंग। सिवाय इसके कि यह वर्तमान नियंत्रण है, मैं पृष्ठभूमि को वापस सफेद पर सेट नहीं करता हूं।

वीबी बराबर की तरह इस

IF txtModelPN <> Me.ActiveControl Then 
    txtModelPN.BackColor = Color.White 
End If 
7

नोट होगा कि ActiveControl के लिए एक एकल कॉल पर्याप्त जब पदानुक्रम उपयोग किया जाता है नहीं है। कल्पना कीजिए:

Form 
    TableLayoutPanel 
     FlowLayoutPanel 
      TextBox (focused) 

(formInstance).ActiveControl, TableLayoutPanel के संदर्भ में वापस आ जाएगी नहीं TextBox

तो यह (पूर्ण प्रकटीकरण: this C# answer से अनुकूलित) का उपयोग

Function FindFocussedControl(ByVal ctr As Control) As Control 
    Dim container As ContainerControl = TryCast(ctr, ContainerControl) 
    Do While (container IsNot Nothing) 
     ctr = container.ActiveControl 
     container = TryCast(ctr, ContainerControl) 
    Loop 
    Return ctr 
    End Function 
1

आप इस का उपयोग कर सकते हैं नियंत्रण नाम से लगता है ।

If DataGridView1.Name = Me.ActiveControl.Name Then 
     TextBox1.Visible = True 
    Else 
     TextBox1.Visible = False 
    End If 
1

आप फॉर्म के ActiveControl गुणों का उपयोग कर सकते हैं और उस नियंत्रण का उपयोग कर सकते हैं।

me.ActiveControl 

या

Form.ActiveControl 
संबंधित मुद्दे

 संबंधित मुद्दे