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" ...
}
Related
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;
}
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.
I am adding dynamic controls and I want to increment the values in Label when adding controls dynamically
Code:
private int controlCount
{
get
{
int val = 0;
try
{
val = (int)ViewState["ControlCount"];
}
catch (Exception e)
{
// handle exception, if required.
}
return val;
}
set { ViewState["ControlCount"] = value; }
}
protected void addnewtext_Click(object sender, EventArgs e)
{
int i = controlCount++;
for (int j = 0; j <= i; j++)
{
AddVisaControl ac = (AddVisaControl)Page.LoadControl("AddVisaControl.ascx");
Label lb = new Label();
string z = Convert.ToString(i + 1);
lb.Text = "Visa " + z;
rpt1.Controls.Add(lb);
lb.Attributes.Add("class", "style8");
rpt1.Controls.Add(ac);
rpt1.Controls.Add(new LiteralControl("<BR>"));
}
}
In the below image I'm getting label values i.e(Visa 3) are overwriting
Any ideas?
You want
string z = Convert.ToString(j + 1);
rather than
string z = Convert.ToString(i + 1);
i have a 10x10 text box ( 100 of them )
I write this code to write into text file :
foreach (Control control in Panel1.Controls)
{
var textBox = control as TextBox;
if (textBox != null)
{
if (string.IsNullOrEmpty(textBox.Text)) // ignore this
{
textBox.Style["visibility"] = "hidden";
}
textBox.Enabled = false;
if (numberofCommas > 8)
{
stringWriter.Write(textBox.Text);
numberofCommas = 0;
}
else
{
stringWriter.Write("," + textBox.Text );
numberofCommas++;
recordsWritten++;
}
if (recordsWritten == 10)
{
stringWriter.WriteLine();
recordsWritten = 0;
}
else
{
}
From the above i want to have 10 rows of 9 commas in the text file but instead i have 9 rows of 10 commas in the text file , is my code logic wrong? because i have been looking it for hours , i still couldn't solve it . sorry if my logic is bad , i am new to programming.
I think that you should increment recordsWritten in the last step:
if (numberofCommas > 8)
{
stringWriter.Write(textBox.Text);
numberofCommas = 0;
recordsWritten++;
}
Here is a better way to do it using Linq:
var textBoxes = Panel1.Controls.OfType<TextBox>().Select((t, i) => new { TextBox = t, Index = i }).ToList();
foreach (var tb in textBoxes)
{
if (string.IsNullOrEmpty(tb.TextBox.Text))
tb.TextBox.Style["visibility"] = "hidden";
tb.TextBox.Enabled = false;
}
foreach (var line in textBoxes.GroupBy(e => e.Index / 10)
.Select(e =>
string.Join(", ",
e.Select(a => a.TextBox.Text).ToArray())))
stringWriter.WriteLine(line);
I wouldn't recommend you to use 100 TextBox objects, you can use a DataGridView binded to a DataTable with 10 rows and 10 columns. You can still edit your data and save it to a file.
Try the below code
StringWriter stringWriter1 = new StringWriter();
DataTable dataTable1 = new DataTable();
private void Form1_Shown(object sender, EventArgs e)
{
dataGridView1.AllowUserToAddRows = false;
int i;
for (i = 0; i < 10; i++)
{
dataTable1.Columns.Add("Column" + (i + 1), typeof(string));
}
for (i = 0; i < 10; i++)
{
DataRow dataRow1 = dataTable1.NewRow();
dataTable1.Rows.Add(dataRow1);
}
dataGridView1.DataSource = dataTable1;
}
private void button1_Click(object sender, EventArgs e)
{
string rowString = "";
int i,j;
for (i = 0; i < 10; i++)
{
rowString = "";
for (j = 0; j < 10; j++)
{
if (dataTable1.Rows[i][j].ToString().Contains(",") == true)
{
//Enclosing the field data inside quotes so that it can
//be identified as a single entity.
rowString += "\"" + dataTable1.Rows[i][j] + "\"" + ",";
}
else
{
rowString += dataTable1.Rows[i][j] + ",";
}
}
rowString = rowString.Substring(0, rowString.Length - 1);
stringWriter1.WriteLine(rowString);
}
}
you just need to add a DataGridView onto your Form.
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 = "";
}