【问题标题】:c# How to access the items from a parent class combobox?c#如何从父类组合框访问项目?
【发布时间】:2018-06-18 23:10:00
【问题描述】:

在“对话框”表单中的其他组件中,我有这个组合框。我想声明它的数据源或其项目列表与我在设计中创建的前一个组合框相同,并且我从文件中加载了它的数据。我该怎么做?

public partial class CardBuilder : Form
{
    public CardBuilder()
    {
        InitializeComponent();

        private static class A
        {
            private static string ShowDialog(string T, string C, int AT)
            {
                int WX = system.Windows.Forms.SystemInformation.WorkingArea.Width,
                    WY = system.Windows.Forms.SystemInformation.WorkingArea.Height;
                string AbilityResult = "";

                Form dialog = new Form
                {
                    Font = new System.Drawing.Font("Garamond", 15,
                    System.Drawing.FontStyle.Regular,
                    System.Drawing.GraphicsUnit.Pixel, ((byte)(0))),
                    FormBorderStyle = FormBorderStyle.Fixed3D,
                    Text = C,
                    StartPosition = FormStartPosition.Manual,
                    Location = new Point(WY - WY / 4, (WX - 350) / 2),
                    AutoSize = false,
                    SizeGripStyle = SizeGripStyle.Hide,
                    Owner = CardBuilder.ActiveForm,
                    ShowIcon = false,
                    ControlBox = false,
                    TopMost = true,
                    Width = 350,
                    Height = 140
                };
                ComboBox CT = new ComboBox()
                {
                    DataSource = ??
                    Sorted = true
                };
                return Result;
            }
        }

【问题讨论】:

  • “与我在设计中创建的前一个组合框相同”是什么意思?在这种情况下,前面的组合框代码是什么?
  • 主类有一个组合框,它是由 VS 自动创建的,我从文件中加载了它的项目。我假装将这些项目的内容复制到 CT 组合框中
  • 然后您只需引用该组合框并设置CT.DataSource = this.comboBox1.DataSource;。您只需要获取现有的组合框名称属性并通过该名称引用它。
  • 我试过了,我得到了 CS0120 错误(非静态字段需要对象引用)
  • 我解决了。它的工作原理与复制组合框范围相同,使用 AddRange() 方法,如下所示:CT.Items.AddRange(comboBox1.Items.OfType().ToArray());

标签: c# winforms class combobox datasource


【解决方案1】:

这是解决方案,您可以将其标记为已回答:)

    private static class A
    {
        private static string ShowDialog(string T, string C, int AT, ComboBox copyFrom)
        {
            int WX = system.Windows.Forms.SystemInformation.WorkingArea.Width,
                WY = system.Windows.Forms.SystemInformation.WorkingArea.Height;
            string AbilityResult = "";

            Form dialog = new Form
            {
                Font = new System.Drawing.Font("Garamond", 15,
                System.Drawing.FontStyle.Regular,
                System.Drawing.GraphicsUnit.Pixel, ((byte)(0))),
                FormBorderStyle = FormBorderStyle.Fixed3D,
                Text = C,
                StartPosition = FormStartPosition.Manual,
                Location = new Point(WY - WY / 4, (WX - 350) / 2),
                AutoSize = false,
                SizeGripStyle = SizeGripStyle.Hide,
                Owner = CardBuilder.ActiveForm,
                ShowIcon = false,
                ControlBox = false,
                TopMost = true,
                Width = 350,
                Height = 140
            };
            ComboBox CT = new ComboBox()
            {
                DataSource = copyFrom.DataSource,
                Sorted = true
            };
            return Result;
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-05
    • 2017-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多