إضافة عناصر تحكم خلية في أوراق العمل
مقدمة
حاليًا، يدعم Aspose.Cells.GridDesktop إضافة ثلاثة أنواع من عناصر التحكم في الخلايا، وتشمل ما يلي:
- زر
- صندوق اختيار
- قائمة تحديد
كل هذه العناصر تستند إلى فئة مجردة، CellControl. تحتوي كل ورقة عمل على مجموعة من التحكم. يمكن بسهولة إضافة عناصر تحكم جديدة في الخلايا والوصول إلى تلك الموجودة باستخدام هذه المجموعة التحكم.
مهم جدًا: إذا كنت ترغب في إضافة عناصر تحكم في جميع الخلايا لعمود بدلاً من إضافتها واحدة تلو الأخرى، فيمكنك الرجوع إلى إدارة العناصر التحكم في الأعمدة.
إضافة زر
لإضافة زر إلى ورقة العمل باستخدام Aspose.Cells.GridDesktop، يرجى اتباع الخطوات التالية:
- أضف تحكم Aspose.Cells.GridDesktop إلى النموذج الخاص بك
- الوصول إلى أي ورقة عمل مرغوبة
- إضافة زر إلى مجموعة الضوابط في ورقة العمل
أثناء إضافة زر، يمكننا تحديد موقع الخلية (حيث يتم عرضها) والعرض والارتفاع وتسمية الزر.
معالجة حدث الزر
لقد ناقشنا إضافة زر إلى ورقة العمل ولكن ما هي الميزة من وجود زر في ورقة العمل إذا لم نتمكن من استخدامه. لذا، هنا يأتي دور معالجة حدث الزر.
للتعامل مع حدث النقر على زر التحكم، يوفر Aspose.Cells.GridDesktop حدث CellButtonClick الذي يجب تنفيذه من قبل المطورين وفقا لاحتياجاتهم. على سبيل المثال، قمنا بعرض رسالة فقط عند النقر على الزر كما هو مبين أدناه:
تحديد صورة خلفية لتحكم الزر
يمكننا تعيين صورة خلفية/صورة لتحكم الزر مع تسميته/نصه كما هو مبين في الكود أدناه:
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = Utils.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Set the image. | |
Image image = Image.FromFile(dataDir + @"AsposeLogo.jpg"); | |
button.Image = image; |
مهم: جميع أحداث تحكمات الخلية تحتوي على وسيطة CellControlEventArgs التي توفر أرقام الصف والعمود للخلية التي تحتوي على تحكم الخلية (الذي تم تشغيل حدثه).
إضافة مربع اختيار
لإضافة مربع اختيار في ورقة العمل باستخدام Aspose.Cells.GridDesktop، يرجى اتباع الخطوات أدناه:
- أضف تحكم Aspose.Cells.GridDesktop إلى النموذج الخاص بك
- الوصول إلى أي ورقة عمل مرغوبة
- إضافة مربع اختيار إلى مجموعة الضوابط في ورقة العمل
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Accessing the worksheet of the Grid that is currently active | |
Worksheet sheet = gridDesktop1.GetActiveWorksheet(); | |
// Accessing the location of the cell that is currently in focus | |
CellLocation cl = sheet.GetFocusedCellLocation(); | |
// Adding checkbox to the Controls collection of the Worksheet | |
sheet.Controls.AddCheckBox(cl.Row, cl.Column, true); |
أثناء إضافة مربع اختيار، يمكننا تحديد موقع الخلية (حيث يتم عرضها) وحالة مربع الاختيار.
معالجة حدث مربع الاختيار
يوفر Aspose.Cells.GridDesktop حدث CellCheckedChanged الذي يتم تشغيله عند تغيير حالة المربع اختيار. يمكن للمطورين التعامل مع هذا الحدث وفقا لاحتياجاتهم. على سبيل المثال، قمنا بعرض رسالة لإظهار حالة المربع اختيار في الكود أدناه:
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Implenting CellCheckedChanged event handler | |
private void gridDesktop1_CellCheckedChanged(object sender, CellControlEventArgs e) | |
{ | |
// Getting the reference of the CheckBox control whose event is triggered | |
Aspose.Cells.GridDesktop.CheckBox check = (Aspose.Cells.GridDesktop.CheckBox)gridDesktop1.GetActiveWorksheet().Controls[e.Row, e.Column]; | |
// Displaying the message when the Checked state of CheckBox is changed | |
MessageBox.Show("Current state of CheckBox is " + check.Checked); | |
} |
إضافة مربع القائمة المنسدلة
لإضافة مربع القائمة المنسدلة في ورقة العمل باستخدام Aspose.Cells.GridDesktop يرجى اتباع الخطوات أدناه:
- أضف تحكم Aspose.Cells.GridDesktop إلى النموذج الخاص بك
- الوصول إلى أي ورقة عمل مرغوبة
- إنشاء مصفوفة من العناصر (أو القيم) التي سيتم إضافتها إلى ComboBox
- إضافة ComboBox إلى مجموعة Controls لورقة العمل بتحديد موقع الخلية (حيث سيتم عرض combobox) والعناصر/القيم التي سيتم عرضها عند النقر على combobox
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Accessing the worksheet of the Grid that is currently active | |
Worksheet sheet = gridDesktop1.GetActiveWorksheet(); | |
// Accessing the location of the cell that is currently in focus | |
CellLocation cl = sheet.GetFocusedCellLocation(); | |
// Creating an array of items or values that will be added to combobox | |
string[] items = new string[3]; | |
items[0] = "Aspose"; | |
items[1] = "Aspose.Grid"; | |
items[2] = "Aspose.Grid.Desktop"; | |
// Adding combobox to the Controls collection of the Worksheet | |
sheet.Controls.AddComboBox(cl.Row, cl.Column, items); |
معالجة الحدث في ComboBox
توفر Aspose.Cells.GridDesktop حدث CellSelectedIndexChanged الذي يتم تشغيله عند تغيير Selected Index للـ combobox. يمكن للمطورين التحكم في هذا الحدث وفقًا لرغباتهم. على سبيل المثال، لقد عرضنا رسالة لإظهار Selected Item للـ combobox:
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Implenting CellSelectedIndexChanged event handler | |
private void gridDesktop1_CellSelectedIndexChanged(object sender, CellComboBoxEventArgs e) | |
{ | |
// Getting the reference of the ComboBox control whose event is triggered | |
Aspose.Cells.GridDesktop.ComboBox combo = | |
(Aspose.Cells.GridDesktop.ComboBox)gridDesktop1.GetActiveWorksheet().Controls[e.Row, e.Column]; | |
// Displaying the message when the Selected Index of ComboBox is changed | |
MessageBox.Show(combo.Items[combo.SelectedIndex].ToString()); | |
} |