Is there a HTML-RadioButton value equivalent in WinForms? - c#

I am trying to create a series of RadioButtons on a WinForm. It works OK, but in the Click-event I want to capture the product ID and do stuff with that.
I am used to HTML-elements, and assigning data to a label and value for the RadioButton. With WinForms I cannot see the equivalent to the value attribute.
Any good advice on how to pass onn the product ID to the RadioButton Change-event?
var products = new Business.Products().GetAll();
if (!products.Any())
GrpCategories.Controls.Clear();
int y = 2;
int x = 2;
foreach (var product in products)
{
var btn = new RadioButton();
btn.Width = 100;
btn.Height = 20;
if (y >= GrpCategories.Height - btn.Height - 10)
{
x += btn.Width + 2;
y = 2;
}
y += btn.Height + 2;
btn.Appearance = Appearance.Button;
btn.Text = product.Name;
btn.Name = "BtnProduct_" + product.ID;
btn.Location = new Point(x, y);
GrpCategories.Controls.Add(btn);
}

Simply use the Tag-property of the RadioButton. This property can store any .NET object.

There is nothing like RadioButtonList control in winform or else it would have solved your problem like nothing.
But,then also You can group radio buttons by adding them inside a container such as a Panel control, a GroupBox control, or a form.
Drag a group panel inside your form and then add your radio buttons, this will group all the radio buttons inside that group.. as is done here.

Related

Dynamic button to fit the area size

I am trying to create a system that has X amount of buttons, like a POS system. Number of buttons will be different based on the number of items user input. My question is if I should create it dynamically like the following:
private void createButton(int numOfBtn)
{
for (int i = 0; i < numOfBtn; i++)
{
Button btn = new Button();
btn.Name = "button";
btn.Text = "button";
btn.ForeColor = Color.White;
btn.BackColor = Color.Green;
btn.Font = new Font("Serif", 24, FontStyle.Bold);
btn.Width = 170;
btn.Height = 80;
btn.TextAlign = ContentAlignment.MiddleCenter;
btn.Margin = new Padding(5);
}
}
Should create buttons one by one on the designer, dynamically like the above code or if there is a better way of doing so?
Also, currently I am having the windows set to Maximum size when it launches, so when I am creating the buttons, how can I tell if the number of button has max out the space(flowlayout).
Edit: Is it better to create all the buttons from the designer first and assign value of it after? but this way there will always have a maximum amount of buttons, let says if I create 20 buttons from the designer, at max I can only assign twenty items.. what would the better way of doing such task?

How can i add several labels to a list in a for loop?

I want to be able to do something like this so that i can just add label then label1 label 2 label3 etc and i cant create an array of new labels or anything since my labels have an image attached to them and have to keep a certain position as well. So making a new label that has some text as a label that wont show up anywhere in particular on my form isn't much use.
List<Label> labels = new List<Label>();
for (int i = 0; i < 10; i++)
{
labels.Add(label + (i.ToString()));
}
You can add Labels to list from Form by name like this :
List<Label> labels = new List<Label>();
for (int i = 0; i < 10; i++)
{
var label =
this.Controls.OfType<Label>().Where(lb => lb.Name == "label" + i).FirstOrDefault();
if (label != null)
labels.Add(label);
}
Here is a Code Sample:
You need to add the labels to the Controls collection of the tab page:
Page2.Controls.Add(CustomLabel[CustomLabel.Count - 1] as Control);
List<Label> customLabels = new List<Label>();
foreach (string ValutaCustomScelta in Properties.Settings.Default.ValuteCustom)
{
Label label = new Label();
label.Location = new System.Drawing.Point(317, 119 + customLabels.Count*26);
label.Parent = Page2;
label.Name = "label" + ValutaCustomScelta;
label.Text = ValutaCustomScelta;
label.Size = new System.Drawing.Size(77, 21);
customLabels.Add(label);
Page2.Controls.Add(label);
}
Have you added the labels by code or by design?
If you added them by code just add them then to the list.
If you added them by design there are two options. They are all on the same container (panel, form, groupbox...) or they are not.
If they are you can:
for each(control ctr in $yourcontainer.controls)
{
if(ctr is Label)
{
labels.add(ctr);
}
}
If they are not in the same container you can add them by hardcode
labels.add(%label1name);
labels.add(%label2name);
labels.add(%label3name);
labels.add(%label4name);
But if they are too much, you can run the code before for each one of the panels

Create dynamic buttons in a grid layout - Create a magic square UI

I'm supposed to create a magic square in 2D using Windows Forms Application. It should look like this:
However, the user should be able to decide the size of the square (3x3, 5x5, 7x7, etc). I already wrote the code in a Console Application, but I don't know how to add the 2D graphics.
Somebody already asked this question (How do I put my result into a GUI?), and one of the answers was to use DataGridView, but I'm not sure if that's what I'm looking for, since I can't make it look like the picture.
Any ideas or advice?
You can use a TableLayoutPanel and add buttons to panel dynamically.
If you don't need interaction with buttons, you can add Label instead.
Create square dynamically:
public void CreateSquare(int size)
{
//Remove previously created controls and free resources
foreach (Control item in this.Controls)
{
this.Controls.Remove(item);
item.Dispose();
}
//Create TableLayoutPanel
var panel = new TableLayoutPanel();
panel.RowCount = size;
panel.ColumnCount = size;
panel.BackColor = Color.Black;
//Set the equal size for columns and rows
for (int i = 0; i < size; i++)
{
var percent = 100f / (float)size;
panel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, percent));
panel.RowStyles.Add(new RowStyle(SizeType.Percent, percent));
}
//Add buttons, if you have your desired output in an array
//you can set the text of buttons from your array
for (var i = 0; i < size; i++)
{
for (var j = 0; j < size; j++)
{
var button = new Button();
button.BackColor = Color.Lime;
button.Font = new Font(button.Font.FontFamily, 20, FontStyle.Bold);
button.FlatStyle = FlatStyle.Flat;
//you can set the text of buttons from your array
//For example button.Text = array[i,j].ToString();
button.Text = string.Format("{0}", (i) * size + j + 1);
button.Name = string.Format("Button{0}", button.Text);
button.Dock = DockStyle.Fill;
//If you need interaction with buttons
button.Click += b_Click;
panel.Controls.Add(button, j, i);
}
}
panel.Dock = DockStyle.Fill;
this.Controls.Add(panel);
}
If you need interaction with buttons
void button_Click(object sender, EventArgs e)
{
var button = (Button)sender;
//Instead put your logic here
MessageBox.Show(string.Format("You clicked {0}", button.Text));
}
As an example, you can call
CreateSquare(3);
Screenshot:
You can create a Form and add a TableLayoutPanel with this property
tableLayoutPanel1.Dock = DockStyle.Fill;
tableLayoutPanel1.BackColor = Color.Gold;
and this is the result
When you create Row and Column, to fit correctly set the percentage in this way:
After this you can add a Button or Label in each square.

Dynamically Adding Checkboxes to a Windows Form Only Shows one Checkbox

I'm sorry if this seems n00bish, but I have been searching for this for a few days now. I am attempting to dynamically add checkboxes to a windows form; however, only one checkbox appears on the form. Here is my code:
for (int i = 0; i < 10; i++)
{
box = new CheckBox();
box.Tag = i.ToString();
box.Text = "a";
box.AutoSize = true;
box.Location = new Point(10, i + 10);
Main.Controls.Add(box);
}
As you can see I am adding the checkboxes via a for loop. I have tried messing with the location and enabling autosize in case they were somehow overlapping. The result is a single checkbox with text "a".
Actually you already created a CheckBox but within the same point.
CheckBox box;
for (int i = 0; i < 10; i++)
{
box = new CheckBox();
box.Tag = i.ToString();
box.Text = "a";
box.AutoSize = true;
box.Location = new Point(10, i * 50); //vertical
//box.Location = new Point(i * 50, 10); //horizontal
this.Controls.Add(box);
}
In this case with help of dynamically assign Name property how to achive checkbox.checked property , in some other action like submit button. how can i get all check box is checked and which is created in loop?
If you have an instance from every button you can make with your button or your event to make something like
CheckBox myCheckedBox = (CheckBox)sender;

How to add a ScrollBar to a GroupBox

Is there any method to add a scroll bar to a group box? My code scenario is: I have many groupboxes on a form. All the groupboxes will appear in the same place and also the height and width is fixed. I'm setting groupboxes visible true/false according to a condition.
Some groupboxes ecxeed the width and height, so I want to put this all in scrollbar. Can anyone help me regarding this?
If its an Windows application then you need to add a panel to Group box and set a property "AutoScroll"=true.
place a groupbox on your form then add this code
int btnPos = 1;
Panel pnl = new Panel();
pnl.AutoScroll = true;
pnl.Top = 15;
pnl.Left = 2;
pnl.Width = groupBox1.Width - 8;
for (int i = 0; i < 22; i++)
{
Button _btn = new Button();
_btn.Text = "lbl";
_btn.Top = btnPos;
btnPos += 23;
pnl.Controls.Add(_btn);
}
groupBox1.Controls.Add(pnl);

Categories

Resources