-1

大家好,我有这个代码:我声明了这个全局:

    FlowLayoutPanel PresPanel = new FlowLayoutPanel();

我的初始化面板:

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

表单加载:

    InitPanel();
        PresPanel.SuspendLayout();

        BtnVote.CenterHorizontally();

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

我的 AddRadioButton :

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

        radio.CheckedChanged += radio_CheckedChanged;
    }

现在我的问题是,当我单击每个单选按钮时,如何从 AddradioButton 获取我的 getID?谢谢.. :)

4

1 回答 1

1

您可以使用该Tag属性:

public void AddRadioButton(string fullName, string imagePath, int getID)
{
    RadioButton radio = new RadioButton { Text = fullName, Parent = PresPanel };
    //.....
    radio.Tag = getID;
    radio.CheckedChanged += radio_CheckedChanged;
}
//then in the CheckedChanged event handler
private void radio_CheckedChanged(object sender, EventArgs e){
  RadioButton radio = sender as RadioButton;
  int getID = (int) (radio.Tag ?? -1);//suppose -1 is an invalid ID which is used to indicate that there is not associated ID
  //other code....
}
于 2013-10-19T08:03:54.327 回答