Assign button text dynamically via a loop - c#

I am trying to assign button controls text dynamically by using a loop like the following:
int ButtonNumber = 1;
while (ButtonNumber < 10)
{
//WHAT TO DO HERE!?
ButtonNumber++;
}
I wanna avoid the following:
button1.Text = "";
button2.Text = "";
button3.Text = "";
button4.Text = "";
button5.Text = "";
button6.Text = "";
button7.Text = "";
button8.Text = "";
button9.Text = "";

Ideally, don't have button1, button2 etc as variables in the first place. Have a collection (e.g. a List<Button>):
private List<Button> buttons = new List<Button>();
(EDIT: You'd need to populate this somewhere, of course...)
then to update the text later:
for (int i = 0; i < buttons.Count; i++)
{
buttons[i].Text = "I'm button " + i;
}
This doesn't play terribly nicely with the designer, unfortunately.
You can fetch a control by ID, but personally I try not to do that. If you really want to:
for (int i = 1; i <= 10; i++)
{
Control control = Controls["button" + i];
control.Text = "I'm button " + i;
}

Create an array of buttons:
Button[] buttons = new Button[10];
for(int i = 1; i < 10; i++)
{
buttons[i] = new Button();
buttons[i].Text = "";
}

You can always use this.Controls.Find() to locate a named control in WinForms.

You can also do like this simply, assuming if you have all buttons in your form
foreach (Control c in this.Controls)
{
if (c is Button)
{
c.Text = "MyButton" + (c.TabIndex + 1);
}
}

// try this
Button btn;
for (int n_i = 0; n_i < 10; n_i++)
{
btn = (Button)Controls.Find("button" + n_i,true)[0];
btn.Text = "";
}

Related

I can't reach the Label on the Panel

public void blockControl()
{
for (int i = 0; i < 10; i++)
{
if (game.wordCount < i + 1)
{
Label lbl = (Label)game.Controls["wordLabel" + (i + 1).ToString()];
lbl.Visible = false;
Panel a1 = (Panel)game.Controls["wordLabel" + (i + 1).ToString()];
a1.Visible = false;
}
}
}
This is how I access the labels in the user control.
But if i put labels on the panel
lbl=0 is happening
What should i do to reach?
I solved this way
public void blockControl()
{
for (int i = 0; i < 10; i++)
{
if (form1.harfSayisi < i + 1)
{
Panel abc = (Panel)form1.Controls["panel" + (i + 1).ToString()];
Label lbl = (Label)abc.Controls["lblHarf" + (i + 1).ToString()];
lbl.Visible = false;
abc.Visible = false;
}
}
}
You can search RECURSIVELY using Controls.Find(). This way you don't need to get a reference to the Panel first. The search will find the control no matter how deeply nested it is inside other containers.
Shortened code could then be:
Label lbl = form1.Controls.Find("lblHarf" + (i + 1).ToString(), true).FirstOrDefault() as Label;
if (lbl != null)
{
// ... do something with "lbl" ...
}

c# change textbox name in code

i was just wondering if something like this is possible:
for(int i = 0; i < 7; i++)
{
textbox + i + .text = aString;
}
I want to change a piece of code to work on multiple textboxes, without having to type the whole code 6 times.
does anyone know if this is possible and how? Thanks :3
in C# you can find your control in your page aspx.
Example:
for (int i = 0; i < 10; i++)
{
TextBox textBox = this.Page.FindControl("textbox" + i.ToString()) as TextBox;
if (textBox != null)
{
textBox.Text = "change Text";
}
}
Well, just telling you that i'm newbie....
public void AddStringToTextbox(int i, string value)
{
switch(i)
{
case: 0:
break;
case: 1:
textbox1.text = value;
break;
case: 2:
textbox2.text = value;
break;
case: 3:
textbox3.text = value;
break;
//and more
}
}
//how to use
for(int i = 0; i < 7; i++)
{
AddStringToTextbox(i, aString);
}
Try this one for Winforms, should be a bit safer.
var textBoxes = this.Controls.OfType<TextBox>();
for (int i = 0; i < 10; i++)
{
var textBox = textBoxes.FirstOrDefault<TextBox>(t => t.Name.Equals("textBox" + i.ToString()));
if(textBox != null)
{
textBox.Text = i.ToString();
}
}
Try putting your controls in an array or list and reference them from there. E.g.:
public class MyClass
{
private List<TextBox> textboxes;
public MyClass()
{
this.InitializeComponent();
textboxes = new List<TextBox>(){ textbox1, textbox2, textbox3 };
}
private void UpdateTextBoxes(string aString)
{
for(var i = 0; i < textboxes.Count; i++)
{
textboxes[i].Text = aString;
}
}
}
Or, if it is a simple an update as your question suggests, try
textbox1.Text =
textbox2.Text =
textbox3.Text =
textbox4.Text =
textbox5.Text =
textbox6.Text =
textbox7.Text =
aString;
May be this help you. Make a list of TextBox and use where ever you need.
List<TextBox> textBoxs = new List<TextBox>();
textBoxs.Add(textBox1);
textBoxs.Add(textBox2);
textBoxs.Add(textBox3);
textBoxs.Add(textBox4);
textBoxs.Add(textBox5);
textBoxs.Add(textBox6);
textBoxs.Add(textBox7);
for (int i = 0; i < 7; i++)
{
textBoxs[i].Text = aString;
}

Dynamic button not firing click event or page life cycle for button(not dynamic)

I've got different entity types and depending on which entity is clicked (dropdownlist) the amount and types of uploads which is needed is different every time.
Thus, I am creating multiple dynamic upload controls in a dynamic table with a dynamic upload button to upload all files at the same time (I also tried adding a button on the asp.net page as well). Everything creates fine and I can choose the files to upload.
The problem that I am having is that the dynamic button control is not firing its onclicked event, thus I am not able to save the files. I tried creating a button on the asp.net side which does fire, but because of the page life cycle it's not picking up my upload controls.
I then tried to add the OnInit event and created the dynamic button in there and the rest of the upload dynamic controls on the selected index change of the dropdown, but then the everything gets created except the dynamic upload controls. The click event then fires. (the Page_Init does the same).
Preferably I would like the button not to be dynamic but reach the file upload controls to save the files. Is there a way around the page life cycle that I can achieve this or could anybody please tell me what I am doing wrong? Or how can I get the dynamic button to fire the click event?
Any help will be greatly appreciated....
Here is my code of what I done:
protected void lstLegalEntity_SelectedIndexChanged(object sender, EventArgs e)
{
if (CTflag == false)
{
this.Rows = tblRow;
this.Columns = tblCol;
lblAmountOfRows.Text = "4";
CreateDynamicTable();
}
else
{
CTflag = true;
}
clrControls();
}
protected void CreateDynamicTable()
{
string filterstring = "";
filterstring = "SELECT let.ID, UploadType " +
"FROM [dbo].[AssetContract_LegalEntityLinks] lel " +
"INNER Join [dbo].[AssetContract_LegalEntity] le " +
"ON lel.LegalEntityRef = le.ID " +
"INNER JOIN [dbo].[AssetContract_LegalEntityTypes] let " +
"ON lel.LegalTypeRef = let.ID " +
"WHERE lel.LegalEntityRef = #LegalEntityRef ";
cn = new SqlConnection(GetConnectionString());
SqlCommand myCmd = new SqlCommand();
myCmd.CommandText = filterstring;
myCmd.Connection = cn;
myCmd.CommandType = CommandType.Text;
if (lstLegalEntity.SelectedValue != "")
{
myCmd.Parameters.AddWithValue("#LegalEntityRef", Convert.ToInt32(lstLegalEntity.SelectedValue));
}
else
{
myCmd.Parameters.AddWithValue("#LegalEntityRef", Convert.ToInt32(0));
}
cn.Open();
SqlDataReader myReader = myCmd.ExecuteReader();
tblRow = GetUploadControlsCount();
if (CTflag == false)
{
table.Caption = "Upload Requirements";
table.ID = "Upload Requirements";
table.BackColor = System.Drawing.Color.BurlyWood;
divFileUploads.Controls.Add(table);
for (i = 0; i < 1; i++)
{
row = new TableRow();
row.BorderStyle = BorderStyle.Ridge;
for (j = 0; j <= tblCol; j++)
{
cell = new TableCell();
cell.BorderWidth = 5;
cell.BorderStyle = BorderStyle.Ridge;
cell.BorderColor = System.Drawing.Color.Azure;
for (j = 0; j <= tblCol; j++)
{
string[] Header = { "Upload Type", "File" };
Label lbl = new Label();
lbl.ID = "lblHeader" + j;
if (j == 1)
{
lbl.Width = 220;
}
else
{
lbl.Width = 100;
}
lbl.Text = Header[j];
cell.Controls.Add(lbl);
}
row.Cells.Add(cell);
}
table.Rows.Add(row);
}
int readCount = 1;
while (myReader.Read())
{
for (i = 0; i < 1; i++)
{
row = new TableRow();
row.ID = "rw" + myReader["UploadType"].ToString();
row.BorderStyle = BorderStyle.Ridge;
for (j = 0; j <= tblCol; j++)
{
cell = new TableCell();
cell.ID = tbColId + i + j + myReader["UploadType"].ToString(); ;
cell.BorderWidth = 5;
cell.BorderStyle = BorderStyle.Ridge;
cell.BorderColor = System.Drawing.Color.Azure;
for (j = 0; j <= 0; j++)
{
Label lbl = new Label();
lbl.ID = "lblCCRow" + i + "Col" + j + myReader["UploadType"].ToString();
lbl.Text = myReader["UploadType"].ToString();
lbl.Width = 100;
// Add the control to the TableCell
cell.Controls.Add(lbl);
}
for (j = 0; j < 1; j++)
{
fileUp = new FileUpload();
//m = i; n = j;
fileUp.ID = filename + i + readCount.ToString();
fileUp.Width = 350;
cell.Controls.Add(fileUp);
cmdArg = fileUp.ID;
}
row.Cells.Add(cell);
}
table.Rows.Add(row);
readCount++;
}
i = 0;
j = 0;
}
for (i = 0; i < 1; i++)
{
rrow = new TableRow();
rrow.ID = "ResultRow";
rrow.BorderStyle = BorderStyle.Ridge;
rrow.HorizontalAlign = HorizontalAlign.Center;
for (j = 0; j <= tblCol; j++)
{
rcell = new TableCell();
rcell.ID = "resultCol" + j;
rcell.BorderWidth = 5;
rcell.BorderStyle = BorderStyle.Ridge;
rcell.BorderColor = System.Drawing.Color.Azure;
for (j = 0; j < 1; j++)
{
btnUpload = new Button();
btnUpload.Width = 100;
btnUpload.Text = "Upload";
btnUpload.ID = btnUpload.Text;
rcell.Controls.Add(btnUpload);
btnUpload.Click += new EventHandler(UpLdButton_Click);
}
rrow.Cells.Add(rcell);
}
table.Rows.Add(rrow);
}
CTflag = true;
ViewState["dynamictable"] = true;
cn.Close();
myReader.Close();
}
}
protected void UpLdButton_Click(object sender, EventArgs e)
{
for (int i = 0; i < tblRow; i++)
{
fileUp = (FileUpload)FindControlRecursive(this, string.Format("fileUpLoader{0}{1}", 0, i));
//rest of code to save file
}
}
Why don't you use asp repeater to display FileUploads?
you can easily set the html template and design, as DataSource use DataTable of your data
<asp:repeater id='rp' runat='server'>
<ItemTemplate>
<%Eval("UploadType"%> -this is the caption
<asp:fileUpload id='file' runat='server'/>
<asp:Button id='btnUpload' onclick='UploadClick' runat='server'/>
</ItemTemplate>
protected void lstLegalEntity_SelectedIndexChanged(object sender, EventArgs e)
{
//get data into DataTable
rp.DataSource=dt;
rp.DataBnd();
}
protected void UpLdButton_Click(object sender, EventArgs e)
{
for (int i = 0; i < tblRow; i++)
{
//this way you get the relevant fileUpload
fileUp = (FileUpload)((Button)Sender).Parent.FindControl ("file);
//rest of code to save file
}
}
You were on the right track when you added the dynamic buttons in the OnInit event - you just needed to also add the FileUpload controls too.
All of the controls need to be recreated on every postback. .NET automatically handles the controls you added to the ASPX page. You are responsible for the dynamic controls you add programmatically.
Here are a couple of approaches you can take from where you are:
Continue with dynamically adding the control but make the change so you are adding all of the controls, not just the buttons.
If you have a known maximum to the number of controls then you could add then all and control what is displayed using the Visible property. This works when the number of controls is small. It looks like you are putting borders around the table cells so this would not look so good in your case. If you can change the layout so that hiding the controls does not result in residual artifacts then this is an option.

Creating and positioning an array of buttons

Hello everyone I need some help with the positioning of an array of buttons.I want to make this function so it scans the name of the previous button and it names the next one +1,afterwards I want to position these buttons on the screen having a certain space between them and them being positioned in the center of the screen.I have tried many times to modify my method but I don't know how to get this method to work.
This is how my method looks like.
UPDATED
PS.Reference not set to an instance of an object Q.Q
public Button[] ButtonCreator(byte numOfBtnsNeeded,Form1 form)
{
Button[] mybtns = new Button[numOfBtnsNeeded];
foreach (Button b in mybtns)
{
for (int i = 0; i < mybtns.Length; i++)
{
mybtns[i].Name = i.ToString();
mybtns[i].Parent = form;
mybtns[i].Height = 50;
mybtns[i].Width = 50;
for (int k = i + 1; k < mybtns.Length; k++)
{
mybtns[i].Location = new Point(190, 80);
mybtns[k].Location = Point.Add(new Point(mybtns[i].Location.X + 10,mybtns[i].Location.Y + 10),new Size(mybtns[i].Size.Width,mybtns[i].Size.Height));
}
}
}
foreach (Button b in mybtns)
{
b.Show();
}
return mybtns;
}
Play with this example...
public partial class Form1 : Form
{
private List<List<Button>> grid = new List<List<Button>>();
public Form1()
{
InitializeComponent();
byte numRows = 5;
byte numCols = 5;
for (byte i = 0; i < numRows; i++)
{
grid.Add(ButtonRowCreator(numCols, 25, (i+1) * 50));
}
}
public List<Button> ButtonRowCreator(byte numOfBtnsNeeded, int x, int y)
{
List<Button> btns = new List<Button>();
for (int i = 0; i < numOfBtnsNeeded; i++)
{
Button btn = new Button();
btn.Size = new Size(50, 50);
btn.Location = new Point(x + (i * btn.Width), y);
btns.Add(btn);
this.Controls.Add(btn);
btn.Click += new EventHandler(btn_Click);
}
return btns;
}
void btn_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
btn.Text = "X";
int curRow = -1, curCol = -1;
for(int i = 0; i < grid.Count; i++)
{
int index = grid[i].IndexOf(btn);
if (index != -1)
{
curRow = i;
curCol = index;
Console.WriteLine("curRow = " + curRow.ToString() + ", curCol = " + curCol.ToString());
}
}
// ... now you can use "curRow", "curCol" and "grid" to do something ...
// reset all BackColors:
foreach (List<Button> row in grid)
{
foreach (Button col in row)
{
col.BackColor = Button.DefaultBackColor;
}
}
// the below should give you some examples for the
// syntax necessary to access buttons in the grid
// highlight current row:
foreach (Button col in grid[curRow])
{
col.BackColor = Color.Yellow;
}
// highlight current col:
for (int i = 0; i < grid.Count; i++)
{
grid[i][curCol].BackColor = Color.Yellow;
}
}
}
You cannot change a foreach variable reference (ie b). If you want to initialize an array you should use for loop:
for(int i = 0; i < numOfBtnsNeeded; i++)
{
var button = mybtns[i] = new Button();
//Here you can modify the reference of button.
}
Also, mybtns will be full of nulls since Button is a reference type which means it's default value is a null.
you want something like:
public Button[] ButtonCreator(byte numOfBtnsNeeded)
{
Button[] mybtns = new Button[numOfBtnsNeeded];
for (int i = 0; i < mybtns.Length; i++)
{
mybtns[i] = new Button();
mybtns[i].Name = (i + 1).ToString();
}
return mybtns;
}
I'm not sure why you're using a byte over an int, but it works either way.
Essentially, when you create the array, you're not creating the objects within the array. And you cannot modify the thing you are looping over within a foreach loop, so you need a for loop.

Increase the label name

I need a command code that increase the label name.
I need to display in 10 (example) labels some texts.
Example:
label1.Text = "1";
label2.Text = "2";
label3.Text = "3";
label4.Text = "4";
label5.Text = "5";
label6.Text = "6";
I need to increase the number from label name (label1, label2, etc.) in a foreach where I will increase the variable i (i will be use in a structure like this label.Name = "label" + i.ToString();).
I hope that you understand what I want to say.
I try this but don't work:
Label[] label = new Label[2];
int ii = 0;
foreach(...) // go through a list
{
label[ii] = new Label();
label[ii].Text = x.materie + tip + "\nsala " + x.sala;
label[ii].Visible = true;
label[ii].Location = new System.Drawing.Point(cX, cY);
label[ii].SetBounds(cX, cY, 98, cH);
label[ii].MinimumSize = new Size(98, cH);
label[ii].MaximumSize = new Size(98, cH);
ii++;
}
int count = 10;
for (int i = 1; i <= count; i++)
{
// setup label and add them to the page hierarchy
Label lbl = new Label();
lbl.Name = "label" + i;
lbl.Text = i.ToString();
//assuming form1 is a form in your page with a runat="server" attribute.
this.Controls.Add(lbl);
}
Assuming you have an array of Label controls, label, you can do the following:
int i = 1;
for (int i = 1; i < label.Length; i++)
{
lbl.ID = String.Format("label{0}", i.ToString());
}

Categories

Resources