Im trying to create a label in runtime. the number of labels depends on the number of items of another variable and the labels do not show. The code is as follows.
int NoofItems = tillfrm.lvbasket.Items.Count;
for (int i = 0; i < NoofItems + 1; i++)
{
Label lblitems = new Label();
lblitems.Name = "lblItems" + i;
lblitems.Font = new Font ("Calibri",lblitems.Font.Size);
lblitems.Location = new Point(95, (152 + (19 * i)));
lblitems.ForeColor = System.Drawing.Color.Black;
lblitems.Show();
lblitems.AutoSize = true;
lblitems.Text = tillfrm.lvbasket.Items[0].Text;
this.Controls.Add(lblitems);
}
some help would be appreciated thanks.
You should change tillfrm.lvbasket.Items[0].Text to tillfrm.lvbasket.Items[i].Text.
And i < NoofItems + 1 to i < NoofItems, because array size is NoofItems.
Try it like this, create a function, make the array GLOBAL,
protected void myFunction()
{
int NoofItems = tillfrm.lvbasket.Items.Count;
for (int i = 0; i < NoofItems; i++)
{
Label lblitems = new Label();
lblitems.Name = "lblItems" + i;
lblitems.Font = new Font ("Calibri",lblitems.Font.Size);
lblitems.Location = new Point(95, (152 + (19 * i)));
lblitems.ForeColor = System.Drawing.Color.Black;
lblitems.Show();
lblitems.AutoSize = true;
lblitems.Text = tillfrm.lvbasket.Items[i].Text;
this.Controls.Add(lblitems);
}
}
Then call this function in Form_load() function or Page_load() function like this
protected void Form_Load(Object sender , EventArgs e)
{
myFunction();
}
Related
I have created a function to dynamically create textboxes based on the amount selected from the textbox, additionally I'm using these textboxes to display data from database. However when the user chooses for exactly five from the dropdownlist, and three textboxes was already there, instead of adding 2 more textboxes, it adds the additional 5 textboxes. What I do in order to just add the additionaly textboxes?
protected void TotalSeal_SelectedIndexChanged(object sender, EventArgs e)
{
populate();
}
public void populate()
{
int count = Convert.ToInt32(TotalSeal.SelectedItem.Value);
for (int i = 0; i < count; i++)
{
if (i < 0)
{
}
else
{
TextBox tx = new TextBox();
tx.MaxLength = 10;
tx.Width = 100;
phSealNum.Controls.Add(tx);
phSealNum.Controls.Add(new LiteralControl(" "));
ControlCache.Add(tx);
}
}
}
UPDATE
public void populate()
{
//ControlCache = new List<Control>();
//phSealNum.Controls.Clear();
int targetCount = Convert.ToInt32(TotalSeal.SelectedItem.Value);
int currentItems = phSealNum.Controls.OfType<TextBox>().Count();
int totalitems = targetCount - currentItems;
if (totalitems <= 7)
{
for (int i = 0; i < totalitems; i++)
{
TextBox tx = new TextBox();
tx.MaxLength = 10;
tx.Width = 100;
phSealNum.Controls.Add(tx);
phSealNum.Controls.Add(new LiteralControl(" "));
ControlCache.Add(tx);
}
}
else
{
lblError.Text = targetCount + " exceeds number of seals";
}
}
Using #indrit-kello logic should be like this:
protected void TotalSeal_SelectedIndexChanged(object sender, EventArgs e)
{
populate();
}
public void populate()
{
int targetCount = Convert.ToInt32(TotalSeal.SelectedItem.Value);
if(targetCount > 7)
targetCount = 7;
int currentItems = 0;//TODO
for (int i = currentItems; i < targetCount; i++)
{
TextBox tx = new TextBox();
tx.MaxLength = 10;
tx.Width = 100;
phSealNum.Controls.Add(tx);
phSealNum.Controls.Add(new LiteralControl(" "));
ControlCache.Add(tx);
}
}
The following method is used by me to create a set of dynamic widgets on a button click and display the contents of an array in the labels!
public void addLabel()
{
for (int i = 0; i < array.Length; i++)
{
Label lbl = new Label();
lbl.Text = array[i]+"\n";
lbl.AutoSize = true;
flowLayoutPanel1.Controls.Add(lbl);
}
}
The problem I face is that some labels display on the same line but I want only one label in each line! How can I modify my code?
You should use flowLayoutPanel1.SetFlowBreak(lbl, true); like this:
for (int i = 0; i < array.Length; i++)
{
Label lbl = new Label();
lbl.Text += array[i] + "\n";
lbl.AutoSize = true;
flowLayoutPanel1.Controls.Add(lbl);
flowLayoutPanel1.SetFlowBreak(lbl, true);
}
However currently you are creating the label in each iteration of the loop. If you just need one label with line breaks you can change your code like below:
Label lbl = new Label();
for (int i = 0; i < array.Length; i++)
{
lbl.Text += array[i] + "\n";
}
lbl.AutoSize = true;
flowLayoutPanel1.Controls.Add(lbl);
You can use this. This is simple...
public void addLabel()
{
for (int i = 0; i < array.Length; i++)
{
Label lbl = new Label();
lbl.Text = array[i] + "\n";
lbl.AutoSize = true;
flowLayoutPanel1.Controls.Add(lbl);
flowLayoutPanel1.FlowDirection = FlowDirection.TopDown;
}
}
Try this
int lblStartPosition = 100;
int lblStartPositionV = 25;
for (int i = 0; i < array.Length; i++)
{
Label lbl = new Label();
lbl.Text = array[i]+"\n";
lbl.AutoSize = true;
lbl.Location = new System.Drawing.Point(lblStartPosition , lblStartPositionV);
flowLayoutPanel1.Controls.Add(lbl);
lblstartPositionV += 30;
}
So I have
TableLayoutPanel table = new System.Windows.Forms.TableLayoutPanel();
public int d;
private void button1_Click(object sender, EventArgs e)
{
d = (int)numericUpDown1.Value;
table.ColumnCount = d;
table.RowCount = d;
this.Controls.Add(table);
int k = 1;
for (int i = 0; i < d; ++i)
for (int j = 0; j < d; j++)
{
Label lab = new System.Windows.Forms.Label();
lab.Size = new Size(50, 50);
lab.Text = (k).ToString();
lab.TabIndex = k++;
lab.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
table.Controls.Add(lab, j, i);
}
table.Show();
}
That is one Button Click method.
I have Button2 Click method and I want to change lab.TabIndex or/and lab.Text.
How can I do that?
And second question:
how can i do something on Click one of that labels? Let's say that i want to change a color one of the labels i click...how can I do that?
I'm a beginner so...have mercy :)
define a array of labels (global variable, like you did with table):
Label[] labels;
in button1_Click, add the line of code:
labels = new Label[d*d]; // array of d*d labels
inside the loop, define the specific label, like:
labels[i*d+j] = new System.Windows.Forms.Label();
So your loop will look like:
for (int i = 0; i < d; ++i)
for (int j = 0; j < d; j++)
{
labels[i*d+j] = new System.Windows.Forms.Label();
labels[i*d+j].Size = new Size(50, 50);
labels[i*d+j].Text = (k).ToString();
labels[i*d+j].TabIndex = k++;
labels[i*d+j].TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
table.Controls.Add(labels[i*d+j], j, i);
}
you can access them from other buttons also in the same way (labels[n])
This is my code.
But all my textboxes's value is just null.
public void createTxtTeamNames()
{
TextBox[] txtTeamNames = new TextBox[teams];
int i = 0;
foreach (TextBox txt in txtTeamNames)
{
string name = "TeamNumber" + i.ToString();
txt.Name = name;
txt.Text = name;
txt.Location = new Point(172, 32 + (i * 28));
txt.Visible = true;
i++;
}
}
Thanks for the help.
The array creation call just initializes the elements to null. You need to individually create them.
TextBox[] txtTeamNames = new TextBox[teams];
for (int i = 0; i < txtTeamNames.Length; i++) {
var txt = new TextBox();
txtTeamNames[i] = txt;
txt.Name = name;
txt.Text = name;
txt.Location = new Point(172, 32 + (i * 28));
txt.Visible = true;
}
Note: As several people have pointed out in order for this code to be meaningful you will need to add each TextBox to a parent Control. eg this.Controls.Add(txt).
You need to initialize your textbox at the start of the loop.
You also need to use a for loop instead of a foreach.
You need to new up your TextBoxes:
for (int i = 0; i < teams; i++)
{
txtTeamNames[i] = new TextBox();
...
}
You are doing it wrong, you have to add textbox instances to the array, and then add it to the form. This is how you should do it.
public void createTxtTeamNames()
{
TextBox[] txtTeamNames = new TextBox[10];
for (int u = 0; u < txtTeamNames.Count(); u++)
{
txtTeamNames[u] = new TextBox();
}
int i = 0;
foreach (TextBox txt in txtTeamNames)
{
string name = "TeamNumber" + i.ToString();
txt.Name = name;
txt.Text = name;
txt.Location = new Point(0, 32 + (i * 28));
txt.Visible = true;
this.Controls.Add(txt);
i++;
}
}
private void button2_Click(object sender, EventArgs e)
{
TextBox tb = new TextBox();
tb.Name = abc;
tb.Text = "" + i;
Point p = new Point(20 + i, 30 * i);
tb.Location = p;
this.Controls.Add(tb);
i++;
}
private void button3_Click(object sender, EventArgs e)
{
foreach (TextBox item in this.Controls.OfType<TextBox>())
{
MessageBox.Show(item.Name + ": " + item.Text + "\\n");
}
}
Is there a way to create 3 rows with 3 colums with labels (or similar) to create a 2d "map" with data to manipulate in an easy way?
just placing 9 labels is easy but I want each labels to be accessed with the same array.
How it looks like in the form:
label1 label2 label3
label4 label5 label6
label7 label8 label9
If i need to change the property of label5 I would like to access it something like this:
labelarray[1][1].Text = "Test";
(labelarray[row][column].Property )
How do I do this?
Or could this be achieved in another way?
class Data
{
private string text;
public string Text
{
get { return text; }
set { text = value; }
}
}
class Program
{
static void Main(string[] args)
{
Data[,] map = new Data[3, 3];
map[1, 1] = new Data();
map[1, 1].Text = "Test";
}
}
Edit: fixed error.
private void button1_Click(object sender, EventArgs e)
{
string[] nine_labels = { "a", "b", "c", "d", "e", "f", "g", "h", "i" };
var labelarray= new Label[3,3];
// putting labels into matrix form
int c = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
var lbl = new Label();
lbl.Text = nine_labels[c];
lbl.Top = i * 100;
lbl.Left = j * 100;
labelarray[i, j] = lbl;
c++;
}
}
// adding labels to form
foreach (var item in labelarray)
{
this.Controls.Add(item);
}
// test
labelarray[1, 1].Text = "test";
}
NOTE: You'll need to add one button and call this function on Click of that button.
The answer from tehMick actually lead to a runtime exception in .NET 2.0 but, except for that, the example is straight to the point.
The types of the two dimensional array must have a public accessible property, so you can access it directly as you said:
public class DTO
{
private String myStrProperty;
public String MyStrProperty
{
get {return myStrProperty; }
set { myStrProperty = value; }
}
public DTO(string myStrProperty)
{
this.myStrProperty = myStrProperty;
}
}
class Program
{
private static Logger logger;
static void Main(string[] args)
{
DTO[,] matrix =
{
{new DTO("label1"), new DTO("label2")},
{new DTO("label3"), new DTO("label4")}
};
matrix[0, 1].MyStrProperty = "otherValue";
}
}
Here's an example that's specific to winforms. Hopefully it will answer the question a little bit better:
const int spacing = 50;
Label[][] map = new Label[3][];
for (int x = 0; x < 3; x++)
{
map[x] = new Label[3];
for (int y = 0; y < 3; y++)
{
map[x][y] = new Label();
map[x][y].AutoSize = true;
map[x][y].Location = new System.Drawing.Point(x * spacing, y * spacing);
map[x][y].Name = "map" + x.ToString() + "," + y.ToString();
map[x][y].Size = new System.Drawing.Size(spacing, spacing);
map[x][y].TabIndex = 0;
map[x][y].Text = x.ToString() + y.ToString();
}
this.Controls.AddRange(map[x]);
}
map[1][1].Text = "Test";