dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Determining which radio button is selected

  1. #1
    Join Date
    Jun 2016
    Posts
    8

    Determining which radio button is selected

    I have created radio buttons at run time to select a particular file to access. The code is:

    Code:
                System.Windows.Forms.RadioButton[]
                    rb = new System.Windows.Forms.RadioButton[fileNamesSaved];
    
                for (int i =0; i < fileNamesSaved; i++)
                {
                    rb[i] = new RadioButton();
                    rb[i].Text = fileNames[i];
                    rb[i].Location = new System.Drawing.Point(10, 10+i * 20);
                    rb[i].Click += new EventHandler(rb_clicked);
                    this.Controls.Add(rb[i]);
                }
                test = 0;
            }
    
            private void ReturnToBaseForm_Click(object sender, EventArgs e)
            {
                Form1 f1 = new BaseForm.Form1();
                this.Hide();
                f1.ShowDialog();
            }
            private void rb_clicked(object sender, EventArgs e)
            {
                // need something in here to get the text associated with the particular button that was clicked.
            }
    The radio buttons are properly created and all use the rb_clicked event handler. What I want to do is to extract the text associated with the button that was clicked. If I look at sender in the debugger the text is there but I don't know how to extract it.

    I appreciate any help - have tried a number of possible way to do this but none of them worked

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,109

    Re: Determining which radio button is selected

    Put a breakpoint on the closing bracket of the clicked handler. Start debugging and click on a radio button. When the breakpoint hits look at the sender variable and notice its type (it should be a radio button). If it is that type you just need to cast the sender variable into a radio button to access its Text property.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)