Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to add text block dynamically to another windows by an other window based on button click?
Posted
Updated 11-Jun-19 3:49am

1 solution

From here[^].
using System;
using System.Windows;
using System.Windows.Controls;

namespace AddControlsDynamically
{
    public partial class Window1 : Window
    {
        public void Window_Loaded(object sender, RoutedEventArgs e)
        {
            GenerateControls();
        }
        public void GenerateControls()
        {
            Button btnClickMe = new Button();
            btnClickMe.Content = "Click Me";
            btnClickMe.Name = "btnClickMe";
            btnClickMe.Click += new RoutedEventHandler(this.CallMeClick);
            someStackPanel.Children.Add(btnClickMe);
            TextBox txtNumber = new TextBox();
            txtNumber.Name = "txtNumber";
            txtNumber.Text = "1776";
            someStackPanel.Children.Add(txtNumber);
            someStackPanel.RegisterName(txtNumber.Name, txtNumber);
        }
        protected void CallMeClick(object sender, RoutedEventArgs e)
        {
            TextBox txtNumber = (TextBox) this.someStackPanel.FindName("txtNumber");
            string message = string.Format("The number is {0}", txtNumber.Text);
            MessageBox.Show(message);
        }
    }
}

You can easily add the created control to other window. It's pretty simple, Once you get this, you will get that ;)

-KR
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900