PDA

View Full Version : پیدا کردن کنترل های یک فرم



visual_sadegh
دوشنبه 08 بهمن 1386, 08:56 صبح
سلام دوستان
فرض کنید در یک فرم تعدادی کنترل مستقیما بر روی فرم و تعدادی کنترل بر روی گروپ باکس قرار دارد ظاهرا خصوصیت controls فرم فقط لیست کنترل های خودش رو نشون می ده چطور می تونیم لیست تمام کنترل های یک فرم رو بدست بیاریم.

mpg_of_shb
دوشنبه 08 بهمن 1386, 13:15 عصر
Public Sub Get_Value_From_Sql_Data_Reader(ByVal Name_Form As Form, ByVal dr As SqlDataReader)
Dim C, C2, C3 As Control
dr.Read()
For Each C In Name_Form.Controls
If TypeOf C Is TextBox Or TypeOf C Is MaskedTextBox Or TypeOf C Is ComboBox Then
If C.Tag <> "" Then
C.Text = CStr(IIf(IsDBNull(dr.Item(C.Tag)) = True, "", dr.Item(C.Tag))) + ""
End If
End If
If TypeOf C Is Panel Or TypeOf C Is GroupBox Or TypeOf C Is TabControl Or TypeOf C Is TabPage Then
For Each C2 In C.Controls
If TypeOf C2 Is TextBox Or TypeOf C2 Is MaskedTextBox Or TypeOf C2 Is ComboBox Then
If C2.Tag <> "" And C2.Visible = True Then
C2.Text = CStr(IIf(IsDBNull(dr.Item(C2.Tag)) = True, "", dr.Item(C2.Tag))) + ""
End If
End If
If TypeOf C2 Is TabPage Then
For Each C3 In C2.Controls
If TypeOf C3 Is TextBox Or TypeOf C3 Is MaskedTextBox Or TypeOf C3 Is ComboBox Then
If C3.Tag <> "" Then
C3.Text = CStr(IIf(IsDBNull(dr.Item(C3.Tag)) = True, "", dr.Item(C3.Tag))) + ""
End If
End If
Next
End If
Next
End If
Next
End Sub

mpg_of_shb
دوشنبه 08 بهمن 1386, 13:16 عصر
Public Sub Reset_Control(ByVal Ctrl As Control)
Dim i As Integer
Clear_Control(Ctrl)
If Ctrl.Controls.Count > 0 Then
For i = 1 To Ctrl.Controls.Count
Reset_Control(Ctrl.Controls(i - 1))
Next
End If
End Sub

mpg_of_shb
دوشنبه 08 بهمن 1386, 13:18 عصر
reset_control(me)
Public Sub Clear_Control(ByVal Ctrl_To_Clear As Control)
If InStr(Ctrl_To_Clear.Tag, "skip", CompareMethod.Text) = 0 Then
If TypeOf Ctrl_To_Clear Is TextBox Then
Ctrl_To_Clear.Text = ""
ElseIf TypeOf Ctrl_To_Clear Is CheckBox Then
Dim Chek_Box As CheckBox = Ctrl_To_Clear
Chek_Box.Checked = False
ElseIf TypeOf Ctrl_To_Clear Is ComboBox Then
Dim Comb_Box As ComboBox = Ctrl_To_Clear
Comb_Box.SelectedIndex = -1
ElseIf TypeOf Ctrl_To_Clear Is MaskedTextBox Then
Dim Mask_Text As MaskedTextBox = Ctrl_To_Clear
Mask_Text.Text = "__/__/__"
End If
End If
End Sub

Dariuosh
دوشنبه 08 بهمن 1386, 13:39 عصر
خواهشن کدها رو تو تگ کد بزارین

visual_sadegh
دوشنبه 08 بهمن 1386, 13:43 عصر
دوست عزیز ممنون از راهنماییت اگه امکان داره کد ها رو توی تگ کد بگذار اینطوری درکش برام راحت تره
اگه کمی در مورد کد ها توضیح بدی ممنون می شم.

sinpin
دوشنبه 08 بهمن 1386, 14:10 عصر
سلام دوستان
فرض کنید در یک فرم تعدادی کنترل مستقیما بر روی فرم و تعدادی کنترل بر روی گروپ باکس قرار دارد ظاهرا خصوصیت controls فرم فقط لیست کنترل های خودش رو نشون می ده چطور می تونیم لیست تمام کنترل های یک فرم رو بدست بیاریم.
با استفاده از روش عقب-گرد و یا یک الگوریتم بازگشتی مثل :



// this code assumes the following using statements
// using System.Collections;
// using System.Windows.Forms;
// return all the controls in a control container
// (including controls inside other controls)
public static Control[] GetChildControls(Control ctrl, Type ctrlType)
{
ArrayList controls = new ArrayList();
foreach (Control c in ctrl.Controls)
{
if (ctrlType == null || ctrlType.IsAssignableFrom(c.GetType()))
{
// add this control and all its nested controls
controls.Add(c);
controls.AddRange(GetChildControls(c, null));
}
}
// return the result as a Control array
return (Control[]) controls.ToArray(typeof(Control));
}البته کد زیر کاراتر هست و از لیستهای ژنریک استفاده کرده :

public delegate bool delTypeCheck(Type type);
/// <summary>
/// Examines the full content of this control's Controls collection and returns the enumerator instead of a generic list.
/// </summary>
/// <typeparam name="T">The Type we are looking for - we will also treat any derived types as a match!</typeparam>
/// <param name="ctrMain">The control to examine.</param>
/// <param name="IsExemptCheck">A delegate allowing us to specify which controls to exempt from examining.</param>
/// <returns>The actual enumerator allowing us to NOT have to create a helper intermediate list.</returns>
public static IEnumerable<T> GetControlsOfType<T>(Control ctrMain, delTypeCheck IsExemptCheck) where T : class
{
// Determine the Type we need to look out for
Type searchType = typeof(T);

foreach (Control c in ctrMain.Controls)
{
// If user wants to exclude certain types then a call-back has been given
// So call it with the current control-type and check if it is exempt.
if (IsExemptCheck != null && IsExemptCheck(c.GetType()))
continue; // the type of c is exempt so continue.

// If a match is found then yield this item back directly
if (c is T) yield return (c as T);

// if you want to search for specific Types only (and NOT derived types) then
// uncomment the following lines (and comment out the above statement).
//if (c.GetType().Equals(searchType))
// yield return (c as T);

// If the control hosts other controls then recursively call this function again.
if (c.Controls.Count > 0)
foreach (T t in GetControlsOfType<T>(c, IsExemptCheck))
yield return t;
}
}