dynamically adding controls under loop in C# - c#

I am developing a windows application where I want to create some controls dynamically inside a loop.
The code I am trying is
private Label newLabel = new Label();
private int txtBoxStartPosition = 100;
private int txtBoxStartPositionV = 25;
for (int i = 0; i < 7; i++)
{
newLabel.Location = new System.Drawing.Point(txtBoxStartPosition, txtBoxStartPositionV);
newLabel.Size = new System.Drawing.Size(70, 40);
newLabel.Text = i.ToString();
panel1.Controls.Add(newLabel);
txtBoxStartPositionV += 30;
}
This code is generating only one Label with text 7 but I want to create 8 Lables with their respective texts, how can I do this?

In your loop you are essentially updating properties of the very same Label. If you want create a new one on each step, move creation of the object inside the loop:
private Label newLabel;
for (int i = 0; i < 7; i++)
{
newLabel = new Label();
...
By the way if you want 8 labels - your for should iterate 8 times, not 7, as it does now:
for (int i = 0; i < 8; i++)

Try this:
private int txtBoxStartPosition = 100;
private int txtBoxStartPositionV = 25;
for (int i = 0; i < 7; i++)
{
newLabel = new Label();
newLabel.Location = new System.Drawing.Point(txtBoxStartPosition, txtBoxStartPositionV);
newLabel.Size = new System.Drawing.Size(70, 40);
newLabel.Text = i.ToString();
panel1.Controls.Add(newLabel);
txtBoxStartPositionV += 30;
}

You need to put the line private Label newLabel = new Label(); in the for loop.
private int txtBoxStartPosition = 100;
private int txtBoxStartPositionV = 25;
for (int i = 0; i < 7; i++)
{
Label newLabel = new Label();
newLabel.Location = new System.Drawing.Point(txtBoxStartPosition, txtBoxStartPositionV);
newLabel.Size = new System.Drawing.Size(70, 40);
newLabel.Text = i.ToString();
panel1.Controls.Add(newLabel);
txtBoxStartPositionV += 30;
}

Related

Why label overLapping on text box in win form c# while generating dynamic text box and label?

try
{
int txtno = 10;
int Textbox_pointY = 15;
int label_pointY = 15;
int label_pointX = 10;
int Textbox_pointX = 75;
panel1.Controls.Clear();
for (int i = 0; i < txtno; i++)
{ //Lable creation
Label lbl = new Label();
panel1.Controls.Add(lbl);
lbl.Text = "Test_" + i;
lbl.Location = new Point(label_pointX, label_pointY);
label_pointY += 22;
//Text box creating
TextBox a = new TextBox();
panel1.Controls.Add(a);
a.Text = (i + 1).ToString();
a.Location = new Point(Textbox_pointX, Textbox_pointY);
//panel1.Show();
Textbox_pointY += 22;
//label_pointY += 5;
}
}
catch (Exception)
{
MessageBox.Show(e.ToString());
}
When dynamically generate label and enter code heretext box in winform c# Label overlaps over textbox .I need to keep text box closely to label text .I have added my code here .
Set AutoSize to false and specify labels' Width explicitly:
int txtno = 10;
int label_pointY = 15;
int label_pointX = 10;
int Textbox_pointX = 75;
// Don't do this: it just removes conrols from the panel,
// but does't free resources (and you have resource leakage)
// panel1.Controls.Clear();
// If you want to get rid of all controls on the panel1 (i.e. dispose them)
// do it like this:
for (int i = panel1.Controls.Count - 1; i >= 0; --i)
panel1.Controls[i].Dispose();
for (int i = 0; i < txtno; i++) {
Label lbl = new Label() {
Parent = panel1,
Text = "Test_" + i,
Location = new Point(label_pointX, label_pointY),
AutoSize = false,
Width = Textbox_pointX - label_pointX,
};
TextBox box = new TextBox() {
Parent = panel1,
Text = (i + 1).ToString(),
Location = new Point(Textbox_pointX, label_pointY)
};
label_pointY += Math.Max(box.Height, lbl.Height);
}

How do i access these arrays so that i can multiply inputted values in the diagonal?

If I inputted 5, it will generate a square matrix by 5 (5x5)
How do I multiply 1, 7, 13, 19, 25?
Is there any applicable algorithm for my code so I could multiply the diagonals or do i need to rewrite a new one?
public partial class Form1 : Form {
public int e = 0;
int Row = 0;
int Column = 0;
int YAxisPosition = 0;
int XAxisPosition = 0;
int Counter = 0;
int PositionalValue = 0;
TextBox[] MyTextBoxDimA = new TextBox[999999];
TextBox tbRow = new TextBox();
Button MyButton = new Button();
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
AutoScroll = true;
WindowState = System.Windows.Forms.FormWindowState.Maximized;
//GENERATING THE SIZE BUTTON
tbRow = new TextBox();
tbRow.Text = "5";
tbRow.Size = new Size(100, 10);
tbRow.Location = new Point(0, 0);
Controls.Add(tbRow);
//GENERATE MATRIX BUTTON
MyButton = new Button();
MyButton.Text = "GENERATE MATRIX";
MyButton.Size = new Size(200, 25);
MyButton.Click += new EventHandler(MyButton_Click);
MyButton.Location = new Point(0, 30);
Controls.Add(MyButton);
}
public void MyButton_Click(object sender, EventArgs ee) {
//CODE FOR GENERATING MATRIX A
e = 1;
PositionalValue = 1;
Counter = 1;
//POSITION
YAxisPosition = 60;
XAxisPosition = 0;
Row = Convert.ToInt32(tbRow.Text);
Column = Convert.ToInt32(tbRow.Text);
while (Row >= e) {
while (Column >= Counter) {
MyTextBoxDimA[PositionalValue] = new TextBox();
MyTextBoxDimA[PositionalValue].Location =
new Point(XAxisPosition, YAxisPosition); //coordinates (start)
MyTextBoxDimA[PositionalValue].Size = new Size(70, 10);
MyTextBoxDimA[PositionalValue].Text = Convert.ToString(PositionalValue);
Controls.Add(MyTextBoxDimA[PositionalValue]);
XAxisPosition = XAxisPosition + 100;
PositionalValue++;
Counter++;
}
YAxisPosition = YAxisPosition + 50;
Counter = 1;
e++;
XAxisPosition = 0;
}
}
}
If you are looking for an algorithm:
static void Main(string[] args)
{
int n = 5;
int ans = 1;
int current = 1;
for (int i = 1; i <= n; i++)
{
ans = ans * current;
current += n + 1;
}
Console.WriteLine(ans);
}
Just loop from zero to the size of the matrix. The index will be used to get each diagonal value (this code assumes that each textbox contains an integer and is not blank):
int matrixSize = 5;
int product = 1;
for (int i = 0; i < matrixSize; i++)
{
product *= int.Parse(MyTextBoxDimA[i,i].Text);
}

Intersecting rectangles from array

private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < pala.cantLetras; i++)
{ guiones[i] = new Label();
guiones[i].Text = "_";
guiones[i].Font = new Font("Berlin Sans FB Demi", 25);
guiones[i].Size = new Size(18, 37);
guiones[i].Name = "guion" + i;
guiones[i].ForeColor = Color.Black;
guiones[i].BackColor = Color.Transparent;
guiones[i].Location = new Point(x, 341);
this.Controls.Add(guiones[i]);
recguion[i] = guiones[i].Bounds;
recguion[i] = new Rectangle();
//recguion[i].Location = new Point(x, 341);
x = x + 50;
}
for (int i = 0; i < pala.cantLetras; i++)
{
labels[i] = new Label();
labels[i].Size = new Size(25, 55);
labels[i].Name = "label" + i;
labels[i].Text = pala.palabra[i].ToString();
labels[i].Font = new Font("Berlin Sans FB Demi", 20);
labels[i].ForeColor = Color.Black;
labels[i].BackColor = Color.Transparent;
labels[i].Location = new Point(y, 165);
this.Controls.Add(labels[i]);
posRandom[i] = y;
reclabel[i] = labels[i].Bounds;
reclabel[i] = new Rectangle();
// reclabel[i].Location = new Point(y, 165);
y = y + 40;
}
}
I need to know when reclabel[] intersects the recguion[] corresponding to that number.
Ex: reclabel[1] Intersects recguion[1] but only that one, if it intersects another it has to say that it's wrong.
The rectangles have inside(or that's what I a'm trying) labels[] and guiones[]
This is what I have tryied but it doesnt work.
private void intersecta()
{
int cont = 0;
for (int i = 0; i < pala.cantLetras; i++)
{
for (int j = 0; j < pala.cantLetras; j++)
{
if (i==j)
{
Rectangle intersect = Rectangle.Intersect(reclabel[i], recguion[j]);
if (intersect != Rectangle.Empty)
{
MessageBox.Show("Intersection!");
cont++;
}
}
if (cont != 0)
{
i = pala.cantLetras - 1;
j = pala.cantLetras - 1;
}
}
}
}
Thank you!
There is no need for a nested loop. Just loop through one array and check both rectangles at that index with .IntersectsWith. My apologies if there are any syntax errors, I don't have access to Visual Studio at the moment.
For(int i = 0; i < Array1.Length; i++)
{
if(Array1[i].IntersectsWith(Array2[i]))
{
//Intersected
}
}
But also, as Andrew pointed out, you have a serious problem here:
reclabel[i] = new Rectangle();
You are just overwriting all your data with a new instance (of a different type!).

C# Picture Box Loop

I have written loop for picture box, it looks like this:
void labelAdder()
{
List<Label> labels = new List<Label>();
List<TextBox> texboxex = new List<TextBox>();
List<PictureBox> pictureBoxes = new List<PictureBox>();
for (int i = 0; i < args.Length - 1; i++)
{
equals++;
var temp = new TextBox();
var temp2 = new PictureBox();
int x = 10;
int y = xD * equals;
temp.Location = new Point(x, y);
temp2.Location = new Point(x + 100, y);
temp2.Image = global::Xbox360_Complex_Checker.Properties.Resources.button_offline;
temp.Text = args[i];
temp2.Text = status[i];
this.Controls.Add(temp);
this.Controls.Add(temp2);
temp.Show();
temp2.Show();
texboxex.Add(temp);
pictureBoxes.Add(temp2);
}
}
My problem is that PictureBox is loading only next to first textBox, it should load next to all textBoxes. I don't know why it's not working. I tried this with labels, and labels were loading properly.

How Can I Add multipe buttons on c# dynamically using point class?

Hello I try to add multiple Buttons on a panel on form and next to each other but instead it put them above each other.
I am using the following function.
the Code:
private void CreatBtn()
{
Point[] p = new Point[6];
string log = "";
Form2 frm2 = new Form2();
Button[] btn = new Button[6];
for (int i = 0; i < btn.GetLength(0); i++)
{
btn[i] = new Button();
btn[i].Height = 65;
btn[i].Width = 80;
p[i] = new Point();
p[i].X = i * 83;
p[i].Y =0;
log +=p.ToString() +"\n";
btn[i].PointToClient(p[i]);
btn[i].Show();
}
panel1.Controls.AddRange(btn);
}
Add a Left value to your buttons
btn[i] = new Button();
btn[i].Height = 65;
btn[i].Width = 80;
btn[i].Left = i * 83; //Now they'll be next to each other.
You may also consider using the FlowLayoutPanel.
Use flowLayoutPanel in the panel of it'll but the next to each other until the button reach the end of it, then it will make an new raw
Point[] p = new Point[6];
string log = "";
Button[] btn = new Button[6];
for (int i = 0; i < btn.GetLength(0); i++)
{
btn[i] = new Button();
btn[i].Height = 65;
btn[i].Width = 80;
p[i] = new Point();
p[i].X = i * 83;
p[i].Y = 0;
log += p.ToString() + "\n";
btn[i].PointToClient(p[i]);
btn[i].Show();
}
FlowLayoutPanel pan = new FlowLayoutPanel();
pan.Width=500;//width of all buttons
pan.Height = 100;
pan.Controls.AddRange(btn);
panel1.Controls.Add(pan);

Categories

Resources