2008-10-14 16 views
21

मेरे पास "साफ़ करें" बटन वाला एक फॉर्म है।वीबीएनईटी - कंटेनर ऑब्जेक्ट में नियंत्रण के माध्यम से इटरेटिंग

जब उपयोगकर्ता "साफ़ करें" पर क्लिक करता है, तो मैं फ़ॉर्म पर सभी दृश्य तत्वों के मान को साफ़ करना चाहता हूं। दिनांक नियंत्रण के मामले में, मैं उन्हें वर्तमान तिथि पर रीसेट करना चाहता हूं।

मेरे सभी नियंत्रण एक पैनल पर निहित हैं।

अभी, मैं इसे नीचे दिए गए कोड के साथ कर रहा हूं। क्या प्रत्येक नियंत्रण प्रकार की मैन्युअल रूप से जांच करने से कोई आसान तरीका है? यह विधि अत्यधिक अनावश्यक लगती है।

उप-कंटेनर (यानी, पैनल के भीतर एक समूह बॉक्स) के अंदर नियंत्रण को स्पष्ट रूप से स्पष्ट करने के लिए, मामलों को और भी खराब बनाने के लिए, मुझे एक ओवरलोडेड "ग्रुपबॉक्स" संस्करण के साथ पूरे राक्षस को दोहराना होगा।

संपादित करें: आपके सुझावों के लिए धन्यवाद, नीचे दिया गया कोड बहुत सरल है।

Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click 
    'User clicks Clear, so clear all the controls within this panel 
    ClearAllControls(panMid, True) 'True indicates that yes, i want to recurse through sub-containers 
End Sub 

ClearAllControls(ByRef container As Panel, Optional Recurse As Boolean = True) 
    'Clear all of the controls within the container object 
    'If "Recurse" is true, then also clear controls within any sub-containers 
    Dim ctrl As Control 
    For Each ctrl In container.Controls 
     If (ctrl.GetType() Is GetType(TextBox)) Then 
      Dim txt As TextBox = CType(ctrl, TextBox) 
      txt.Text = "" 
     End If 
     If (ctrl.GetType() Is GetType(CheckBox)) Then 
      Dim chkbx As CheckBox = CType(ctrl, CheckBox) 
      chkbx.Checked = False 
     End If 
     If (ctrl.GetType() Is GetType(ComboBox)) Then 
      Dim cbobx As ComboBox = CType(ctrl, ComboBox) 
      cbobx.SelectedIndex = -1 
     End If 
     If (ctrl.GetType() Is GetType(DateTimePicker)) Then 
      Dim dtp As DateTimePicker = CType(ctrl, DateTimePicker) 
      dtp.Value = Now() 
     End If 

     If Recurse Then 
      If (ctrl.GetType() Is GetType(Panel)) Then 
       Dim pnl As Panel = CType(ctrl, Panel) 
       ClearAllControls(pnl, Recurse) 
      End If 
      If ctrl.GetType() Is GetType(GroupBox) Then 
       Dim grbx As GroupBox = CType(ctrl, GroupBox) 
       ClearAllControls(grbx, Recurse) 
      End If 
     End If 
    Next 
End Sub 

@Theraccoonbear: मैं अपने सुझाव पसंद है, लेकिन जब मैं इस के लिए घोषणा बदलने के लिए:

Private Sub ClearAllControls(ByRef controls As ControlCollection, Optional ByVal Recurse As Boolean = True) 

तो इस लाइन मुझे देता है "प्रकार की वस्तु कास्ट करने के लिए 'ControlCollection' टाइप करने के लिए 'में असमर्थ ControlCollection '।:

ClearAllControls(panMid.Controls) 

उत्तर

15

आप TryCast साथ GetType और CTYPE नृत्य को छोड़ सकते हैं:

Dim dtp as DateTimePicker = TryCast(ctrl, DateTimePicker) 
If dtp IsNot Nothing then dtp.Value = Now() 

है कि आप के बारे में 10 लाइनों बचा सकते हैं।

एक extension method नियंत्रण वर्ग से दूर यह बहुत साफ रखना चाहिए:

<Extension()> _ 
Public Shared Sub ClearValue(c as Control, recursive as Boolean) 
    Dim dtp as DateTimePicker = TryCast(c, DateTimePicker) 
    If dtp IsNot Nothing Then dtp.Value = Now() 
    ' Blah, Blah, Blah 
End Sub 

संपादित करें: आप बुराई विस्तार तरीकों कि NullReferenceExceptions उपेक्षा के बारे में सोचा नहीं करते हैं तो चापलूसी:

<Extension()> _ 
Public Shared Sub ClearValue(c as CheckBox) 
    If c IsNot Nothing Then c.Checked = False 
End Sub 

TryCast(ctrl, CheckBox).ClearValue() 
1

मैंने कुछ ऐसा किया है और यह मूल रूप से मैं इसे करने के बारे में कैसे चला गया। एकमात्र परिवर्तन जो मैं सुझा सकता हूं वह विधि को अधिभारित करने के बजाय होगा, बस पास में एक नियंत्रण टाइप करें और आप ग्रुपबॉक्स, पैनल या किसी अन्य कंटेनर नियंत्रण के लिए उसी संस्करण का उपयोग कर सकते हैं जो कॉन्ट्रोल गुण प्रदान करता है। इसके अलावा, मुझे लगता है कि नियंत्रण को "समाशोधन" की परिभाषा कुछ हद तक संदिग्ध हो सकती है और इस प्रकार नियंत्रण वर्ग से संबंधित कोई स्पष्ट() विधि नहीं है, इसलिए आपको प्रत्येक नियंत्रण प्रकार के लिए अपने उद्देश्यों के लिए इसका अर्थ लागू करने की आवश्यकता है।

2

क्यों न केवल एक दिनचर्या

ClearAllControls(ByRef container As Control, Optional ByVal Recurse As Boolean = True) 

आप मेरी देखभाल कर सकते हैं इस पर ध्यान दिए बिना पदानुक्रम में आप किस स्तर पर कॉल शुरू करते हैं, फॉर्म स्तर से नीचे एक कंटेनर तक।

इसके अलावा, टेक्स्ट बॉक्स नियंत्रण पर, मैं का उपयोग Textbox.Text = String.Empty

1
For Each c In CONTAINER.Controls 
    If TypeOf c Is TextBox Then 
     c.Text = "" 
    End If 
Next 

तुम्हारा के नाम से (कंटेनर) की जगह
वेतन ध्यान जो करने के लिए (यह एक रूप है, एक पैनल, एक GROUPBOX हो सकता है) आपने अपना नियंत्रण शामिल किया था।

1

यहां यह सभी आंतरिक नियंत्रणों के लिए काम करता है।
जोड़ें कि क्या कोई अन्य नियंत्रण आपको साफ़ करने की आवश्यकता है।

Private Sub ClearAll() 
    Try 
     For Each ctrl As Control In Me.Controls 
      If ctrl.[GetType]().Name = "Panel" Then 
       ClearControls(ctrl) 
      End If 

      If ctrl.[GetType]().Name = "GroupBox" Then 
       ClearControls(ctrl) 
      End If 
      If ctrl.[GetType]().Name = "ComboBox" Then 
       Dim tb As ComboBox = TryCast(ctrl, ComboBox) 
       tb.SelectedText = "" 
      End If 


      If ctrl.[GetType]().Name = "TabControl" Then 
       ClearControls(ctrl) 
      End If 

      If ctrl.[GetType]().Name = "TextBox" Then 
       Dim tb As TextBox = TryCast(ctrl, TextBox) 
       tb.Clear() 
      End If 

      If ctrl.[GetType]().Name = "RadioButton" Then 
       Dim tb As RadioButton = TryCast(ctrl, RadioButton) 
       tb.Checked = False 
      End If 

      If ctrl.[GetType]().Name = "CheckBox" Then 
       Dim tb As CheckBox = TryCast(ctrl, CheckBox) 
       tb.Checked = False 
      End If 

      If ctrl.[GetType]().Name = "ComboBox" Then 
       Dim tb As ComboBox = TryCast(ctrl, ComboBox) 
       tb.SelectedIndex = 0 
      End If 

      If ctrl.[GetType]().Name = "RichTextBox" Then 
       Dim tb As RichTextBox = TryCast(ctrl, RichTextBox) 
       tb.Clear() 

      End If 
     Next 
    Catch ex As Exception 
     MessageBox.Show(ex.Message, "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error) 
    End Try 
End Sub 


Private Sub ClearControls(ByVal Type As Control) 

    Try 
     For Each ctrl As Control In Type.Controls 

      If ctrl.[GetType]().Name = "TextBox" Then 
       Dim tb As TextBox = TryCast(ctrl, TextBox) 
       tb.Clear() 
      End If 

      If ctrl.[GetType]().Name = "Panel" Then 
       ClearControls(ctrl) 
      End If 

      If ctrl.[GetType]().Name = "GroupBox" Then 
       ClearControls(ctrl) 
      End If 

      If ctrl.[GetType]().Name = "TabPage" Then 
       ClearControls(ctrl) 
      End If 

      If ctrl.[GetType]().Name = "ComboBox" Then 
       Dim tb As ComboBox = TryCast(ctrl, ComboBox) 
       tb.SelectedText = "" 
      End If 

      If ctrl.[GetType]().Name = "RadioButton" Then 
       Dim tb As RadioButton = TryCast(ctrl, RadioButton) 
       tb.Checked = False 
      End If 

      If ctrl.[GetType]().Name = "CheckBox" Then 
       Dim tb As CheckBox = TryCast(ctrl, CheckBox) 
       tb.Checked = False 
      End If 

      If ctrl.[GetType]().Name = "RichTextBox" Then 
       Dim tb As RichTextBox = TryCast(ctrl, RichTextBox) 
       tb.Clear() 

      End If 
     Next 
    Catch ex As Exception 
     MessageBox.Show(ex.Message, "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error) 
    End Try 
End Sub 
7

यहाँ एक प्रपत्र के सभी GroupControls के सभी नियंत्रण प्राप्त करने के लिए कोड है और आप GroupBox नियंत्रण में कुछ कर सकते हैं

Private Sub GetControls() 
    For Each GroupBoxCntrol As Control In Me.Controls 
     If TypeOf GroupBoxCntrol Is GroupBox Then 
      For Each cntrl As Control In GroupBoxCntrol.Controls 
       'do somethin here 

      Next 
     End If 

    Next 
End Sub 
0

इस तकनीक पर चर्चा अब है कि नियंत्रण का उपयोग करने के article से सीधे आता है VB6 से VB.NET तक जाने के साथ Arrays को समाप्त कर दिया गया है।

Private Sub ClearForm(ByVal ctrlParent As Control) 
    Dim ctrl As Control 
    For Each ctrl In ctrlParent.Controls 
     If TypeOf ctrl Is TextBox Then 
      ctrl.Text = "" 
     End If 
     ' If the control has children, 
     ' recursively call this function 
     If ctrl.HasChildren Then 
      ClearForm(ctrl) 
     End If 
    Next 
End Sub 
0

मैंने तुम्हें अपनी ControlIterator क्लास पेश

स्रोत: http://pastebin.com/dubt4nPG

कुछ उपयोग के उदाहरण:

ControlIterator.Disable(CheckBox1) 

ControlIterator.Enable({CheckBox1, CheckBox2}) 

ControlIterator.Check(Of CheckBox)(Me) 

ControlIterator.Uncheck(Of CheckBox)(Me.GroupBox1) 

ControlIterator.Hide(Of CheckBox)("1") 

ControlIterator.PerformAction(Of CheckBox)(Sub(ctrl As CheckBox) ctrl.Visible = True) 

ControlIterator.AsyncPerformAction(RichTextBox1, 
            Sub(rb As RichTextBox) 
             For n As Integer = 0 To 9 
              rb.AppendText(CStr(n)) 
             Next 
            End Sub) 

ControlIterator.PerformAction(Me.Controls, Sub(c As Control) 
               c.BackColor = Color.Green 
              End Sub) 
0

सार्वजनिक उप सूबेदार राज (lst Control.ControlCollection के रूप में, वैकल्पिक पुनरावर्ती बूलियन के रूप में = सच)

 For Each ctrl As Control In lst 

      If TypeOf ctrl Is TextBox Then 
       CType(ctrl, TextBox).Clear() 
      End If 


      If TypeOf ctrl Is MaskedTextBox Then 
       CType(ctrl, MaskedTextBox).Clear() 
      End If 

      If TypeOf ctrl Is ComboBox Then 
       CType(ctrl, ComboBox).SelectedIndex = -1 
      End If 

      If TypeOf ctrl Is DateTimePicker Then 
       Dim dtp As DateTimePicker = CType(ctrl, DateTimePicker) 
       dtp.CustomFormat = " " 
      End If 

      If TypeOf ctrl Is CheckedListBox Then 
       Dim clbox As CheckedListBox = CType(ctrl, CheckedListBox) 
       For i As Integer = 0 To clbox.Items.Count - 1 
        clbox.SetItemChecked(i, False) 
       Next 
      End If 

      If TypeOf ctrl Is RadioButton Then 
       CType(ctrl, RadioButton).Checked = False 

      End If 

      If recursive Then 
       If TypeOf ctrl Is GroupBox Then 
        raz(CType(ctrl, GroupBox).Controls) 
       End If 
      End If 






     Next 
    End Sub 
+0

मेहोद के लिए कॉलिंग: raz (Me.Controls) – user3692282

+0

कोड केवल समाधान पोस्ट न करने का प्रयास करें। – NathanOliver

+0

ठीक है, लेकिन, मुझे लगता है कि यह उपर्युक्त प्रश्न का समाधान है, है ना? – user3692282

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