在工作表上放置.net控件



我正在使用spreadsheetgage,我想将组合框(ComponentOne)放入1个单元格中。我希望当用户转到这个单元格时,这个组合框将激活并向用户显示列表。用户在列表中选择项目后,会将该项目放入单元格值并隐藏组合框。如何在Spreadsheetgear中执行此操作。

谢谢,

Doit

我不熟悉ComponentOne控件,所以无法真正回答您的问题。但是,关于将自定义控件嵌入SpreadsheetGear WorkbookView UI控件的更为通用的方法,这可以通过对UIManager类进行子类来实现,这将允许您截取工作表上现有形状的创建,并用自己的自定义控件替换它们。

下面是一个简单的例子,它用Windows窗体WorkbookView控件和SpreadsheetGear.Windows.Forms.UIManager的一个子类演示了这一点。这个例子只是用按钮替换矩形AutoShape。您可以将其修改为显示ComponentOne复选框。

请注意,每当形状滚动到视图中/在WorkbookView上可见时,都会调用UIManager.CreateCustomControl(…)方法。还要注意,每次将自定义控件滚动出视图或以其他方式使其不可见时,都会将其处理掉。有关此API的更多详细信息,请参阅文档。

关于形状和工作表的另一个重要点是,形状通常不会嵌入单元格中。相反,它们在细胞上空盘旋。因此,在给定的形状和给定的单元格之间不会有明确的"联系"。最接近建立这种关联的方法是使用IShape.TopLeftCell或BottomRightCell属性,这些属性将提供该形状的各个边所在的范围。IShape接口包含许多其他API,您可能会发现这些API在您的用例中很有用。例如,可以通过将IShape.Visible属性设置为false来隐藏形状。

using System;
using System.Windows.Forms;
using SpreadsheetGear;
using SpreadsheetGear.Shapes;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// Create the UIManager replacement.
new MyUIManager(workbookView1.ActiveWorkbookSet);
}
private void buttonRunSample_Click(object sender, EventArgs e)
{
// NOTE: Must acquire a workbook set lock.
workbookView1.GetLock();
try
{
// Get a reference to the active worksheet and window information.
IWorksheetWindowInfo windowInfo = workbookView1.ActiveWorksheetWindowInfo;
IWorksheet worksheet = workbookView1.ActiveWorksheet;
// Get a reference to a cell.
IRange cell = workbookView1.ActiveWorksheet.Cells["B2"];
// Add a placeholder shape to the worksheet's shape collection.
// This shape will be replaced with a custom control.
double left = windowInfo.ColumnToPoints(cell.Column) + 5;
double top = windowInfo.RowToPoints(cell.Row) + 5;
double width = 100;
double height = 30;
IShape shape = worksheet.Shapes.AddShape(AutoShapeType.Rectangle, left, top, width, height);
// Set the name of the shape for identification purposes.
shape.Name = "MyCustomControl";
}
finally
{
// NOTE: Must release the workbook set lock.
workbookView1.ReleaseLock();
}
buttonRunSample.Enabled = false;
}
// UIManager replacement class.
private class MyUIManager : SpreadsheetGear.Windows.Forms.UIManager
{
private Button _customControl;
public MyUIManager(IWorkbookSet workbookSet)
: base(workbookSet)
{
_customControl = null;
}
// Override to substitute a custom control for any existing shape in the worksheet.  
// This method is called when a control is first displayed within the WorkbookView.
public override System.Windows.Forms.Control CreateCustomControl(IShape shape)
{
// If the shape name matches...
if (String.Equals(shape.Name, "MyCustomControl"))
{
// Verify that a control does not already exist.
System.Diagnostics.Debug.Assert(_customControl == null);
// Create a custom control and set various properties.
_customControl = new Button();
_customControl.Text = "My Custom Button";
// Add a Click event handler.
_customControl.Click += new EventHandler(CustomControl_Click);
// Add an event handler so that we know when the control 
// has been disposed.  The control will be disposed when
// it is no longer in the viewable area of the WorkbookView.
_customControl.Disposed += new EventHandler(CustomControl_Disposed);
return _customControl;
}
return base.CreateCustomControl(shape);
}
private void CustomControl_Click(object sender, EventArgs e)
{
MessageBox.Show("Custom Control was Clicked!");
}
private void CustomControl_Disposed(object sender, EventArgs e)
{
// Add any cleanup code here...
// Set the custom control reference to null.
_customControl = null;
}
}
}

最新更新