Lets just say that I have three textboxes: TextBox1, TextBox2, TextBox3. Normally if I wanted to change the text for example I would put TextBox1.Text = "Whatever" and so on. For what I'm doing right now I would like to something like (TextBox & "i").Text. That obviously isn't the syntax I need to use I'm just using it as an example for what I need to do. So how can I do something like this? The main reason I'm doing this is to reduce code with a loop.
Please keep in mind that I'm not actually changing the text of the textboxes I'm simply using that as an example to get the point across.
Use an array to access the TextBox objects by index:
TextBox[] textBoxes = new TextBox[3];
textBoxes[0] = textBox1;
textBoxes[1] = textBox2;
textBoxes[2] = textBox3;
for (int i = 0; i < 3; i++)
{
textBoxes[i].Text = "Whatever";
}
this.Controls.OfType<TextBox>().First(r => r.ID == "textbox1").Text = "whatever";
if you know of course, that textbox with id 'textbox1' exists
or
foreach (var tb in this.Controls.OfType<TextBox>()) {
tb.Text ="whatever";
}
When you create your TextBox dynamically you can use an array of TextBoxes which is much easier.
However it is possible to use reflection, too:
var textBoxes = GetType().GetFields( BindingFlags.NonPublic | BindingFlags.Instance )
foreach( var fieldInfo in textBoxes )
{
if( fieldInfo.FieldType == typeof( TextBox ) )
{
var textBox = ( TextBox )fieldInfo.GetValue( this );
textBox.Text = "";
}
}
I would recommend not doing this in general. Often, it's best to put your object references into a collection, and then work on the collection - or to use some other approach along those lines.
However, it is possible to do this (though it's slow) via Reflection:
var fields = this.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic).Where(f => f.Name.StartsWith("TextBox"));
foreach(var field in fields)
{
TextBox box = field.GetValue(this) as TextBox;
if (box != null)
box.Text = "Whatever";
}
As the goal is to reduce code size, no, you can't do that. Near to what you are trying to achieve would be:
to save the controls in a List or Array
loop the List or Array
List<TextBox> myTxts = new List<TextBox> {textBox1, textBox2, textBox3};
foreach(TextBox txt in myTxts) {
txt.Text = "";
}
Another approach would be to poll the Controls Collection of the Form
foreach(Control ctl in this.Controls) {
var txt = ctl as TextBox;
if (txt != null) {
txt.Text = "";
}
}
Related
I´d like to be able to acces a Checkbox (or Textbox or similar) by just adding the last digit or letter like this:
int number = 1;
CheckBox tempCheckbox = "myform.checkBoxTool" + number;
tempCheckbox.Checked = true;
I guess this is already covered but I cant seem to find the right search words.
You have several opportunities. The most obvious is to use the ControlsCollection of your Form:
var checkBox = myForm.Controls["myform.checkBoxTool" + number];
Alternativly go with ControlCollection.Find:
var checkBox = myForm.Controls.Find("myform.checkBoxTool" + number, true).FirstOrDefault();
However if you have multiple checkboxes that all differ only by a single index, it´s a better idea to store them as a list or array of CheckBox in the first place:
List<CheckBox> myCheckBoxes = new List<CheckBox>();
Now you can easily access them by index:
var checkBox = myChekBoxes[number];
Be aware that indices - as any number in .NET - are zero-based. So the very first element in the list has index zero.
Technically, if tempCheckbox is on the same form where the querying code is, you can try Linq;
using System.Linq;
...
public partial class MyForm : Form {
...
int number = 1;
CheckBox tempCheckbox = this
.Controls
.Find($"checkBoxTool{number}", true) // we don't want "myform." here
.OfType<CheckBox>()
.FirstOrDefault();
// If check box found, check it
if (tempCheckbox != null)
tempCheckbox.Checked = true;
A better approach is to organize these controls into a collection, e.g. Dictionary:
public partial class MyForm : Form {
private Dictionary<int, CheckBox> m_CheckBoxTools = new Dictionary<int, CheckBox>();
public MyForm() {
InitializeComponent();
m_CheckBoxTools.Add(1, checkBoxTool1);
m_CheckBoxTools.Add(3, checkBoxTool3);
m_CheckBoxTools.Add(25, checkBoxTool25);
}
Then you can query the dictionary
if (m_CheckBoxTools.TryGetValue(number, out CheckBox tempCheckbox)) {
tempCheckbox.Checked = true;
}
you can use something like below
CheckBox chkList1 =new CheckBox();
chkList1.Text = strCheckboxText;
chkList1.ID="Chk"+intControlIndex;
chkList1.Font.Name = "Verdana";
chkList1.Font.Size = 9;
chkList1.Checked = true;
this.Form.Controls.Add(chkList1);
then you can check existing check box and find your ID. Assuming checkbox is in Panel control.
string str1="";
foreach (Control c in panel1.Controls)
{
if((c is CheckBox) && ((CheckBox) c).Checked)
str1 += c.ID+ ", ";
}
str1=str1.Trim();
I have 16 text boxes in my Form whose names are suffixed sequentially from 1 to 16 respectively.
i.e. The 16 test boxes are names TextBox1, 'TextBox2, .... all the way until the 16th one, which is namedTextBox16`.
I would like to read the contents of these 16 text boxes in a loop and modify the ith TextBox's contents or properties based on a certain condition.
How do I do this?
If you use WinForms, easiest way is to store text boxes references in array, in constructor of window:
TextBox[] data = new TextBox[16]{textBox1,textBox2, [...],textBox16};
then you can use for loop to access them.
You can try something like this:
Dictionary<string, string> yourValues = new Dictionary<string, string>();
foreach (Control x in this.Controls)
{
if (x is TextBox)
{
yourValues.Add(((TextBox)x).Name, ((TextBox)x).Text);
}
}
NOTE: On your future question please provide more information and make your question more clear.
i would try to find and modify using Linq:
using System.Linq;
//...
int x = 5; //number of textbox to search
var textbox = this.Controls.OfType<TextBox>().Single(tb => tb.Name.EndsWith(x.ToString()));
textbox.Text = "abc";
In case you have to loop thru all the texboxes in form, you can do something like this:
List<TextBox> textboxes = this.Controls.OfType<TextBox>().ToList();
foreach (TextBox box in textboxes)
{
box.Text = "something";
}
Easiest way according to what you specified is to use Linq.
Let's assume you have 3 TextBoxes :
// 1st -> which is named meTextBox1
// 2nd -> which is named meTextBox2
// 3rd -> which is named meTextBox3
As you can see from above every line differs only by the number ( index .. call it whatever you want ).
Now you can make your base "query" which would look like :
const string TB_NAME = "meTextBox{0}";
And as you can presume right now this will be used inside of string.Format method. Now to retrieve desired TextBox all you have to do is to make Linq statement :
string boxName = string.Format(TB_NAME, 7); // retrieve 7th text box
TextBox tBox = Controls.OfType<TextBox>().FirstOrDefault(tb => tb.Name == boxName);
This example does not consider nested Controls but you can make do this recursively which will retrieve nested Controls:
TextBox ByIndex(int idx, Control parent)
{
TextBox result = null;
string searchFor = string.Format(TB_NAME, idx);
foreach(Control ctrl in parent.Controls)
{
if(!(ctrl is TextBox) && ctrl.HasChildren)
{
result = ByIndex(idx, ctrl);
if( result != null)
break;
}
else if(ctrl is TextBox)
{
if(ctrl.Name = searchFor)
{
result = ctrl as TextBox;
break;
}
}
}
return result;
}
To use above method you can just call it like such :
public class MeForm : Form
{
//.. your code here ..
void meImaginaryMethodToRetrieveTextBox()
{
int meRandomIndex = 7;
TextBox tb = ByIndex(meRandomIndex, this);
// rest of the code...
}
}
I am new to C# programming. I have number of text boxes on a form and instead of writing the same code for each text box, is it possible to use loop for writing same code for each text box? In the code below can we use the variable i for the same?
private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < 3; i++)
{
MessageBox.Show(amountTextBox.Text);
MessageBox.Show(amountTextBox1.Text);
MessageBox.Show(amountTextBox2.Text);
MessageBox.Show(amountTextBox3.Text);
}
}
Thanks in advance.
Instead of trying to generate code (which is a lot harder than it sounds), whenever you have variables named something1, something2, … somethingN, you should consider using an array, or some other collection.
Create an array of text boxes, like this:
var amountTextBoxes = new[] { amountTextBox, amountTextBox1, amountTextBox2, amountTextBox3 };
And then loop through them like this:
for (int i = 0; i < amountTextBoxes.Length; i++)
{
MessageBox.Show(amountTextBoxes[i].Text);
}
Or like this:
foreach (var textBox in amountTextBoxes)
{
MessageBox.Show(textBox.Text);
}
Another option that would probably work in this specific case, (though this solution is not as general as the previous one) involves directly searching for the controls based on the name. If this is a Windows Forms application, you could use Find (assuming all controls have the same name pattern):
for (int i = 0; i < 3; i++)
{
var controlName = "amountTextBoxes" + i;
vat textBox = (TextBox)this.Controls.Find(controlName, true)[0];
MessageBox.Show(textBox.Text);
}
Or if this is WPF, you could use FindName:
for (int i = 0; i < 3; i++)
{
var controlName = "amountTextBoxes" + i;
vat textBox = (TextBox)this.FindName(controlName);
MessageBox.Show(textBox.Text);
}
You can try something like this
foreach (Control x in this.Controls)
{
if (x is TextBox)
{
MessageBox.Show(((TextBox)x).Text);
}
}
Better version with OfType extension method:
foreach (TextBox x in this.Controls.OfType<TextBox>().OrderBy(x => x.Name))
{
MessageBox.Show(x.Text);
}
Here is a linq version.
this.Controls
.OfType<TextBox>()
.ToList()
.ForEach(control =>
MessageBox.Show(control.Text));
Instead of using for loop you can do like this also..
*note * - This is for all the textbox controls in the form.. for specific textboxes you may use a for loop which loop through an array or collection of text boxes you need .
foreach(Control control in this.Controls)
{
if(control.GetType() == typeof(TextBox))
{
MessageBox.Show(control.Text);
}
}
As Selman22 mentioned you can solved it using this.Controls.OfType(). You can further refine your query as mentioned below
foreach (TextBox textBox in this.Controls.OfType<TextBox>().Where(x => x.Name.Contains("amountTextBox")).OrderBy(x => x.Name).ToList())
{
MessageBox.Show(textBox.Text);
}
A simple and maintainable way would be to yield return every textbox you're interested in, in the order that you're interested in, and then iterate over that.
private void button1_Click(object sender, EventArgs e)
{
foreach (var textBox in GetAmountTextBoxes())
MessageBox.Show(textBox.Text);
}
private IEnumerable<TextBox> GetAmountTextBoxes()
{
yield return amountTextBox;
yield return amountTextBox1;
yield return amountTextBox2;
yield return amountTextBox3;
}
If you have many textboxes and maintaining this list is cumbersome, you could generalise it to something like:
return Controls.OfType<TextBox>();
But take care to filter it, if necessary, to only the textbox controls you need with Where.
And if order is important, you'll need to devise some function to sort the textboxes (in conjunction with OrderBy). This is necessary because the default sorting algorithm will sort TextBox10 before TextBox1.
Your Code Rewritten:
for (int i = 0; i < 3; i++)
{
MessageBox.Show(amountTextBox[i].Text);
}
Hi when I create textboxes on Windows Application Form I cannot name it as box[0], box[1] and so on. The purpose why I want to do like this is because I want to use them in a loop.
Actually I found TextBox[] array = { firstTextBox, secondTextBox }; works too!
How about making a list of them after you create them? In your form initialization function, you can do something like:
List<TextBox> myTextboxList = new List<TextBox>();
myTextBoxList.Add(TextBox1);
myTextBoxList.Add(TextBox2);
mytextBoxList.Add(TextBox3);
Now you can itterate through with your "myTextboxList" with something like below:
Foreach (TextBox singleItem in myTextboxList) {
// Do something to your textboxes here, for example:
singleItem.Text = "Type in Entry Here";
}
You can create textboxes on runtime and just put them in an array...
If you want to do it in design time, you will have to do some control filtering logic on the whole this.Controls array in order to access only the wanted textboxes. Consider if (currControl is TextBox) if all textboxes in the form are ones you want in the array.
Another option for design time, is putting all wanted textboxes in a panel which will be their parent, and then iterating over the panel's children (controls) and cast them to TextBox.
A runtime solution would be something like:
var arr = new TextBox[10];
for (var i = 0; i < 10; i++)
{
var tbox = new TextBox();
// tbox.Text = i.ToString();
// Other properties sets for tbox
this.Controls.Add(tbox);
arr[i] = tbox;
}
I wouldn't use an array for this, personally. I would use some form of generic collection, like List.
List<TextBox> textBoxList = new List<TextBox>();
//Example insert method
public void InsertTextBox(TextBox tb)
{
textBoxList.Add(tb);
}
//Example contains method
public bool CheckIfTextBoxExists(TextBox tb)
{
if (textBoxList.Contains(tb))
return true;
else
return false;
}
You don't necessarily have to use the Contains method, you could also use Any(), or maybe even find another way- all depends on what you're doing. I just think using a generic collection gives you more flexibility than a simple array in this case.
for C# just use this to create an array of text boxes
public Text [] "YourName" = new Text ["how long you want the array"];
then add the text boxes to the array individually.
TextBox Array using C#
// Declaring array of TextBox
private System.Windows.Forms.TextBox[] txtArray;
private void AddControls(int cNumber)
{
// assign number of controls
txtArray = new System.Windows.Forms.TextBox[cNumber + 1];
for (int i = 0; i < cNumber + 1; i++)
{
// Initialize one variable
txtArray[i] = new System.Windows.Forms.TextBox();
}
}
TextBox[] t = new TextBox[10];
for(int i=0;i<required;i++)
{
t[i]=new TextBox();
this.Controls.Add(t[]);
}
Not sure what is the best way to word this, but I am wondering if a dynamic variable name access can be done in C# (3.5).
Here is the code I am currently looking to "smarten up" or make more elegant with a loop.
private void frmFilter_Load(object sender, EventArgs e)
{
chkCategory1.Text = categories[0];
chkCategory2.Text = categories[1];
chkCategory3.Text = categories[2];
chkCategory4.Text = categories[3];
chkCategory5.Text = categories[4];
chkCategory6.Text = categories[5];
chkCategory7.Text = categories[6];
chkCategory8.Text = categories[7];
chkCategory9.Text = categories[8];
chkCategory10.Text = categories[9];
chkCategory11.Text = categories[10];
chkCategory12.Text = categories[11];
}
Is there a way to do something like ("chkCategory" + i.ToString()).Text?
Yes, you can use
Control c = this.Controls.Find("chkCategory" + i.ToString(), true).Single();
(c as textBox).Text = ...;
Add some errorchecking and wrap it in a nice (extension) method.
Edit: It returns Control[] so either a [0] or a .Single() are needed at the end. Added.
for(...)
{
CheckBox c = this.Controls["chkCategory" + i.ToString()] as CheckBox ;
c.Text = categories[i];
}
You can do that with reflection. But don't.
It's more proper to instantiate a list of contols, add them programmatically to your form, and index that.
Sometimes it can help to put your controls into an array or collection as such:
Checkbox[] chkCataegories = new Checkbox[] { chkCategory1, chkCategory2 ... };
for(int i = 0; i < chkCategories.Length; i++)
chkCategories[i].Text = categories[i];
As another approach, you can dynamically create your checkboxes at runtime instead of design time:
for(int i = 0; i < categories.Length; i++)
{
Checkbox chkCategory = new chkCategory { Text = categories[i] };
someContainer.Controls.Add(chkCategory);
}
At least with dynamically created controls, you don't need to modify your GUI or your form code whenever you add new categories.
You don't need dynamic for that. Put chkCategory1 - 12 in an array, and loop through it with a for loop. I would suggest you keep it around in a field and initialize it at form construction time, because chkCategory seems to be related. But if you want a simple example of how to do it in that simple method, then it would be something like this:
private void frmFilter_Load(object sender, EventArgs e)
{
var chkCategories = new [] { chkCategory1, chkCategory2, chkCategory3, .......... };
for(int i = 0 ; i < chkCategories.Length ; i++ )
chkCategoies[i].Text = categories[i];
}
You know more about the application, so you could perhaps avoid writing out all the control names - for instance, if they are placed on a common parent control, then you could find them by going through it's children.
No, but you could do something like this (untested, beware of syntax errors):
private readonly CheckBox[] allMyCheckboxes = new CheckBox[] { chkCategory1, chkCategory2, ... }
Then you just need to do
for (i = 0; i < 12; i++) allMyCheckboxes[i].Text = categories[i];
The "this.Controls["chkCategory" + i.ToString()]" and "this.Controls.Find("chkCategory" + i.ToString(), true)" both do not work... the former informs you that the contents of the [] are not an int and the latter that ControlCollection does not contain a definition for Find.
Use "Control myControl1 = FindControl("TextBox2");" instead.
I needed this form as I was looping through another array, extracting values and using them to populate form fields. Much easier to look for label1, label2, label3, etc.