在Excel电子表格编程C#创建复选框 [英] Programmatically create checkboxes in c# in an excel spreadsheet

查看:594
本文介绍了在Excel电子表格编程C#创建复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为项目的一部分,我的工作,我需要能够到Excel电子表格中创建复选框,任何人都可以提供一个简单的例子,或直接我一个有用的参考?
我使用Excel互操作。

As part of a project I am working on, I a need to be able to create checkboxes inside an excel spreadsheet, could anyone provide a simple example or direct me to a useful reference? I am using the excel Interop.

先谢谢了。

推荐答案

有两种,你可以把工作表上的控件的ActiveX控件工具箱控制,并从表格(或图)工具栏窗体控件。

There are two kinds of controls that you can put on a worksheet, ActiveX controls from the Control Toolbox and Forms controls from the Forms (or Drawing) toolbar.

在一般情况下,这两种类型的控制是在它们的工作方式相同相似。也就是说,从绘图工具栏的文本框是可以在其中键入文本的控制。这是基本相同的的ActiveX文本框。任一类型的复选框用于选择或取消选择一个选项。

In general, the two types of controls are similar in that they work the same way. That is, a textbox from the Drawing toolbar is a control in which you can type text. That’s fundamentally the same as the ActiveX textbox. Checkboxes of either type are used to select or deselect an option.

显然,使用一个或其他将根据他们之间的分歧,而不是他们的相似性决定。主要的差异,我反正是事件和格式化。 ActiveX控件给电子表格设计者更多的灵活性,当涉及到显示或反应的控件的事件。查看一个ActiveX控件的属性演示了如何更与他们做比窗体控件。此外,Forrms控件有一个基本事件,单击事件,您可以通过指定一个宏来控制访问。 ActiveX控件,而另一方面,有很多事件。该ActiveX组合框,例如,有十五的事件。

Obviously, a decision to use one or the other will based on their differences, not their similarities. The primary differences, for me anyway, are events and formatting. ActiveX controls give the spreadsheet designer a lot more flexibility when it comes to displaying or reacting to events of controls. Viewing the properties of an ActiveX control demonstrates how much more you can do with them than with Forms controls. Also, Forrms controls have one basic event, the click event, which you access by assigning a macro to the control. ActiveX controls, on the other hand, have a lot of events. The ActiveX combobox, for instance, has fifteen events.

所有的灵活性是有代价的(你知道它会)。 ActiveX控件随身携带了更多开销和有被越野车的历史。

All that flexibility comes at a cost (you knew it would). ActiveX controls carry a lot more overhead with them and have a history of being buggy.

称量好的和坏的,我总是用表单控件,除非我有一个具体的理由使用ActiveX。表单控件是精简的意思是,简单易用。但是,如果我需要一个组合框来显示某种字体,我没有选择,只能使用ActiveX控件。同样的,如果我需要,以避免在一个文本框某些字符,我需要KeyPress事件是不能从形式文本框。所以我的建议,我的做法是,除非我需要的功能,只有ActiveX控件提供使用窗体控件。

Weighing the good and the bad, I always use Forms controls unless I have a specific reason to use an ActiveX. Forms controls are lean and mean and simple to use. If, however, I need a combobox to display a certain font, I have no choice but to use an ActiveX control. Similarly, if I need to avoid certain characters in a textbox, I’ll need the KeyPress event which is not available from the Forms textbox. So my advice, and my practice, is to use Forms controls unless I need functionality that only ActiveX controls offer.

窗体复选框添加代码

 ActiveSheet.CheckBoxes.Add 87, 18, 72, 17.25

的ActiveX复选框添加代码

ActiveX Checkbox add code

    ActiveSheet.OLEObjects.Add(ClassType:="Forms.CheckBox.1", Link:=False, _
    DisplayAsIcon:=False, Left:=69.75, Top:=59.25, Width:=117.75, Height _
    :=14.25)

这代码是VBA,但可以很容易地通过净互操作

This code is VBA but can easily be invoked from .Net via interop

这篇关于在Excel电子表格编程C#创建复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆