I'm kind of new at this,
It is a WindowsForm application
I have a group box with 8 Radio buttons in it. What i am trying to do is when i select a radio button from the group i want the text for that radio button to appear in the text box at the bottom of the form. Any ideas?
Loading

Kirtan PatelRank #292 · 8.6K reputationPosted Feb 17, 2010, 10:33 AM EST
Here is you super compact code !! to perform desire task :-)
public void RadioButtonChanged(Object Sender, EventArgs e)
{
// Show Radio Button text In Text box
textBox1.Text = ((RadioButton)Sender).Text;
}
private void Form1_Load(object sender, EventArgs e)
{
// Add Event Handlers to All radio Buttons
foreach (Control c in grpBeverages.Controls)
{
if (c is RadioButton)
{
((RadioButton)c).CheckedChanged += new EventHandler(RadioButtonChanged);
}
}
}
Nainil148 reputationPosted Feb 17, 2010, 9:20 AM EST
You should have your RadioButton fire the CheckChanged event. Inside of the event, cast the "sender" object as RadioButton and then simply set the Text property of the TextBox control to the RadioButton.Text. Following is the code that you can use:
private void radioButton_CheckedChanged(object sender, EventArgs e)
{
RadioButton oRadioButton = sender as RadioButton;
textBox1.Text = oRadioButton.Text;
}
You can have each of the RadioButton object's CheckChanged event to the above function. That should do it. Hope this helps.
kevin15 reputationPosted Feb 17, 2010, 10:00 AM EST
For ex, i have 8 radio buttons,
btnCoke
btnPepsi
btnDew
btnSprite
btnSevenUp
btnGingerale
btnLemonaid
btnWine
How would the whole code look like? :$
Where would the radiobuttons objects be placed:S
Kirtan PatelRank #292 · 8.6K reputationPosted Feb 17, 2010, 10:09 AM EST
[email protected]
kevin15 reputationPosted Feb 17, 2010, 10:35 AM EST
kevin15 reputationPosted Feb 17, 2010, 5:08 PM EST
How would i got about selecting a radiobutton and 2 items from list box and showing all 3 in textbox.
kiran kumarRank #3,110 · 461 reputationPosted Feb 17, 2010, 10:27 PM EST
HERE IS THE CODE FOR YOU KEVIN
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace radlis1lis2
{
public partial class Form1 : Form
{
string[] hi = { "kiran", "kumar" };
string[] hello = { "hi", "kevin" };
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < hi.Length; i++)
{
listBox1.Items.Add(hi[i]);
}
for (int j = 0; j < hello.Length; j++)
{
listBox2.Items.Add(hello[j]);
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (radioButton1.Checked)
{
radioButton1.Checked = false;
textBox1.Text += listBox1.SelectedItem.ToString();
}
else
{
textBox1.Text += listBox1.SelectedItem.ToString();
}
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
if (radioButton1.Checked)
{
radioButton1.Checked = false;
textBox1.Text += listBox2.SelectedItem.ToString();
}
else
{
textBox1.Text += listBox2.SelectedItem.ToString();
}
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if (radioButton1.Checked)
{
textBox1.Text += radioButton1.Text;
}
}
}
}
mark the answer if it helps you
kiran kumarRank #3,110 · 461 reputationPosted Feb 17, 2010, 10:32 PM EST
MARK THE ANSWER IT HELPS YOU