How to Place Text from a Textbox on a Label? C# WPF

Asked

Viewed 1,507 times

3

I am developing an application in C# WPF, and I have two Forms.

A Form has several Abels (with the name of several fields, such as gender age, name, etc...) and under each of the Abels has a Textbox to insert a value for the same.

In the following form I have again a set of Labels with the name of the fields, and below each label I want to have another label with the value that was entered in the respective textbox in the previous form, that is to say 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: values

public class AtributosValores
{

    public string Name { get; set; }
    public string Value { get; set; }
}

Basically I want to present in the second set of Labels of the 2nd Form this Value, ie the Value entered in the Text Box

I create the 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!

1 answer

1


Create a list of objects from the Attribute valuesclass, scroll through the Form by locating the Textbox, and add the objects to the list. Pass the List to Form2, and scroll through creating the fields. Use the Textbox 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);
        }
    }
}

Follow 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();
        }
  • I think you edited while you were still responding

  • I edited the answer to suit your edition.

  • is a public List<Attributesvalues> Values2 = new List<Attributesvalues>(); but within Form2

  • in the Load Form 2 event, you scroll through this list to create the fields

  • Ctrl. Controls with S at the end

  • ok, I’m talking about Forms and vc of Window (WPF), a moment

  • put the code to WPF

  • any more code ?

  • I don’t know if there’s direct chat here, but call on Facebook

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.