Reputation: 586
Lately, I've been using this to iterate through some controls in for loops
for (int i = 1; i <= 4; i++)
{
Controls["label" + i].Text = "I am label " + i;
}
This sets all controls with the name label, and numbers 1-4 to its corresponding text.
I also found that you can iterate like this for Properties.Settings.Default["example"], etc
.
Is it possible for to go through the names of elements in an array of public static booleans from another form and how would I do this? If this sounds confusing, this is an example of what I'm somewhat looking for;
Form1:
public static bool[] T1Cover = new bool[2];
public static bool[] T1Supplied = new bool[2];
public static bool[] T2Cover = new bool[2];
public static bool[] T2Supplied = new bool[2];
T1Cover[0] = false;
T1Cover[1] = true;
T2Cover[0] = false;
T2Cover[1] = false;
T1Supplied[0] = false;
T1Supplied[1] = false;
T2Supplied[0] = true;
T2Supplied[1] = false;
Form2:
if (Form1.T1Cover[i] || Form1.T1Supplied[i])
{
Controls["lblT1P" + a + "A"].Text = Form1.T1Coverers[i];
}
I want to put the above from Form2 into a for loop, where I go through the different array names, and check if an index of the different arrays are true.
An I simply missing something?
Let me know if I can add something for further clarification.
Upvotes: 1
Views: 82
Reputation: 125197
As an option you can create a Model
class containing Cover
, Supplied
and Coverer
properties. Then you can have a List<List<Model>>
to store values. Then you can simply use a for
loop to access values.
Here is a sample implementation. You can use this idea for your requirement.
Add this class to the project:
public class Model
{
public bool Cover { get; set; }
public bool Supplied { get; set; }
public string Coverers { get; set; }
}
Define this member in Form1:
public static List<List<Model>> Data = new List<List<Model>>()
{
new List<Model>()
{
new Model{Cover= false, Supplied= false, Coverers= "A"},
new Model{Cover= true, Supplied= false, Coverers= "B"},
},
new List<Model>()
{
new Model{Cover= false, Supplied= true, Coverers= "C"},
new Model{Cover= false, Supplied= false, Coverers= "D"},
},
};
Then you can use such loop in Form2:
for (int i = 0; i < Form1.Data.Count; i++)
{
for (int j = 0; j < Form1.Data[i].Count; j++)
{
if (Form1.Data[i][j].Cover || Form1.Data[i][j].Supplied)
this.Controls[string.Format("lblT{0}P{1}A", i+1, j+1)]
.Text= Form1.Data[i][j].Coverers;
}
}
Upvotes: 1
Reputation:
Is it possible for to go through the names of elements in an array of public static booleans from another form?
Essentially your question is "can I access public members in another class?" - to which the answer is yes, because they are public
. This operation is made easier because they are also static
.
Upvotes: 0