سرمایه‌گذاری روی خودت، پرسودترین سرمایه‌گذاری دنیاست! سرمایه‌گذاری روی خودت، پرسودترین سرمایه‌گذاری دنیاست!
پایان تخفیف تا:
مشاهده دوره‌ها
0

پاک کردن کمبو باکس در c#

من برای پاک کردن تموم کمبو باکس ها کد زیر رو نوشتم ولی اجرا نمیشه:

               foreach (Control c in grb1stNewSymp.Controls)
               {

                   if (c is System.Windows.Forms.ComboBox)
                   {
                       c.SelectedIndex = 0;
                   }
               }

ارورشم اینه:

Error	2	'System.Windows.Forms.Control' does not contain a definition for 'SelectedIndex' and no extension method 'SelectedIndex' accepting a first argument of type 'System.Windows.Forms.Control' could be found (are you missing a using directive or an assembly reference?)	C:\Users\Hooman\Documents\Visual Studio 2012\Projects\DXApplication2\Sepanta4\Form1.cs	737	26	Sepanta4

بصورت زیر هم تغییرش دادم:

               foreach (System.Windows.Forms.ComboBox c in grb1stNewSymp.Controls)
               {

                       c.SelectedIndex = 0;

               }

ولی موقع اجرا ارور زیر رو می گیره:

Unable to cast object of type 'System.Windows.Forms.Label' to type 'System.Windows.Forms.ComboBox'.

ممنون بابت کمکهای سریعتون

پرسیده شده در 1393/10/03 توسط

1 پاسخ

7

کدی که نوشتید رو به صورت زیر تغییر بدید:

foreach (var c in grb1stNewSymp.Controls.OfType<System.Windows.Forms.ComboBox>())
{
        c.SelectedIndex = -1;
}

متد OfType یک متد جنریک هست که از یک لیست مثل Controls بر اساس تایپ ورودی عملیات فیلتر رو انجام میده و لیست جدیدی به شما بر میگردونه. در مورد SelectedIndex هم باید مقدار برابر 1- باشه، چون 0 به اولین آیتم اشاره می کنه.

پاسخ در 1393/10/03 توسط

پاسخ شما