C# giving radio buttons and panels a value - c#

I am very new to C# and am trying to make a form program with a lot of radio buttons, i was hoping to have something to the effect of
radioButton1.value = 1;
radioButton2.value = 2;
panel1.Value = Selected radiobuttons value;
or just something like that without having to write out so many if/switch statements.

You can use LINQ to get the selected button.
Assume that your radioButtons are in form, so the variable this means the class form.
var selectedButton = this.Controls.OfType<RadioButton>()
.FirstOrDefault(r => r.Checked);
Console.WriteLine(selectedButton.Text);

Related

How to check only one radio button at a time (others disable)

I am working in silverlight c# and i have situation where i create radio buttons programatically. My code to do so is this:
Grid childGrid = CreateChildGrid();
int NumberOfRadioButton =0;
RadioButton[] RadioBut= new RadioButton[5];
int count = 0;
foreach (var item in param.Component.Attributes.Items)// this param.Component.Attributes.Items value is 4 in fact.
{
NumberOfRadioButton++;
RadioBut[count] = new RadioButton();
RadioBut[count].GroupName = item;
RadioBut[count].Content = item;
sp.Children.Add(RadioBut[count]);
count++;
}
Problem: The problem is it checks all the button where as i want only one button checked at a time . I mean if one checked the others must be disable.
Could some one please help me to achieve my target ? Thanks a lot.
Note: I am using silverlight to do so.
You should set the GroupName property of all RadioButtons to the same, so they will be "grouped", meaning only one of them can be selected at a time.
So this line:
rbs[count].GroupName = item;
Should be something like this:
rbs[count].GroupName = "MyRadioButtonGroup";
Of course the string can be anything, as long as it is the same for all RadioButtons.
You'll have to assign these radio buttons to a common group. Check Radio Button Group Name
RadioButton[] rbs = new RadioButton[5];
rbs.GroupName = "Add your common groupname here"; // added code
int count = 0;

get a value from a radio button

I have a radio button in my .aspx page and in the code behind I have written the following:
if (rbOnlyCreatedorUpdated.Checked == true)
{
searchObject.CreatedOrUpdatedOnValidDate = SearchCritera.OnlyCreatedOrUpdated;
}
else if (rbOnlyOld.checked == true)
{
searchObject.CreatedOrUpdatedOnValidDate = SearchCritera.OnlyOld;
}
else
{
searchObject.CreatedOrUpdatedOnValidDate = SearchCritera.AllChanged;
}
And I really dislike the above. It feels clumsy and unclean. I would like a value to be returned by the radio button itself (named rbOnlyCreatedOrUpdatedOnValidDate, i.e the GroupName).
Is it possible or is the above the correct way to get the value?
If you use a RadioButtonList control, you can access the selected radiobuttons value by using IdOfRadioButtonList.SelectedValue.
This will give you a string, so you will still have to do the conversion to one of the SearchCriteria class' properties yourself...
If it makes you feel any better, you can make it into a ternary operator, like so:
searchObject.CreatedOrUpdatedOnValidDate =
rbOnlyCreatedorUpdated.Checked ? SearchCritera.OnlyCreatedOrUpdated :
rbOnlyOld.checked ? SearchCritera.OnlyOld :
SearchCritera.AllChanged;

Code Behind Manipulations/Giving custom names to Variables/Combining names

So my problem goes, like, I want to have dynamic seted names, something like this:
for(int i=0;i<5;i++)
{
CheckBox i = new CheckBox();
}
2 bad it does not work ^^
Is it even possible to get values like that from variables, and transform them into variables names? Or should I just set thoes things staticly?
Ow yeah and afeter setting something like that I would like to do something like this to set the content, bascily it will go in the same loop
fe. i want to have 5 buttons and i have made 5 strings in the AppResources, their names goes like : a0,a1,a2,a3,a4;
So now I'm creating the buttons and using the content.
for(int i=0;i<5;i++)
{
CheckBox i = new CheckBox();
i.Content = Resources.AppResources.a+"i";
}
So, anyone can help?^^
You need to use an array:
ArrayList<CheckBox> checkboxes = new ArrayList<CheckBox>();
for(int i=0;i<5;i++)
{
checkboxes.Add(new CheckBox());
}

ASP.NET c# - RadioButtons not grouped

I have a GridView and in its OnRowDataBound callback I am adding a new cell and to that cell I'm adding a RadioButton. I've set the GroupName on the radio button, but I can still select more than one button at a time.
How can I make them grouped so that only one radio button can be selected within the group.
EDIT - This is my code:
// Add the radio button to select if this purchased address
// is set the the home address of the contact
e.Row.Cells.AddAt(0, new TableCell());
RadioButton rbSetAddress = new RadioButton();
rbSetAddress.ID = "rdSetAddress" + e.Row.Cells[2].Text;
rbSetAddress.GroupName = "SetAddress";
e.Row.Cells[0].Controls.Add(rbSetAddress);
So I'm setting a unique ID and the same GroupName. ASP.NET is NOT setting the name attribute of the input to the GroupName. So I can select as many radio buttons as I want. This is not what I want to happen. I want to be able to select one and only one in the group identified by the GroupName.
http://www.codeproject.com/KB/webforms/How_group_RButtons.aspx
http://www.developer.com/net/asp/article.php/3623096/ASPNET-Tip-Using-RadioButton-Controls-in-a-Repeater.htm
Basically, when in a repeater of any kind (which a GridView is), the names are no longer all the same from the group property as a result of the way asp.net names things inside a repeater (essentially each control gets a prefix to make it unique).
Are you sure you are giving different names to the radiobuttons on codebehind?
for (int i = 0; i < 10; i++)
{
RadioButton rb = new RadioButton();
rb.ID = "rb"+i;
rb.GroupName="rbGroup";
GridView1.Rows[i].Cells[3].Controls.Add(rb);
}

Working with listbox in winforms (c#)

How can i add 10 items to listbox dynamically to a listbox and after that I want to show the selected item value in click event of list box.
I tried like this
for(int i=1;i<10 ;i++)
{
mylistbox.Items.Add(i.ToString());
}
in click event handler
MessageBox.Show(mylistbox.SelectedValue.ToString());
it is showing error.
Whats the wrong with this?
Try using the SelectedItem property instead.
SelectedValue only works when you fill the ListBox with objects and have a ValueMember assigned.
Here is a minimal example:
var mylistbox = new ListBox {Dock = DockStyle.Fill};
mylistbox.Click += (sender, e) =>
MessageBox.Show(mylistbox.SelectedItem.ToString());
for (int i = 1; i < 10; i++)
{
mylistbox.Items.Add(i.ToString());
}
new Form {Controls = {mylistbox}}.ShowDialog();
Use the following code on the click handler
MessageBox.Show(mylistbox.Text.ToString()); //This will show the selected item as your requirement.
replace the .SelectedValue with .Text
Dmitriy has it exactly.
A good way to check what is happening when you're debugging is to highlight 'mylistbox.SelectedValue' and right-click, then select 'Add Watch'. You can then track the value of that property in the Watch window.
You can do this with any variable, and any time it shows null and you're trying to use that value you know it will throw a Null Reference Exception.
It's also good for picking up letters in a string you're trying to convert to an integer, and other similar "d'oh!" moments.

Categories

Resources