0

我有两个自定义 ListBox 类:

public class MyListBox : ListBox
public class MyCheckedListBox : ListBox

它们是 winformsSystem.Windows.Forms.ListBox控件的包装器,用于添加一些实现和样式等。

我为每个代码添加了以下代码,以便在必要时删除垂直滚动条:

private const int WS_VSCROLL = 0x00200000;
private bool verticalScrollbar = true;

[DefaultValue(true)]
public bool VerticalScrollbar
{
    get { return this.verticalScrollbar; }
    set
    {
        if (this.verticalScrollbar != value)
        {
            this.verticalScrollbar = value;
            this.RecreateHandle();
        }
    }
}

protected override System.Windows.Forms.CreateParams CreateParams
{
    get
    {
        System.Windows.Forms.CreateParams parms = base.CreateParams;
        if (!this.verticalScrollbar)
            parms.Style &= ~WS_VSCROLL;
        return parms;
    }
}

我将每个控件添加到现有用户控件ProfileGeneralPanel中,它们按预期工作,默认情况下显示滚动条,将VerticalScrollbar属性设置为 false 将其删除。虽然,现在需要在多个地方使用这种安排,所以我将这个结构提取到一个单独的新用户控件中,称为PrivilegesListView. 这个新控件在一个表中有一个MyCheckedListBox和两个。MyListBox这三个都VerticalScrollbar设置为false。到目前为止一切都很好,设计师正确地显示了一切。

但是,当我尝试将此控件从工具箱拖到另一个用户控件上时,会引发异常,说明它找不到方法:MyCheckedListBox.set_VerticalScrollbar(Boolean)

有问题的方法显然是属性的生成方法。我已经尝试了所有常规方法,清理并重建,重新启动了 Visual Studio。出于绝望,我还检查了构建时生成的 IL,并且该类MyCheckedListBox确实定义了该方法。任何想法为什么它无法找到它?

请注意,控件本身的设计器每次都可以正常打开,只有将其添加到另一个表单时才会出现问题。

请注意,该问题仅在VerticalScrollbar设置为 false 时发生,无论是在设计器生成的代码中PrivilegesListView还是在构造函数本身中手写。

注意:不幸的是,在这个阶段不可能使用像 a 这样的其他控件DataGridView来代替列表框......

4

1 回答 1

3

This goes wrong when you previously added the control to the Toolbox with the "Choose items" dialog. That makes a copy of the control assembly, stored in a private directory where toolbox item assemblies are kept. You can see this go wrong now perhaps, you are putting an old version of the control on your form, one that didn't yet have the added method.

The best way to avoid this trap is to let Visual Studio automatically add controls you are working on to the toolbox. Make sure the setting is still correct, it tends to get changed by unwise attempts at improving VS perf. Tools + Options, Window Forms Designer, General, AutoToolboxPopulate should be set to True. Then any project in your solution that has a class that derives from Control or Component will have its controls automatically added to the top of the toolbox after you compile. Changes you make to the control code are now always in sync.

In general, use Fuslogvw.exe to troubleshoot assembly resolution problems. It works just as well for VS as it does for your own programs. You want to log all bindings so you also see the ones that succeeded but might have picked a copy of the assembly from the wrong folder.

于 2012-05-01T11:40:22.157 回答