الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

For Each LOOP

مغلق
بدأه بن ملعاط في 13 مارس 2005 · 3 رد · 522 مشاهدة · في Microsoft Visual Basic.NET
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

إخوتي الكرام

في الكود المرفق أحاول أن أمسح محتويات كل ال TextBox دفعة واحد ولكن لم تنجح الفكرة ،،،، ممكن :D :D :D

 

Dim MyTxt As System.Windows.Forms.TextBox

For Each MyTxt In Me.Controls
       MyTxt.Text = ""
Next MyTxt

ولكن تظهر هذه الرسالة

An unhandled exception of type 'System.InvalidCastException' occurred in StockSymbol.exe

Additional information: Specified cast is not valid.

اللهم اغفر لي و لوالدي و للمؤمنين يوم يقوم الحساب

لا إله إلا الله عدد ما كان و عدد ما يكون و عدد الحركات و السكون

#2

السلام عليكم

عرف متغير من نوع control ثم في الحلقة ضعه في المكان المقصود مثل form أو panel أو group أووووو

وبعدين شوف هل هذا الـ control من نوع textBox إذا كان ... فضيه بهذا الشكل

Dim ctrl As Control
        For Each ctrl In Me.Controls
            Try
                If TypeOf ctrl Is TextBox Then
                    ctrl.Text = ""
                End If
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        Next

موفق إن شاء الله

في المثال يتضح المقال

#3

هنالك طريقة استخدمها دائما لـعمل Reset للفورم أو الكنترول مرة واحدة واليك الكود

أولا قم باضافة Module جديد لمشروعك وقم بلصق الشفرة التالية :

Public Sub ResetForm(ByVal FormToReset As Form) 
    ' Resets the main data entry controls on the passed FormToReset 
    Dim objControl As Control 
    ' Loop around every control on the form and run the reset method 
    For Each objControl In FormToReset.Controls 
        ResetControl(objControl) 
    Next 
End Sub 

Public Sub ResetControl(ByVal ControlToReset As Control) 
    ' Resets the core control, then loops and 
    ' resets any sub controls, such as Tab pages 
    Dim intCount As Integer 
    ClearControl(ControlToReset) 
    If ControlToReset.Controls.Count > 0 Then 
        For intCount = 1 To ControlToReset.Controls.Count 
            ResetControl(ControlToReset.Controls(intCount - 1)) 
        Next 
    End If 
End Sub 

Public Sub ClearControl(ByVal ControlToClear As Control) 
    ' Clears the value of a particular control -
    ' you may wish to extend this to suit your exact needs 
    If InStr(ControlToClear.Tag, "skip", CompareMethod.Text) = 0 Then 
        If TypeOf (ControlToClear) Is System.Windows.Forms.TextBox Then 
            ControlToClear.Text = ""  ' Clear TextBox 
        ElseIf TypeOf (ControlToClear) Is System.Windows.Forms.CheckBox Then 
            Dim objCheckBox As System.Windows.Forms.CheckBox = ControlToClear 
            objCheckBox.Checked = False ' Uncheck CheckBox 
        ElseIf TypeOf (ControlToClear) Is System.Windows.Forms.ComboBox Then 
            Dim objComboBox As System.Windows.Forms.ComboBox = ControlToClear 
            objComboBox.SelectedIndex = -1 ' Deselect any ComboBox entry 
        End If 
    End If 
End Sub

ولاستخدام هذه الاجراءات :

إذا كنت تريد ان تعمل Reset لجميع محتويات الفورم :

ResetForm(Me)

أما إذا كنت تريد تريد عمل Reset لمحتويات Control معين مثل ال GroupBox أو ال Pannel أو غيره :

Reset(ControlName)

والله الموفق

#4

جزاكم الله كل خير وبارك فيكم

اللهم اغفر لي و لوالدي و للمؤمنين يوم يقوم الحساب

لا إله إلا الله عدد ما كان و عدد ما يكون و عدد الحركات و السكون

هذا الموضوع مغلق.

مواضيع مشابهة