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

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;

Related

C# giving radio buttons and panels a value

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

Edit listview item based on click_event c#

i have created a listview
ListView ListView1 = new ListView();
ListView1.Location = new System.Drawing.Point(12, 12);
ListView1.Name = "ListView1";
ListView1.Size = new System.Drawing.Size(280, 300);
ListView1.BackColor = System.Drawing.Color.White;
ListView1.ForeColor = System.Drawing.Color.Black;
ListView1.View = View.Details;
ListView1.GridLines = true;
ListView1.FullRowSelect = true;
ListView1.Columns.Add("ProductName", 100);
ListView1.Columns.Add("Quantity", 100);
ListView1.Columns.Add("Price", 100);
and i add items to it using the following code :
b.Click += (s, e) => {
string[] arr = new string[4];
ListViewItem itm;
arr[0] = b.Text;
arr[1] = x.ToString();
arr[2] = price;
itm = new ListViewItem(arr);
ListView1.Items.Add(itm);
x++;
};
b is an auto generated button, what i want to achieve is simple the variable x will increment with value 1 everytime i click on the button b, and x indicate the Quantity.
What i want:
when ever i click on the button b, the Quantity will change for the current item that has the Column["Productname"]=b.Text
What im getting:
the Quantity change but the item gets reinserted so i want to check if the item does exist first(based on Column["Productname"]) and if it does w the Quantity gets incremented by 1.
image_to_help_understand
More details: im sorry if this is getting too long, but im simply having a number of auto generated buttons and every button represents a product, when the user click on a product it gets added to the list ( to buy it later) and if the client clicks the same product n times, the Quantity should became Quantity=n without the item being added another time. thanks all and sorry for the long post.
Poking values into the UI is not a good design. I have these changes from my previous comments. Use a BindingList, public class Product : INotifyPropertyChanged, and replace the ListView with a DataGridView. This just makes life a whole lot easier and the code so simple. Creating ListBoxItems on your own is NOT a good way of doing things.
BTW: to find a product you just use a LINQ query on the ProductCollection. No searching the UI.

How can I iterate through selected ComboBoxes controls with a loop in C#?

I am working in a simple C# form. I have a total of 20 ComboBoxes . 10 ComboBoxes will contain similar data type and will have very similar name (CB1, CB2, CB3, ... CB10). Each ComboBox was loaded with a list of 5 elements A,B,C,D,E (by this I meant that I added those values to each of the 10 "CB" ComboBoxes). My intend is that the user can have the ability to select items (one out of A,B,C,D,E) from either 1 combobox, or 2 comboboxes, . . . ., or all of them (10 comboboxes).
I wish to store the data from the ComboBoxes where an item was selected in a list or array. For that I would like to iterate through the 10 ComboBoxes (the comboboxes which names are CB1, CB2, CB3, ..., CB10), check if the an item was selected in the combobox, and store the value selected in the ComboBox into a list (in the code below the list is called symbols). At the end the length of my symbols list (number of items) will depend on how many ComboBoxes the user selected from. Here is the code I am using:
List<string> symbols = new List<string>();
for (int i = 1; i <= 10; i++)
{
var my_comboBox = this.Controls["CB" + i.ToString()];
if (null != my_comboBox.SelectedItem)
{ symbols.add(my_comboBox.Text); }
}
when I run the code I get the following error.
Object reference not set to an instance of an object.
Could anyone please explain what I am doing wrong? I got the code from another question that was posted and answered, below is the link to that question. Thanks in advance.
How can I iterate all ComboBoxes controls with a loop in C#?
I also tried the other alternative proposed on the answers to the questions cited. But it did not work. It does not go through the foreach loop (no error is shown though). Here is the code for option 2.
List<string> symbols = new List<string>();
var comboBoxes = this.Controls.OfType<ComboBox>().Where(x => x.Name.StartsWith("CB"));
foreach (var cmbBox in comboBoxes)
{
if (null != my_comboBox.SelectedItem)
{ symbols.add(my_comboBox.Text); }
}
Again if anyone can please provide me with ideas to what I am doing wrong that would be very nice. Thanks in advance once more.
The ComboBoxes belong to the form as shown in the picture below. In there the ComboBoxes are called component_CB1, component_CB2, component_CB3, ..., component_CB10 (I changed the name of the ComboBoxes in the question to CB to make it easier to understand).
screenshot of my solution explorer
Thanks to everyone who contributed to find the answer to this problem. Please read the comments to identify the contributors.
The answer is that you can iterate through selected ComboBoxes in C#. However for this to work you need to make sure you know to what container control your ComboBoxes are added.
To know to container control your ComboBoxes are added to, go to View → Other Windows → Document Outline. You can see if those controls are directly children of the form or they are children of another container control.
If the ComboBoxes are added to your form directly, then here there are two alternatives to iterate through the ComboBoxes:
ALTERNATIVE 1: (ComboBoxes added to the form directly)
List<string> symbols = new List<string>();
for (int i = 1; i <= 10; i++)
{ //CB is the begining of the name of the comboboxes CB1, CB2, ... CB10
var my_comboBox = this.Controls["CB" + i.ToString()];
if (null != my_comboBox.SelectedItem)
{ symbols.add(my_comboBox.Text); }
}
ALTERNATIVE 2: (ComboBoxes added to the form directly)
List<string> symbols = new List<string>();
//CB is the begining of the name of the comboboxes CB1, CB2, ... CB10
var comboBoxes = this.Controls.OfType<ComboBox>().Where(x=>x.Name.StartsWith("CB"));
foreach (var cmbBox in comboBoxes)
{
if (null != my_comboBox.SelectedItem)
{ symbols.add(my_comboBox.Text); }
}
If the ComboBoxes are added to a different control container (e.g. a tab in a TabControl as in my initial case), you must specify the control container name rather than "this". Here are two alternatives taking as example the ComboBoxes Cb1, CB2, CB3, ..., CB10 that are added to a tab called tab1.
ALTERNATIVE 1: (ComboBoxes added to a tab of a TabControl)
List<string> symbols = new List<string>();
for (int i = 1; i <= 10; i++)
{//CB is the begining of the name of the comboboxes CB1, CB2, ... CB10
var my_comboBox = tab1.Controls["CB" + i.ToString()];
if (null != my_comboBox.SelectedItem)
{ symbols.add(my_comboBox.Text); }
}
ALTERNATIVE 2: (ComboBoxes added to a tab of a TabControl)
List<string> symbols = new List<string>();
//CB is the begining of the name of the comboboxes CB1, CB2, ... CB10
var comboBoxes = tab1.Controls.OfType<ComboBox>().Where(x
=>x.Name.StartsWith("CB"));
foreach (var cmbBox in comboBoxes)
{
if (null != my_comboBox.SelectedItem)
{ symbols.add(my_comboBox.Text); }
}
Thanks for the help again. I hope this can be useful for others.

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