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?
Related
I am working on a homework c# winforms project and would like to add date and time in top right corner of my main form in a way that in first row I have a date written in one label, and on second row I have time written in second label.
Also I need that those stick in the top right corner if form is resized.
I don't know if it matters, but those label controls are inside panel which is top docked in form, and this panel already contains two controls that are docked left.
example of what I want
I've been playing with anchor and dock properties but I can't get it to work in a way I want.
private void GlavnaForma_Load(object sender, EventArgs e)
{
timerDateTime.Start();
lblDate.Text = DateTime.Now.ToString("dddd, dd.M.yyyy", new CultureInfo("hr-HR"));
lblTime.Text = DateTime.Now.ToString("HH:mm:ss", new CultureInfo("hr-HR"));
}
private void timerDateTime_Tick(object sender, EventArgs e)
{
lblDate.Text = DateTime.Now.ToString("dddd, dd.M.yyyy", new CultureInfo("hr-HR"));
lblTime.Text = DateTime.Now.ToString("HH:mm:ss", new CultureInfo("hr-HR"));
}
Set the anchor to Top, Right like so:
There are several ways to do this.
I would probably make the main form have a table layout panel with one column and three rows. Make the top two rows be absolutely sized and the third row have size type "percent" with a value of 100.0% to take up all remaining room. Then put a label each in the top two rows and justify the labels to the right via setting their "Dock" property to "Right".
All of this can be done in the form designer GUI. The generated code looks like the following:
this.tableLayout = new System.Windows.Forms.TableLayoutPanel();
this.labelDate = new System.Windows.Forms.Label();
this.labelTime = new System.Windows.Forms.Label();
this.tableLayout.SuspendLayout();
this.SuspendLayout();
//
// tableLayout
//
this.tableLayout.ColumnCount = 1;
this.tableLayout.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
this.tableLayout.Controls.Add(this.labelDate, 0, 0);
this.tableLayout.Controls.Add(this.labelTime, 0, 1);
this.tableLayout.Dock = System.Windows.Forms.DockStyle.Fill;
this.tableLayout.Location = new System.Drawing.Point(0, 0);
this.tableLayout.Name = "tableLayout";
this.tableLayout.RowCount = 3;
this.tableLayout.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 24F));
this.tableLayout.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 24F));
this.tableLayout.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
this.tableLayout.Size = new System.Drawing.Size(800, 450);
this.tableLayout.TabIndex = 0;
//
// labelDate
//
this.labelDate.AutoSize = true;
this.labelDate.Dock = System.Windows.Forms.DockStyle.Right;
this.labelDate.Location = new System.Drawing.Point(742, 0);
this.labelDate.Name = "labelDate";
this.labelDate.Size = new System.Drawing.Size(55, 24);
this.labelDate.TabIndex = 0;
this.labelDate.Text = "26-8-2019";
//
// labelTime
//
this.labelTime.AutoSize = true;
this.labelTime.Dock = System.Windows.Forms.DockStyle.Right;
this.labelTime.Location = new System.Drawing.Point(748, 24);
this.labelTime.Name = "labelTime";
this.labelTime.Size = new System.Drawing.Size(49, 24);
this.labelTime.TabIndex = 1;
this.labelTime.Text = "19:59:58";
Add whatever further content you want to the third row. Maybe add a panel to that row docked to "Fill"
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.
i'm making a simple login-register program, i save usernames and passwords in lists.
i'm trying to show all the usernames under each other and that it wil repeat everytime a new user will be registered.
for some reason it only shows the last user, and that's it.
for (Int32 i = 0; i < frmLogin.reg_usernames.Count; i++ )
{
TextBox lbl = new TextBox { Location = new Point(15, 30), BorderStyle = BorderStyle.Fixed3D, BackColor = Color.AliceBlue, Font = new Font(Font.FontFamily.Name, 9), ScrollBars = ScrollBars.Vertical };
this.Controls.Add(lbl);
lbl.Text = frmLogin.reg_usernames[i];
}
You need to move the boxes down as you go:
TextBox lbl = new TextBox { Location = new Point(15, 30 * i), BorderStyle ....
Note that your form would need to be large enough to see them all, as well. You may need to set your height appropriately, or place the text boxes inside of a container which could scroll, instead of directly on the form itself.
I'm working with .NET forms in Visual C#.
I've created a label dynamically, which shows upon a button click. This all works fine; what I'm trying to do is position it so that the element is at the centre of the form. Normally, I'd just set it to half the form size, minus half the element size, but of course this won't work as I am setting the text programmatically also.
My code for the label is as follows:
if(part == 1){
theLabel.Text = "Choose a name for your character!";
}
theLabel.ForeColor = Color.DarkGray;
theLabel.Font = new Font("Arial", 14, FontStyle.Bold);
theLabel.Location = new Point();
I've tried many things here, but I just cannot think of a way. I've tried int[] sizes = (int)theLabel.Size and various other but I just cannot get this to work. Is there another way to line this element to the middle?
If I was you I'd do it this way.
Label theLabel;
private void button1_Click(object sender, EventArgs e)
{
theLabel = new Label();
theLabel.AutoSize = true;
theLabel.BackColor = Color.Red;
theLabel.TextAlign = ContentAlignment.MiddleCenter;
theLabel.Text = "Choose a name for your character!";
this.Controls.Add(this.theLabel);
theLabel.Left = (this.ClientRectangle.Width - theLabel.Width) / 2;
//theLabel.ForeColor = Color.Black;
theLabel.Top = 25;
theLabel.Show();
}
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.