我正在使用spreadsheetgear,我想将组合框(ComponentOne)放入1个单元格中。我希望当用户转到此单元格时,此组合框将激活并向用户显示列表。用户在列表中选择项目后,会将此项目放入单元格值并隐藏组合框。 如何在Spreadsheetgear中完成。
谢谢,
大一
答案 0 :(得分:1)
我不熟悉ComponentOne控件,所以无法真正说出你问题的这一部分。但是,关于将自定义控件嵌入SpreadsheetGear WorkbookView UI控件的更多通用方法,可以通过对UIManager类进行子类化来实现,这样就可以拦截工作表上现有形状的创建并用您自己的自定义控件替换它们。
下面是一个简单的示例,它使用Windows Forms WorkbookView control和SpreadsheetGear.Windows.Forms.UIManager的子类来演示这一点。此示例仅使用按钮替换矩形AutoShape。您可以修改它以显示ComponentOne CheckBox。
请注意,只要在WorkbookView上滚动到视图/显示形状,就会调用UIManager。CreateCustomControl(...)方法。另请注意,每次将自定义控件滚出视图或以其他方式使其不可见时,您的自定义控件都将被丢弃。有关此API的更多详细信息,请参阅文档。
关于形状和工作表的另一个重要观点 - 形状不是嵌入 in 单元格。相反,它们悬停在细胞上。因此,给定形状和给定单元格之间不存在明确的“链接”。您最接近进行此类关联的方法是使用IShape。TopLeftCell或BottomRightCell属性,这些属性将提供此形状各自边缘所在的范围。 IShape interface包含许多其他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;
}
}
}