Jump to content

Form Elemanlarını Döngü Içerisinde çekme


korasoglu
 Share

Recommended Posts

            // formdaki kontrolleri döngüye sok
            foreach (Control item in this.Controls)
            {
                if (item.GetType() == typeof(DataGridView)) // eğer kontrol tipi datagridview ise
                {
                    MessageBox.Show(item.Name);
                }
            }

yalnız bu kodun çalışması için gridlerin direk formda olması gerekiyor. Eğer gridler groupbox, panel, strip gibi nesnelerin içindeyse o zaman kodlar değişir.

  • Like 1
Link to comment
Share on other sites

bu sefer de döngü içinde döngü kullanabilirsiniz. Yani önce form içindeki kontrolleri döngüye sokup içindeki groupbox'i bulmalı, sonra onun içindeki kontrolleri de döngüye sokup içindeki kontrollerden datagridviewleri bulmalısınız.

            foreach (Control item in this.Controls)
            {
                if (item.GetType() == typeof(GroupBox)) // eğer kontrol tipi groupbox ise
                {
                    GroupBox grp = (GroupBox)item; // item i groupbox a çevir
                    foreach (Control kontrol in grp.Controls) // groupbox in içindeki kontrolleri döngüye sok
                    {
                        if (kontrol.GetType() == typeof(DataGridView)) // eğer kontrol tipi dataGridView ise
                        {
                            MessageBox.Show(kontrol.Name); // dataGridView'in ismini ekrana yazdır
                        }
                    }
                }
            }

eğer groupbox ismi belli ise ya da bir tane varsa form kontrollerini döngüye sokmanıza gerek yok. 2. döngüyü kullanmanız yeterli olur.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...