I have a group of textbox controls that i would like to populate with an array of doubles.The controls names are numerically incremented like so
Tol1.Text = lineTolFront[0].ToString();
Tol2.Text = lineTolFront[1].ToString();
Tol3.Text = lineTolFront[2].ToString();
Tol4.Text = lineTolFront[3].ToString();
Tol5.Text = lineTolFront[4].ToString();
Tol6.Text = lineTolFront[5].ToString();
//and so on
is there a simpler way to do this using a loop without having to manually input the values?
First, get all those TextBoxes using LINQ (note: this is useful particularly when you have many controls you do not want manually put in a collection).
var tboxes = this.Controls.Cast<Control>()
.OfType<TextBox>()
.Where(l => l.Name.Contains("Tol"));
and then loop through them and set the content.
int i = 0;
foreach(var tb in tboxes)
tb.Text = lineTolFront[i++].ToString();
You could add the TextBoxes to a collection first. It's a little less copy/paste work at least.
var textBoxes = new List<TextBox> { Tol1, Tol2, Tol3, Tol4, Tol5, Tol6 };
for (var i = 0; i < lineTolFront.Count; i++)
textBoxes[i].Text = lineTolFront[i].ToString();
Regarding M Patel's comment, make sure you add the TextBoxes to the collection in the same order you want to assign the doubles from the lineTolFront array.
You could add your controls to an array and loop through it:
var controls = new[] { Tol1, Tol2, Tol3, Tol4, Tol5, }; //etc
for(int i = 0; i < controls.Length; i++)
{
controls[i].Text = lineTolFront[i].ToString();
}
Related
I am getting "index out of range" from this loop. But I need to use new elements that loop founds, how do I do that? Please help to fix the problem
int linkCount = driver.FindElements(By.CssSelector("a[href]")).Count;
string[] links = new string[linkCount];
for (int i = 0; i < linkCount; i++)
{
List<IWebElement> linksToClick = driver.FindElements(By.CssSelector("a[href]")).ToList();
links[i] = linksToClick[i].GetAttribute("href");
}
I think that you could refactor your code:
var linkElements = driver.FindElements(By.CssSelector("a[href]")).ToList();
var links = new List<string>();
foreach (var elem in linkElements)
{
links.Add(elem.GetAttribute("href"));
}
If that works, you could simplify the query:
var instantLinks = driver.FindElements(By.CssSelector("a[href]"))
.Select(e => e.GetAttribute("href"))
.ToList();
You can rewrite your code to bypass the for loop:
string[] links = driver.FindElements(By.CssSelector("a[href]")).Select(l => l.GetAttribute("href")).ToArray();
This should also avoid the index out of range problem, and cut down the amount of code you have to write.
First of all i dont see a point in assigning linkstoclick values inside loop... And Reason for error must be that linksToClick list's length is more than that of linkCount.
int linkCount = driver.FindElements(By.CssSelector("a[href]")).Count;
List<string> links = new List<string>();
for (int i = 0; i < linkCount; i++)
{
List<IWebElement> linksToClick = driver.FindElements(By.CssSelector("a[href]")).ToList();
if (linksToClick.Count < i)
links.Add(linksToClick[i].GetAttribute("href"));
}
This might help with the out of range exception.
Doing this allows you to create a list of type: string without having to explicitly define the size of the list
the first one gets all of your elements by tag name ...let's assume 5.
in the loop, your driver get's all the elements by css selector, and you might have a different number here. let's say 4.
then, you might be trying to set the fifth element in a four element array.
boom.
Easiest fix to debug:
int linkCount = driver.FindElements(By.TagName("a")).Count;
string[] links = new string[linkCount];
// WRITE OUT HOM MANY links you have
for (int i = 0; i < linkCount; i++)
{
List<IWebElement> linksToClick = driver.FindElements(By.CssSelector("a[href]")).ToList();
// ASSERT THAT YOU HAVE THE SAME AMOUNT HERE
If (links.Count != linksToClick.Count)
// your logic here
links[i] = linksToClick[i].GetAttribute("href");
}
I've stumbled upon this code to loop through textboxes, and it's what I need, except I need to do it for ovals (Microsoft.VisualBasic.PowerPacks.OvalShape)
for (int i = 1; i < 29; i++)
{
TextBox textBox = (TextBox)Controls["textBox" + i];
}
So it would probably look like
for (int i = 1; i < 29; i++)
{
Shape oval = (Shape)???["oval" + i]
oval.FillColor = blue;
}
I just don't know what the "Controls" counterpart is for shapes. I don't know if this code will work, so please tell me if there's another way to do this.
You can typically use LINQ to select a certain type of control inside of a collection, in this case Controls and ShapeContainer, by using OfType<>.
var ovals = Controls.OfType<ShapeContainer>()
.SelectMany(sc => sc.Controls.OfType<OvalShape>());
foreach (var oval in ovals)
{
oval.FillColor = Color.Blue;
}
As GrawCube pointed out, the ShapeContainer has its own property for accessing its shapes, appropriately named Shapes. You may have to use this instead:
var ovals = Controls.OfType<ShapeContainer>()
.SelectMany(sc => sc.Shapes.OfType<OvalShape>());
Given that some ShapeContainers are inside Panels, and others are not, the query gets a little uglier but not too bad. First we search the main Controls collection, then search all the child Panels, and concatenate the results.
var ovals = Controls.OfType<ShapeContainer>()
.SelectMany(sc => sc.Shapes.OfType<OvalShape>())
.Concat(Controls.OfType<Panel>()
.SelectMany(p => p.Controls.OfType<ShapeContainer>()
.SelectMany(sc => sc.Shapes.OfType<OvalShape>())));
I am attempting to dynamically add content to labels in WPF from an object in a list.
I can add the content from the Object fine by hard-coding the index number:
// assign forecast values to 5 user interface labels
Forecast fc1 = day.Weather.getForecastInList(0);
day1Label.Content = fc1.Day;
day1ConditionLabel.Content = fc1.ForecastCondition;
day1TempLabel.Content = formatHiLow(fc1.TemperatureHigh, fc1.TemperatureLow);
Forecast fc2 = day.Weather.getForecastInList(1);
day2Label.Content = fc2.Day;
day2ConditionLabel.Content = fc2.ForecastCondition;
day2TempLabel.Content = formatHiLow(fc2.TemperatureHigh, fc2.TemperatureLow);
etc...
But then I would have to repeat a lot of the assignment code for each of the 5 labels.
I have attempted to use a for() loop which works fine to get each of the objects from the list but won't let me rename the labels e.g.
// assign forecast values to 5 user interface labels
for (int i = 0; i < 5; i++ )
{
Forecast fc+(i+1) = day.Weather.getForecastInList(i);
day+(i+1)+Label.Content = fc+(i+1)+.Day;
}
How can I dynamically identify WPF labels using the index in a for() loop?
I don't recommend this but to answer your question, theres a possibility:
Create a list which contains all your labels:
List<Label> dayLabels = new List<Label>();
dayLabels.Add(day1Label);
dayLabels.Add(day2Label);
//and so on...
Loop through all the labels:
for (int i = 0; i < 5; i++ )
{
Forecast fc = day.Weather.getForecastInList(i);
dayLabels.ElementAt(i).Content = fc.Day;
}
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.