Question:
I'm developing an application in C# WPF, and I have two forms.
A Form has several labels (with the name of several fields, such as age, gender, name, etc…) and under each of the labels there is a TextBox to insert a value for it.
In the following form I have again a set of labels with the names of the fields, and below each label I want to have another label with the value that was introduced in the respective textBox in the previous Form, that is, this has to be achieved dynamically.
Does anyone have any idea how I can do this?
I have as auxiliary classes for the fields:
class:attributes
public class atributos
{
public string Name { get; set; }
public int atributoID { get; set; }
}
class: valores
public class AtributosValores
{
public string Name { get; set; }
public string Value { get; set; }
}
Basically I want to present this Value in the second set of labels of the 2nd Form, that is, the Value introduced in the Text Box
I create textBox and Labels dynamically too:
public static Label createNewLabelSize (string name, int content, HorizontalAlignment horizontal, VerticalAlignment vertical, int width, int height)
{
//Create a new label
Label newLabel = new Label();
//Defines the new label characteristics
newLabel.Name = name;
newLabel.Content = content;
newLabel.HorizontalAlignment = horizontal;
newLabel.VerticalAlignment = vertical;
newLabel.Width = width;
newLabel.Height = height;
return newLabel;
}
So things like label1.text = textbox2.text… don't work!
Answer:
Create a list of objects of the AttributesValues class, traverse the Form finding the TextBox, and add the objects to the list. Pass the List to Form2, and cycle through creating the fields. Use the TextBox's Name property as the attribute name (which is displayed in the Label).
Example:
public List<AtributosValores> ValoresForm1 = new List<AtributosValores>();
private void button1_Click(object sender, EventArgs e)
{
LerValores(this);
Form2 form = new Form2();
form.ValoresForm2 = this.ValoresForm1;
form.Show();
}
private void LerValores(Control ctrl)
{
foreach (Control c in ctrl.Controls)
{
if (c.HasChildren)
{
LerValores(c);
}
else if (c is TextBox)
{
AtributosValores obj = new AtributosValores();
obj.Name = c.Name;
obj.Value = ((TextBox)c).Text;
ValoresForm1.Add(obj);
}
}
}
Here's the code changed to WPF:
public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
if (depObj != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
if (child != null && child is T)
{
yield return (T)child;
}
foreach (T childOfChild in FindVisualChildren<T>(child))
{
yield return childOfChild;
}
}
}
}
public List<AtributosValores> ValoresForm1 = new List<AtributosValores>();
private void LerValores(ContentControl ctrl)
{
foreach (TextBox tb in FindVisualChildren<TextBox>(ctrl))
{
AtributosValores obj = new AtributosValores();
obj.Name = tb.Name;
obj.Value = tb.Text;
ValoresForm1.Add(obj);
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
LerValores(this);
Form2 form = new Form2();
form.ValoresForm2 = this.ValoresForm1;
form.Show();
}