PDA

View Full Version : سوال: چک کردن وضعیت یک Item در Checked List Box



parsa lotfy
پنج شنبه 05 فروردین 1395, 10:15 صبح
با سلام...

دوستان میخواستم وضعیت یک Item از یک Checked List Box رو چک کنم که ایا اون Item تیک خورده یا ن ؟

چنین کدی رو نوشتم ولی کار نمیکنه ...:اشتباه:

میشه توضیح بدین که چرا کار نمیکنه ؟ و باید چیکار کنم ک کار کنه ؟؟؟

private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if(checkedListBox1.SelectedItem == CheckState.Checked)
{
// Some Code...
}
}

البته روشی میدانم ک با استفاده از e.NewValue هستش ولی من مفهومش رو نمیفهمم و میخوام با کدی مثل کد خودم این برنامه رو درست کنم...

مثلا با selected index یا selected item یا selected items یا .....

با تشکر:قلب::قلب:

parsa lotfy
پنج شنبه 05 فروردین 1395, 11:22 صبح
اگه زحمت نیست میشه این کد رو هم برام توضیح بدین ؟

if ( e.NewValue == CheckState.Checked )

ممنون میشم:قلب::قلب:

vb8334
پنج شنبه 05 فروردین 1395, 12:02 عصر
سلام دوست عزیز

دلیل اینکه کد شما کار نمیکنه این هستش که شما داری مقایسه اشتباه انجام میدی اگر دقت کنی میبینی که selectedItem یک object برمیگردونه ولی checkstate یک enum

برمیگردونه و این خطا رو به شما میده.


operator '==' cannot be applied to operands of type 'object' and 'checkState'




حالا چیزی که گفتید رو بخوام توضیح بدم این کد داره از رویداد ItemCheck استفاده میکنه و این رویداد یک سری properties داره به نام newValue , currentVlue و...



private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{

}




در واقع رویداد ItemCheck داره از ItemCheckEventArgs کمک میگیره و اون properties توی این کلاس (ItemCheckEventArgs) هستن.

newValue : در واقع این کد خودش از نوع checkState هستش بخاطر همین هست که این کد اجرا میشه، این event ی هست که اگه آیتم کلیک بشه یا نشه 3 تا حالت داره

1-checked

2-unchecked

3-indeterminate (وضعیت نامشخص)

currentValue : وضعیت کنونی آیتم ها رو مشخص میکنه که چه حالتی دارند و فقط حالت رو برمیگردونه (get) و چیزی نمیتونی بهش set کنی

vb8334
پنج شنبه 05 فروردین 1395, 12:06 عصر
و اینم اضافه کنم اگه داخل ItemCheckEventArgs,checkstate نگاه کنیم فکر کنم فهمش ساده تر باشه برای اینکه اطلاعاتت کامل تر بشه

توصیه میکنم به msdn مایکروسافت مراجعه کنی

https://msdn.microsoft.com/en-us/library/system.windows.forms.itemcheckeventargs(v=vs.110). aspx

https://msdn.microsoft.com/en-us/library/system.windows.forms.checkedlistbox(v=vs.110).aspx






//
// Summary:
// Specifies the state of a control, such as a check box, that can be checked, unchecked,
// or set to an indeterminate state.
public enum CheckState
{
//
// Summary:
// The control is unchecked.
Unchecked = 0,
//
// Summary:
// The control is checked.
Checked = 1,
//
// Summary:
// The control is indeterminate. An indeterminate control generally has a shaded
// appearance.
Indeterminate = 2
}


کد ItemCheckEventArgs




//
// Summary:
// Provides data for the System.Windows.Forms.CheckedListBox.ItemCheck event of
// the System.Windows.Forms.CheckedListBox and System.Windows.Forms.ListView controls.
[ComVisible(true)]
public class ItemCheckEventArgs : EventArgs
{
//
// Summary:
// Initializes a new instance of the System.Windows.Forms.ItemCheckEventArgs class.
//
// Parameters:
// index:
// The zero-based index of the item to change.
//
// newCheckValue:
// One of the System.Windows.Forms.CheckState values that indicates whether to change
// the check box for the item to be checked, unchecked, or indeterminate.
//
// currentValue:
// One of the System.Windows.Forms.CheckState values that indicates whether the
// check box for the item is currently checked, unchecked, or indeterminate.
public ItemCheckEventArgs(int index, CheckState newCheckValue, CheckState currentValue);


//
// Summary:
// Gets a value indicating the current state of the item's check box.
//
// Returns:
// One of the System.Windows.Forms.CheckState values.
public CheckState CurrentValue { get; }
//
// Summary:
// Gets the zero-based index of the item to change.
//
// Returns:
// The zero-based index of the item to change.
public int Index { get; }
//
// Summary:
// Gets or sets a value indicating whether to set the check box for the item to
// be checked, unchecked, or indeterminate.
//
// Returns:
// One of the System.Windows.Forms.CheckState values.
public CheckState NewValue { get; set; }
}