为什么我的自定义lookupEdit抛出以下错误

发布于 2024-12-04 12:27:42 字数 2909 浏览 0 评论 0原文

错误:索引超出范围 必须为非负值且小于集合的大小

我有一个自定义的 LookUpEdit -

public class MyLookUpEdit : LookUpEdit

我参考以下链接将 MyLookUpEdit 添加到功能区。现在,MyLookUpEdit 在我的 RibbonPageGroup-->Add Editor 中可用。

http://www.devexpress.com/Support/Center/KB /ViewKBIssue.aspx?kbid=A1237

这是 MyLookUpEdit 的 InitializeControl 方法:

public void InitializeControl(ICollection cache, string columnField1, string columnField2, string valueField, bool isMultiColumn, int searchColumn)
        {
            Properties.ForceInitialize();
            const int maxDropDownSize = 7;
            Properties.Columns.Clear();

        if(isMultiColumn)
        {
                Properties.Columns.AddRange(new[] {
                new LookUpColumnInfo(columnField1, columnField1, columnField1.Length*6, FormatType.None, "", true, HorzAlignment.Near, DevExpress.Data.ColumnSortOrder.None),
                new LookUpColumnInfo(columnField2, columnField2, 100, FormatType.None, "", true , HorzAlignment.Near, DevExpress.Data.ColumnSortOrder.None)});
                Properties.ShowHeader = true;
                Properties.PopupFormMinSize = new Size(0, 0);
                Properties.AppearanceDropDownHeader.TextOptions.HAlignment = HorzAlignment.Near;
                Properties.AutoSearchColumnIndex = searchColumn;
        }
        else
        {
                Properties.Columns.AddRange(new[]{
                new LookUpColumnInfo(columnField1, columnField1, 10, FormatType.None, "", true, HorzAlignment.Near, DevExpress.Data.ColumnSortOrder.None),
                new LookUpColumnInfo(columnField2, columnField2, 0, FormatType.None, "", false , HorzAlignment.Near, DevExpress.Data.ColumnSortOrder.None)});
                Properties.ShowHeader = false;
                Properties.PopupFormMinSize = new Size(10, 10); // set popup width to control width

                break;
        }

        Properties.ShowFooter = false;
        Properties.ShowLines = true;
        Properties.ValueMember = valueField;
        Properties.DisplayMember = columnField1;
        Properties.DataSource = cache;
        if (cache == null) return;
        Properties.DropDownRows = cache.Count > maxDropDownSize ? maxDropDownSize : cache.Count;

        ColumnBestFit();
        HideIcon();
    }

但是在运行时,在功能区中,我无法单击按钮并查看所有可用值Lookupedit - 看起来弹出窗口没有打开,当我离开编辑时,以下代码会抛出错误:

    protected override void OnLeave(EventArgs e)
    {
        if (IsPopupOpen) 
        {
            ItemIndex = PopupForm.SelectedIndex;
            _selectfromPopup = true;
        }
    Properties.DisplayMember = Properties.Columns[0].Caption; // This line throw the error

        base.OnLeave(e);

        if (textChanged)
            OnLeaveWithChangedText(e);

    }

Error: Index was out of range Must be non-negative and less than the size of the collection

I have a customized LookUpEdit -

public class MyLookUpEdit : LookUpEdit

I refer to the following link to add MyLookUpEdit to a ribbon. Now MyLookUpEdit is available in my ribbonPageGroup-->Add Editor.

http://www.devexpress.com/Support/Center/KB/ViewKBIssue.aspx?kbid=A1237

Here is the InitializeControl Method for MyLookUpEdit:

public void InitializeControl(ICollection cache, string columnField1, string columnField2, string valueField, bool isMultiColumn, int searchColumn)
        {
            Properties.ForceInitialize();
            const int maxDropDownSize = 7;
            Properties.Columns.Clear();

        if(isMultiColumn)
        {
                Properties.Columns.AddRange(new[] {
                new LookUpColumnInfo(columnField1, columnField1, columnField1.Length*6, FormatType.None, "", true, HorzAlignment.Near, DevExpress.Data.ColumnSortOrder.None),
                new LookUpColumnInfo(columnField2, columnField2, 100, FormatType.None, "", true , HorzAlignment.Near, DevExpress.Data.ColumnSortOrder.None)});
                Properties.ShowHeader = true;
                Properties.PopupFormMinSize = new Size(0, 0);
                Properties.AppearanceDropDownHeader.TextOptions.HAlignment = HorzAlignment.Near;
                Properties.AutoSearchColumnIndex = searchColumn;
        }
        else
        {
                Properties.Columns.AddRange(new[]{
                new LookUpColumnInfo(columnField1, columnField1, 10, FormatType.None, "", true, HorzAlignment.Near, DevExpress.Data.ColumnSortOrder.None),
                new LookUpColumnInfo(columnField2, columnField2, 0, FormatType.None, "", false , HorzAlignment.Near, DevExpress.Data.ColumnSortOrder.None)});
                Properties.ShowHeader = false;
                Properties.PopupFormMinSize = new Size(10, 10); // set popup width to control width

                break;
        }

        Properties.ShowFooter = false;
        Properties.ShowLines = true;
        Properties.ValueMember = valueField;
        Properties.DisplayMember = columnField1;
        Properties.DataSource = cache;
        if (cache == null) return;
        Properties.DropDownRows = cache.Count > maxDropDownSize ? maxDropDownSize : cache.Count;

        ColumnBestFit();
        HideIcon();
    }

But at the run time, in ribbon, I cannot click the button and see all available values for the lookupedit - it looks like the popup is not open, and when I leave the edit, the following code throw the error:

    protected override void OnLeave(EventArgs e)
    {
        if (IsPopupOpen) 
        {
            ItemIndex = PopupForm.SelectedIndex;
            _selectfromPopup = true;
        }
    Properties.DisplayMember = Properties.Columns[0].Caption; // This line throw the error

        base.OnLeave(e);

        if (textChanged)
            OnLeaveWithChangedText(e);

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤城病女 2024-12-11 12:27:42

列列表为空的原因可能有不同,这取决于整个表单事件和方法链等等,但为了确保使用防御方法(我的瑞士军刀;-)),

只需在之前进行检查:

Properties.DisplayMember = Properties.Columns[0].Caption;

所以如果 Properties.Columns.Length > 则执行该行0

这并不能解决所有问题,但据我现在的理解,将防止该异常,并且您的代码将继续运行(但如果错误实际上发生在其他地方,那么最终不会真正执行您所期望的操作该列表不应为空)。

there could be different reasons why the columns list is empty, it depends on the whole form events and methods chain and whatsoever, but to be sure and use a defensive approach (my swiss army knife ;-) )

just put a check before:

Properties.DisplayMember = Properties.Columns[0].Caption;

so to execute that row only if the Properties.Columns.Length > 0

this won't solve everything but as I understand it right now, will prevent that exception and your code will keep running (but eventually not really doing exactly what you expect from it if the error is somewhere else in the fact that list should not be empty).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文