Can't take an instance using Controls.Add - c#

I'm triyng to create buttons programmatically. I don't want to use Button btn = new Button();.. btn.height=.... control.Add(btn);. The code I'm using can add the buttons to the form but I can't take an instance so I can't create button.click event. Can anybody help me to solve it. The code I'm using below thanks.
int k = 0;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
Controls.Add(
new Button()
{
Top = 50 + (50 * i),
Left = 50 + (50 * j),
Width = 50, Height = 50,
Text = (++k).ToString()
});
}
}

If you don't want to create a button, and subscribe directly to the event (i don't know why is this important), you can do this:
private void Method()
{
int k = 0;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
Controls.Add(new Button() { Top = 50 + (50 * i), Left = 50 + (50 * j), Width = 50, Height = 50, Text = (++k).ToString() });
}
}
Controls.OfType<Button>().ToList().ForEach(x => x.Click += Button_Click);
}
private void Button_Click(object sender, System.EventArgs e)
{
MessageBox.Show((sender as Button).Text);
}

Related

which button is pressed in dynamically created button 2d array

I have a 2d array of LEDButton : Button.
I want to find out the index [x,y] of each buttons the user clicks.
I am new to Windows Forms and not used to working outside of a console so these GUI objects are very unfamiliar to me.
private void Form1_Load(object sender, EventArgs e)
{
LEDButton[,] leds = new LEDButton[11, 11];
for (int x = 0; x < leds.GetUpperBound(0); x++)
{
listBox1.Items.Add("x = " + x);
for (int y = 0; y < leds.GetUpperBound(1); y++)
{
leds[x, y] = new LEDButton()
{
Name = String.Format("Button{0}{1}", x, y),
TabIndex = 40 * x + y,
Location = new Point(40 * y + 50, 40 * x + 50)
};
leds[x, y].pointx = x;
leds[x, y].pointy = y;
}
}
// add buttons to controls
for (int x = 0; x < leds.GetUpperBound(0); x++)
{
for (int y = 0; y < leds.GetUpperBound(1); y++)
{
Controls.Add(leds[x, y]);
leds[x, y].Click += Form1_Click;
}
}
public class LEDButton : Button
{
public const int LEDWidth = 20;
public const int LEDHeight = 20;
public int pointx = 0;
public int pointy = 0;
public LEDButton()
{
BackColor = Color.FromArgb(0, 64, 0);
ForeColor = Color.Black;
FlatStyle = FlatStyle.Flat;
Size = new Size(LEDWidth, LEDHeight);
UseVisualStyleBackColor = false;
this.Click += LEDButton_Click; //throws error
}
}
I think I found my answer with the help of Lars.
Code should be
private void Form1_Click(object? sender, EventArgs e)
{
LEDButton btn = sender as LEDButton;
listBox2.Items.Add(btn.Name);
}

How can I set click event for 3-d object in wpf?

I want to set Click event for multiple spheres in wpf page. The spheres are like a cube. Then I want to change the color of each sphere by clicking on it. Here is the method to create my model.
private void DefineModel(Model3DGroup model_group)
{
int myRow = enteredRow / 2;
int myCol = enteredColumn / 2;
int myDep = enteredScale / 2;
// Make spheres centered at (+/-1, 0, 0).
MeshGeometry3D mesh1 = new MeshGeometry3D();
for (int i = 0; i < enteredRow; i++)
{
for (int j = 0; j < enteredColumn; j++)
{
for (int k = 0; k < enteredScale; k++)
{
AddSmoothSphere(mesh1, new Point3D(-k, -j, -i), 0.25, 10, 20);
}
}
}
SolidColorBrush brush1 = Brushes.Green;
DiffuseMaterial material1 = new DiffuseMaterial(brush1);
GeometryModel3D model1 = new GeometryModel3D(mesh1, material1);
model_group.Children.Add(model1);
}

How can I make n*n buttons using an array at runtime in C#?

It takes a number n from the user and makes n*n buttons for it on the form.
When I run the code, only the first row appears on form, instead of all n rows.
private void button1_Click(object sender, EventArgs e)
{
int n = Convert.ToInt32(textBox1.Text.ToString());
Button[,] v = new Button[n, n];
for (int i = 0; i < n; i++)
{
int top = 60;
int left = 160;
int width = 25;
for (int j = 0; j < n; j++)
{
Button c= new Button();
c.Top =top ;
c.Left= left;
c.Width = width;
v[i, j] = c;
left += c.Width + 2;
}
top += 2;
left = 160;
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
this.Controls.Add(v[i, j]);
}
}
}
You are resetting the top variable at each loop, your buttons are all in the same top position. Move the initialization of the top variable outside the first loop
private void button1_Click(object sender, EventArgs e)
{
int top = 60;
int n = Convert.ToInt32(textBox1.Text.ToString());
Button[,] v = new Button[n, n];
for (int i = 0; i < n; i++)
{
// int top = 60;
......
However, you are incrementing the top only by 2 pixel. This is not enough to avoid covering one row of button with the next row. You need to increment top by at least 25 pixels at the end of the inner loop.
....
top += 25;
left = 160;
}
You can also use your for loops to set the top and left property values (more than one variable can be defined and incremented in a for loop).
For each row, we increase the top value by the height + padding (padding is the gap between buttons), and for each col, we increase the left by width + padding.
We can also add each button to the controls collection as we create it rather than adding it to an array and then iterating again over that array, and we can add some input validation to the textbox.Text that we're assuming is a positive number, presenting the user with a message box if they entered something invalid.
Given this, we can simplify the code to something like:
private void button1_Click(object sender, EventArgs e)
{
int count;
// Validate input
if (!int.TryParse(textBox1.Text, out count) || count < 0)
{
MessageBox.Show("Please enter a positive whole number");
return;
}
// remove old buttons (except this one)
var otherButtons = Controls.OfType<Button>().Where(b => b != button1).ToList();
foreach (Button b in otherButtons)
{
Controls.Remove(b);
}
// Add button controls to form with these size and spacing values (modify as desired)
var width = 25;
var height = 25;
var padding = 2;
// For each row, increment the top by the height plus padding
for (int row = 0, top = padding; row < count; row++, top += height + padding)
{
// For each column, increment the left by the width plus padding
for (int col = 0, left = padding; col < count; col++, left += width + padding)
{
// Add our new button
Controls.Add(new Button
{
Top = top,
Left = left,
Width = width,
Height = height,
});
}
}
}

Button next to the users in a loop

I got a form with 7 users on it among each other. But I want to set 7 button next to it in a loop. Right now I've got this.
int x = 12;
int y = 30;
foreach (details dets1 in detailsList)
{
var label = new Label();
label.AutoSize = true;
label.Location = new Point(x, y);
label.Name = dets1.fname;
label.Text = dets1.fname;
this.Controls.Add(label);
y += label.Height + 5;
}
for (int i = 0; i < 7 ; i++)
{
Button button = new Button();
button.Location = new Point(200, 30);
button.Text = "Off";
button.Tag = i;
button.BackColor = Color.Red;
this.Controls.Add(button);
button.Click += button_Click;
}
Your problem is in the Location: you put all the buttons on the same place
for (int i = 0; i < 7 ; i++) {
...
button.Location = new Point(200, 30);
...
}
Let's organaize all the buttons vertically:
const int shift = 50;
for (int i = 0; i < 7 ; i++) {
...
button.Location = new Point(200, 30 + shift * i);
...
}
Or horizontally:
const int shift = 90;
for (int i = 0; i < 7 ; i++) {
...
button.Location = new Point(200 + shift * i, 30);
...
}

Creating a collection of Controls with a loop

So i'm basically attempting to create a grid out of pictureboxes through a for loop using the following code:
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i <= 5; i++)
{
for (int j = 0; j <= 5; j++)
{
PictureBox tile = new PictureBox() ;
tile.Size = new Size(49, 49);
tile.BackColor = Color.Firebrick;
tile.Location = new Point((100 + (50 * i)), (100 + (50 * j)));
Debug.WriteLine("PB created with index ["+i+","+j+"]");
}
}
}
But during runtime, the form appears blank, with no pictureboxes generated at all.
What did I do wrong?
You're not adding the control you just created to the form's Controls collection.
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i <= 5; i++)
{
for (int j = 0; j <= 5; j++)
{
PictureBox tile = new PictureBox() ;
tile.Size = new Size(49, 49);
tile.BackColor = Color.Firebrick;
tile.Location = new Point((100 + (50 * i)), (100 + (50 * j)));
Debug.WriteLine("PB created with index ["+i+","+j+"]");
// The control needs to be added to the form's Controls collection
Controls.Add(tile);
}
}
}

Categories

Resources