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;
}
Related
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" ...
}
I want to store all listbox control values in a string with "," separated so that I can show it in the label. I'm using for loop but giving error
for (int i = 0; i < ListBox2.Items.Count; i++){
if (ListBox2.Items[i].Selected == true || ListBox2.Items.Count > 0){
string projectnames += ListBox2.Items[i].ToString();
}
}
string projectnames = "";
bool firstValue = true;
for (int i = 0; i < ListBox2.Items.Count; i++)
{
if (ListBox2.Items[i].Selected == true || ListBox2.Items.Count > 0)
{
if(!firstValue)
{
projectnames += ", " + ListBox2.Items[i].ToString();
}
else
{
projectnames += ListBox2.Items[i].ToString();
firstValue = false;
}
}
}
Instead of the loop i'd use LINQ + String.Join:
var selected = ListBox2.Items.Cast<ListItem>()
.Where(li => li.Selected)
.Select(li => li.ToString());
string projectnames = String.Join(",", selected);
On that way it's much better to read and you don't need to care about trailing commas.
This will generate a string of all selected items in the list.
string projectnames = "";
for (int i = 0; i < ListBox2.Items.Count; i++)
{
if (ListBox2.Items[i].Selected)
{
projectnames += ListBox2.Items[i].ToString() + ", ";
}
}
The most succinct method I can think of is:
var label = string.Join(",", listBox2.Items.Cast<string>());
(This uses System.Linq)
Try this, will help you!
protected void btnSave_Click(object sender, EventArgs e)
{
string selectedItem=string.Empty;
if (ListBox1.Items.Count > 0)
{
for (int i = 0; i < ListBox1.Items.Count; i++)
{
if (ListBox1.Items[i].Selected)
{
selectedItem += ListBox1.Items[i].Text.ToString() + ", ";
//insert command
}
}
}
}
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.
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 = "";
}