解决 CustomCollectionEditor-自定义外观

tim8w

知名会员
已加入
2020年9月8日
留言内容
64
编程经验
10+
你好
我希望能够控制“集合编辑器”的大多数方面。首先,我要承认,这里提出的大多数想法都来自在网上搜寻的各种帖子和示例。在这一点上,我不记得每个想法的来历,因此,如果其中任何一个是您的,请告诉我,我将在这篇文章中表示赞赏。无论如何,我只会发布CustomerCollectionEditor。注释解释了它的大部分工作。如果您需要更多说明,请给我发送便条。

C#:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualBasic;
using System.ComponentModel.Design;
using System.Windows.Forms;

namespace Defects
{
    public class MyCollectionEditor : CollectionEditor
    {
        public static bool bEditingCollection = false;

        public delegate void MyFormClosedEventHandler(object sender, FormClosedEventArgs e);

        public event MyFormClosedEventHandler MyFormClosed;

        public delegate void MyFormLoadEventHandler(object sender, System.EventArgs e);

        public event MyFormLoadEventHandler MyFormLoad;


        public delegate void MyFormControlAddedEventHandler(object sender, System.EventArgs e);

        public event MyFormControlAddedEventHandler MyFormControlAdded;

        public MyCollectionEditor(Type type)
            : base(type)
        {
        }

        protected override string GetDisplayText(object value)
        {
            Defect item = new Defect();
            item = (Defect)value;

            if (String.IsNullOrEmpty(item.DefectCode))
                return base.GetDisplayText(" ");
            else
            {
                String sDisplay = item.DefectCode.ToString().Substring(0, item.DefectCode.ToString().IndexOf(":"));
                return base.GetDisplayText(string.Format("{0}", sDisplay));
            }
        }

        protected override CollectionForm CreateCollectionForm()
        {
            CollectionForm collectionForm = base.CreateCollectionForm();

            // Turn OFF 救命 Button
            collectionForm.HelpButton = false;

            // Set the BackColor, StartPosition and Size of the form
            collectionForm.BackColor = Color.FromName("SteelBlue");
            collectionForm.StartPosition = FormStartPosition.CenterParent;
            collectionForm.Size = new Size(660, 500);

            // Add Form Handlers for trapping Load and Close
            collectionForm.FormClosed += this.collection_FormClosed;
            collectionForm.Load += this.collection_FormLoad;

            var overArchingTableLayoutPanel = collectionForm.Controls["overArchingTableLayoutPanel"];

            var addRemoveTableLayoutPanel = overArchingTableLayoutPanel.Controls["addRemoveTableLayoutPanel"];
            // Set the BackColor of the Buttons - It appears that they get set by default to the form's BackColor
            addRemoveTableLayoutPanel.Controls["addButton"].BackColor = Color.FromName("Control");
            addRemoveTableLayoutPanel.Controls["removeButton"].BackColor = Color.FromName("Control");
            // Add Handlers for trapping Add and Remove Buttons
            addRemoveTableLayoutPanel.Controls["addButton"].Click += AddButton_Click;
            addRemoveTableLayoutPanel.Controls["removeButton"].Click += RemoveButton_Click;

            var okCancelTableLayoutPanel = overArchingTableLayoutPanel.Controls["okCancelTableLayoutPanel"];
            // Set the BackColor of the Buttons - It appears that they get set by default to the form's BackColor
            okCancelTableLayoutPanel.Controls["okButton"].BackColor = Color.FromName("Control");
            okCancelTableLayoutPanel.Controls["cancelButton"].BackColor = Color.FromName("Control");
            // Add Handlers for trapping OK and Cancel Buttons
            okCancelTableLayoutPanel.Controls["okButton"].Click += OKButton_Click;
            okCancelTableLayoutPanel.Controls["cancelButton"].Click += CancelButton_Click;

            // Change the Member Labels' Text and Color
            overArchingTableLayoutPanel.Controls["membersLabel"].Text = "&Defect 清单:";
            overArchingTableLayoutPanel.Controls["membersLabel"].ForeColor = Color.FromName("White");
            // Change the Properties Labels' Text and Color
            overArchingTableLayoutPanel.Controls["propertiesLabel"].Text = "&Properties:";
            overArchingTableLayoutPanel.Controls["propertiesLabel"].ForeColor = Color.FromName("White");
            // Set the BackColor of the Buttons - It appears that they get set by default to the form's BackColor
            overArchingTableLayoutPanel.Controls["upButton"].BackColor = Color.FromName("Control");
            overArchingTableLayoutPanel.Controls["downButton"].BackColor = Color.FromName("Control");
            // Add Handlers for trapping Up and Down Buttons
            overArchingTableLayoutPanel.Controls["upButton"].Click += UpButton_Click;
            overArchingTableLayoutPanel.Controls["downButton"].Click += DownButton_Click;

            var propertyBrowser = overArchingTableLayoutPanel.Controls["propertyBrowser"] as PropertyGrid;
            if (propertyBrowser.Controls.Count != 0)
            {
                // Set the PropertySorting and ToolbarVisibilty
                propertyBrowser.PropertySort = PropertySort.NoSort;
                propertyBrowser.ToolbarVisible = false;
            }

            // Do something based on which Collection Editor was called
            switch (collectionForm.Text)
            {
                case "Defect Collection Editor":
                    {
                        break;
                    }
            }

            // Hide Add, Remove, Up and Down Buttons
            //collectionForm.Controls["overArchingTableLayoutPanel"].Controls["addRemoveTableLayoutPanel"].Controls["addButton"].Visible = false;
            //collectionForm.Controls["overArchingTableLayoutPanel"].Controls["addRemoveTableLayoutPanel"].Controls["removeButton"].Visible = false;
            //collectionForm.Controls["overArchingTableLayoutPanel"].Controls["upButton"].Visible = false;
            //collectionForm.Controls["overArchingTableLayoutPanel"].Controls["downButton"].Visible = false;

            return collectionForm;
        }

        private void collection_FormLoad(object sender, System.EventArgs e)
        {
            bEditingCollection = true;
        }

        private void AddButton_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Add Button Clicked");
        }

        private void RemoveButton_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Remove Button Clicked");
        }

        private void OKButton_Click(object sender, EventArgs e)
        {
            MessageBox.Show("OK Button Clicked");
        }

        private void CancelButton_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Cancel Button Clicked");
        }

        private void UpButton_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Up Button Clicked");
        }

        private void DownButton_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Down Button Clicked");
        }

        private void collection_FormClosed(object sender, FormClosedEventArgs e)
        {
            bEditingCollection = false;
        }
    }
}

我希望可以更改一些内容(主要是“字体大小”),但是其中许多内容是"Read-only"。我什至尝试创建新的控件来覆盖我无法自定义的控件,但是我无法将它们放置在想要的位置。如果有人有任何想法,请告诉我。
 

tim8w

知名会员
已加入
2020年9月8日
留言内容
64
编程经验
10+
字体大小是只读的,因为您必须创建一个新的字体对象并设置其大小。 字体构造器(System.Drawing)
约翰,
太棒了!这适用于窗体的字体。当列表为空时,它也适用于单独的“ membersLabel”和“ propertiesLabel”以进行初始加载。当列表中有成员时,成员名称将放在'propertiesLabel'的前面,并且字体返回其原始字体。
 
Last edited:

tim8w

知名会员
已加入
2020年9月8日
留言内容
64
编程经验
10+
修改的另一件事是成员区域的宽度("listbox")和物业区域("propertyBrowser")。两者都具有Size()属性,但是当您对其进行编辑时,它们不会更改。您需要使用反射还是要做些什么?
 
Last edited:

tim8w

知名会员
已加入
2020年9月8日
留言内容
64
编程经验
10+
修改的另一件事是成员区域的宽度("listbox")和物业区域("propertyBrowser")。两者都具有Size()属性,但是当您对其进行编辑时,它们不会更改。您需要使用反射还是要做些什么?
我收到Reza Aghaei的一封电子邮件,指出该机器人"listbox" and "propertyBrowser"将锚定为"上,下,左,右"。他建议将它们设置为"Top,Left"将允许我设置新的Size()"listbox"。然后,只需对"addButton" and "removeButton"并调整的Width()"addButton" and "removeButton"s,一切都作了相应调整。以下是所有定制的结果:

CollectionEditor Resized.png
 

羊皮

退休程序员
工作人员
已加入
2018年9月5日
留言内容
1,926
地点
英国
编程经验
10+
您是要共享给他人使用的此主题代码,还是正在寻求支持的主题?

通常,在共享提示之前进行研究是个好主意。

只是想知道我们是否可以在您的主题上设置适当的标签,以免误导读者。
 

tim8w

知名会员
已加入
2020年9月8日
留言内容
64
编程经验
10+
您是要共享给他人使用的此主题代码,还是正在寻求支持的主题?

通常,在共享提示之前进行研究是个好主意。

只是想知道我们是否可以在您的主题上设置适当的标签,以免误导读者。
我认为它已经完成。我发现了一些针对特定情况的其他信息。如果需要,我可以删除它,然后输入我现在拥有的完整解决方案。
 

羊皮

退休程序员
工作人员
已加入
2018年9月5日
留言内容
1,926
地点
英国
编程经验
10+
只需将您所做的更改重新发布为TIP即可,并说明其用法和功能。

我已将此主题设置为已解决。
 
最佳 底部