Witam.
Mam taką apkę:
Kod:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace testy2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
foreach (Label tb in this.Controls.OfType<Label>())
{
MessageBox.Show(tb.Name);
}
}
}
}
Chciałbym, aby po kliknięciu przycisku wyświetliła mi się nazwa użytych labeli(czyli w sumie 3).
Niestety teraz dostaję tylko jeden- [b]label3[/b]
Dzieje się tak dlatego, że label2 i label1 są w GroupBoxie1;
Oczywiście mógłbym zrobić taki kod:
private void button1_Click(object sender, EventArgs e)
{
foreach (Label tb in this.Controls.OfType<Label>())
{
MessageBox.Show(tb.Name);
}
/////////////
foreach (Label tb in groupBox1.Controls.OfType<Label>())
{
MessageBox.Show(tb.Name);
}
}
Ale czy nie da się jakoś zrobić tego za jednym zamachem?