Tema: Re: formos elementų listas
Autorius: Jornada Del Muerto
Data: 2010-07-19 12:30:07
Jei labai reikia pereit visus controlus stai rekursyvi OOP metodika:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
// Deklaruojam interfeisa kuris dirba su control
public interface IControlAccessor
{
void Do (Control c);
}
// Nusiveldime interfeisa ir sukuriame pvz Visible busenos verimo i atbula funkcionaluma
public class VisibleInverter : IControlAccessor
{
#region IControlAccessor Members
public void Do (Control c)
{
c.Visible = !c.Visible;
}
#endregion
}
// Sukuriame rekursivine funkcija einancia per absoliuciai visus formos controlus ir sub sub controlus
// Tiesiog jei forma tures panele tai paneleje esantys controlai bus paneles.Controls propertyje
// Controls property priklauso Control tipui
public class FormUtils
{
public static void AccessControls (
Control control,
IControlAccessor accessor)
{
foreach (Control c in control.Controls)
{
accessor.Do (c);
AccessControls (c, accessor);
}
}
}
// Iskvieciame visu formos controlu apdirbima
public partial class Form1 : Form
{
public Form1 ()
{
InitializeComponent ();
FormUtils.AccessControls (this, new VisibleInverter ());
}
}
Tuo paciu jei reikia apdirbti kazkoki konkretu controla kazkaip ipatingai, galima tai daryti su "is" pvz
public class OtherAccessor : IControlAccessor
{
#region IControlAccessor Members
public void Do (Control c)
{
if(c is TextBox) {
// Assigninimas castinant
TextBox t = (TextBox)c;
TextBox.Clear();
}
else if (c is ComboBox){
// Accessinimas be assigninimo
((ComboBox)c).SelectedIndex = 0;
}
// ir t.t...
}
#endregion
}
P.S. Parasyta viskas per 5 min ;)