c#如何从父类组合框访问项目?

时间:2018-01-09 20:35:14

标签: c# winforms class combobox datasource

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

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;
            }
        }

1 个答案:

答案 0 :(得分:0)

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

    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;
        }
    }