0

我有这 2 个动态FlowLayoutPanels,它带有动态单选按钮。现在我的问题是,是否有可能将两个FlowLayoutPanels 合二为一Form_Load?因为,我做了这样的:

    private void FVotingArea_Load(object sender, EventArgs e)
    {
        PanelPresident();

        PresPanel.SuspendLayout();

        BtnVote.CenterHorizontally();

        try
        {
            string cmdPres = "SELECT (FirstName + ' ' + MiddleName + ' ' + LastName) as PresName, " +
                        "imgPath as ImagePath, " + "id as PresID FROM TableVote WHERE Position='President'";
            using (SqlCommand Prescom = new SqlCommand(cmdPres, sc))
            {
                if (sc.State != ConnectionState.Open) sc.Open();
                SqlDataReader Presreader = Prescom.ExecuteReader();
                while (Presreader.Read())
                {
                    PresRadioButton(Presreader.GetString(0), Presreader.GetString(1), Presreader.GetInt32(2));
                }
                Presreader.Close();
                sc.Close();
                PresPanel.ResumeLayout(true);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

        PanelIVPresident();
        IVPresPanel.SuspendLayout();
        try
        {
            string cmdIVPres = "SELECT (FirstName + ' ' + MiddleName + ' ' + LastName) as IVPresName, " +
                            "imgPath as ImagePath, " + "id as IVPresID FROM TableVote WHERE Position='Internal VP'";
            using (SqlCommand IVPrescom = new SqlCommand(cmdIVPres, sc))
            {
                if (sc.State != ConnectionState.Open) sc.Open();
                SqlDataReader IVPresreader = IVPrescom.ExecuteReader();
                while (IVPresreader.Read())
                {
                    IVPresRadioButton(IVPresreader.GetString(0), IVPresreader.GetString(1), IVPresreader.GetInt32(2));
                }
                IVPresreader.Close();
                sc.Close();
                IVPresPanel.ResumeLayout(true);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

但似乎我radiobutton的 forInternal VP不去 2nd FlowLayoutPanel。它只是去第一个FlowLayoutPanel总统所在的地方。

原来是这样的:

    public FVotingArea()
    {
        InitializeComponent();
        Load += FVotingArea_Load;
    }

    FlowLayoutPanel PresPanel = new FlowLayoutPanel();

    private void PanelPresident()
    {
        Label PresLabel = new Label();
        PresLabel.Text = "PRESIDENT :";
        PresLabel.AutoSize = true;
        PresLabel.Location = new Point(30, 20);
        PresLabel.Font = new Font(this.Font, FontStyle.Bold);
        PresLabel.Font = new Font("Courier New", 18);
        PresLabel.ForeColor = Color.Orange;
        this.Controls.Add(PresLabel);

        PresPanel.Size = new Size(630, 160);
        PresPanel.Location = new Point(50, 50);
        PresPanel.FlowDirection = FlowDirection.LeftToRight;
        PresPanel.BorderStyle = BorderStyle.FixedSingle;
        PresPanel.AutoScroll = true;
        PresPanel.WrapContents = false;
        Controls.Add(PresPanel);
    }

    private void FVotingArea_Load(object sender, EventArgs e)
    {
        PanelPresident();

        PresPanel.SuspendLayout();

        BtnVote.CenterHorizontally();

        try
        {
            string cmdPres = "SELECT (FirstName + ' ' + MiddleName + ' ' + LastName) as PresName, " +
                        "imgPath as ImagePath, " + "id as PresID FROM TableVote WHERE Position='President'";
            using (SqlCommand Prescom = new SqlCommand(cmdPres, sc))
            {
                if (sc.State != ConnectionState.Open) sc.Open();
                SqlDataReader Presreader = Prescom.ExecuteReader();
                while (Presreader.Read())
                {
                    PresRadioButton(Presreader.GetString(0), Presreader.GetString(1), Presreader.GetInt32(2));
                }
                Presreader.Close();
                sc.Close();
                PresPanel.ResumeLayout(true);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    public void PresRadioButton(string PresName, string imagePath, int PresID)
    {
        RadioButton Presradio = new RadioButton { Text = PresName, Parent = PresPanel };
        Presradio.AutoSize = false;
        Presradio.Size = new Size(150, 130);
        Presradio.Image = new Bitmap(Image.FromFile(imagePath), 90, 90);
        Presradio.TextImageRelation = TextImageRelation.ImageAboveText;
        Presradio.CheckAlign = ContentAlignment.BottomCenter;
        Presradio.CheckAlign = ContentAlignment.BottomCenter;
        Presradio.ImageAlign = ContentAlignment.MiddleCenter;
        Presradio.TextAlign = ContentAlignment.MiddleCenter;
        Presradio.ForeColor = Color.LimeGreen;

        Presradio.Tag = PresID;
        Presradio.CheckedChanged += Presradio_CheckedChanged;
    }

    public void Presradio_CheckedChanged(object sender, EventArgs e)
    {
        try
        {
            RadioButton Presradio = sender as RadioButton;
            int PresID = (int)(Presradio.Tag ?? -1);

            TxtPresID.Text = PresID.ToString();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

但是我该如何制作另一个flowlayoutpanel?我会在里面做form_load吗?因为我试过这样做,但所有的职位都Internal VP去了总统的flowlayoutpanel..它没有去flowlayoutpanel我新做的新的。

4

0 回答 0