SORU
5 AĞUSTOS 2010, PERŞEMBE


Nasıl belirli bir türü (Düğme/Textbox) Windows Form bir formun TÜM alt denetimler için?

Türde bir form üzerinde x kontrolleri almak istiyorum. Bu kodu bir kere böyle bir şey eskiden geçmişte gördüm eminim:

dim ctrls() as Control
ctrls = Me.Controls(GetType(TextBox))

Tüm kontrolleri çocuklar bir özyinelemeli fonksiyonu kullanılarak elde, ama ispat edebilirim bir şey daha kolay ya da daha basit, belki de aşağıdaki gibi?

Dim Ctrls = From ctrl In Me.Controls Where ctrl.GetType Is Textbox

CEVAP
6 AĞUSTOS 2010, Cuma


İşte size başka bir seçenek. Örnek bir uygulama oluşturarak test ettim, daha sonra bir grup kutusu koy ve ilk grup kutusu içinde bir grup kutusu. İç içe grup kutusu içinde 3 TextBox ve bir buton koydum. Bu kullandığım kod (hatta aradığın özyineleme içerir)

public IEnumerable<Control> GetAll(Control control,Type type)
{
    var controls = control.Controls.Cast<Control>();

    return controls.SelectMany(ctrl => GetAll(ctrl,type))
                              .Concat(controls)
                              .Where(c => c.GetType() == type);
}

Form load olayı test etmek için ilk grup kutusu içinde tüm kontrolleri sayısını istedim

private void Form1_Load(object sender, EventArgs e)
{
    var c = GetAll(this,typeof(TextBox));
    MessageBox.Show("Total Controls: "   c.Count());
}

Ve bu sizin için ne arıyorsanız mükemmel iş:) bence çok doğru saymak, her zaman döndü

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • MndsgnVEVO

    MndsgnVEVO

    26 Kasım 2013
  • Modus Recordings

    Modus Record

    26 Kasım 2008
  • Ownage Pranks

    Ownage Prank

    13 AĞUSTOS 2007