简体   繁体   中英

Multi radiobutton Click Event

anyone knows why this code not working?

what the code should do is get all the users in the list and make radiobutton for them and then with each click it will take the text inside the List in each user class and add them into textbox

but the thing is it only works for the last added radiobutton not all of them idk why.

 private void UserMessages()
    {
        int y = 8;
        int x = 7;
        
        if (TheClients.Count > 0)
        {
            foreach (HandleClients C1 in TheClients)
            {
                RB = new RadioButton();
                RB.Text = C1.ClientUser;
                RB.Location = new Point(x, y);
                RB.Font = PL_UsersCont.Font;
                RB.Visible = true;
                RB.AutoSize = true;
                RB.ForeColor = Color.Black;
                RB.FlatStyle = FlatStyle.Flat;
                PL_UsersCont.Controls.Add(RB);
                y += RB.Height;
            }
            RB.Click += RB_Click;
        }

    }

private void RB_Click(object sender, EventArgs e)
    {
        foreach (HandleClients MSGS in TheClients)
        {
            if (RB.Text == MSGS.ClientUser)
            {
                TXB_MSGS.Text = string.Empty;
                foreach (string M in MSGS.ClientMessages)
                {
                    TXB_MSGS.Text += M + "\r\n";
                }
            }
        }   
    }

I can see two problems in the code. The first is with assigning a handler to the Click event. To fix this you need to move the line

RB.Click += RB_Click;

to be inside the foreach loop. As it stands, your code is only adding the event handler for the last created RadioButton and not for each one that you create.

The second, you will also find a problem in the RB_Click Event Handler. To ensure the RadioButton that is selected is updated, add this line of code to the foreach loop:

RB.Tag = C1;

And then change the RB_Click handler to be:

private void RB_Click(object sender, EventArgs e)
{
    RadioButton thisRadioButton = sender as RadioButton;

    if (thisRadioButton != null)
    {
        HandleClients MSGS = thisRadioButton.Tag as HandleClients;
        TXB_MSGS.Text = string.Empty;

        TXB_MSGS.Text += String.Join (Environment.NewLine, MSGS.ClientMessages);
    }   
}

This code will now only work on the RadioButton that was clicked.

The easy way to do this is to avoid the RB_Click method altogether.

Try this:

private void UserMessages()
{
    int y = 8;
    int x = 7;

    if (TheClients.Count > 0)
    {
        foreach (HandleClients C1 in TheClients)
        {
            var rb = new RadioButton()
            {
                Text = C1.ClientUser,
                Location = new Point(x, y),
                Font = PL_UsersCont.Font,
                Visible = true,
                AutoSize = true,
                ForeColor = Color.Black,
                FlatStyle = FlatStyle.Flat,
            }
            rb.Click += (s, ea) => TXB_MSGS.Text = String.Join(Environment.NewLine, C1.ClientMessages);
            PL_UsersCont.Controls.Add(rb)
            y += rb.Height;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM