-2

I am trying to create a table with check marks and WPF controls (a range slider with two textblocks). WPF controls is written using Avalon library and I successfully added that to my table. But, when the form is closed, I need to get the texts from textblocks and apply that somewhere. I can iterate through the controls on the form, and find element host, but I don't know how to extract the value from two text blocks on the form. My code is attached below. Can you please help me with this?

Code:

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public static TableLayoutPanel _T;
        static void set_globalvalue(TableLayoutPanel val)
        {
            _T = val;
        }

        public Form1()
        {
            InitializeComponent();

            TableLayoutPanel TP = new TableLayoutPanel();
            TP.ColumnCount = 2;
            TP.RowCount = 5;
            TP.BackColor = Color.White;
            this.Controls.Add(TP);
            TP.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
            int size = 0;
            for (int i = 0; i < TP.RowCount; i++)
            {
                ElementHost host = new ElementHost();
                UserControl1 uc = new UserControl1(1,15);
                host.Name = i.ToString();
                host.Dock = DockStyle.Top;
                host.Child = uc;
                TP.Controls.Add(host, 1, i);
                host.Width = 270;
                host.Height = 40;
                CheckBox ch = new CheckBox();
                ch.Name = i.ToString() + "_che";
                TP.Controls.Add(ch, 0, i);
                size = size + host.Height+20;
            }

            TP.Height = size;
            TP.Width = 700;
            set_globalvalue(TP);
            this.FormClosing += Form1_FormClosing;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            for (int i = 1; i <= _T.ColumnCount; i++)
            {
                for (int j = 0; j <= _T.RowCount; j++)
                {
                    Control c = _T.GetControlFromPosition(i, j);

                    if (c != null & c is System.Windows.Forms.Integration.ElementHost)
                    {
                        ElementHost host =c as ElementHost;
                        System.Windows.UIElement u = host.Child;
                          ???????????????????
                    }
                }
            }
        }
    }
} 
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Andy
  • 3
  • 1
  • That is not a question, that's a bunch of tags. – H.B. Jul 16 '12 at 16:56
  • so what do you suggest for title? – Andy Jul 16 '12 at 16:57
  • 2
    It's not my job to suggest better titles for you, it's your question, you think of something. – H.B. Jul 16 '12 at 16:59
  • 2
    It's unclear what part you're having trouble with. Right now it just looks like you're asking us to code it for you. – N_A Jul 16 '12 at 17:27
  • 2
    @H.B. indeed, it's our job, all of us :), although the initial post is relatively unclear and the title is poor. Surprising comment for such a famous user :). – Jean-Rémy Revy Jul 16 '12 at 17:37
  • @Andy try at least to explain us what is your problem. Your title must reflect the meaning of your question ... Are you, yourself, sure of what you want to do ? You don't even ask anything except "Help me". Try to be more constructive if you really want we answer you. – Jean-Rémy Revy Jul 16 '12 at 17:38
  • @Jean-Rémy: It's our job to maintain the site, so if someone makes a mess why not ask that someone to clean it up? I do my part, as i edit slightly malformed titles, fix tags and formatting; however i cannot be asked to be creative and come up with a good title from scratch, *i am not the one asking for help here*, people need to do their part as well. – H.B. Jul 16 '12 at 17:54
  • I am sorry, this was my first post on this website, I will improve it. – Andy Jul 16 '12 at 18:30
  • For a title, something like "How do I access child of ElementHost from WinForms?" – RQDQ Jul 16 '12 at 18:47

1 Answers1

1

Once you have the reference to the element host, you can do something along these lines:

ElementHost host = c as ElementHost;

UserControl1 uc = host.Child as UserControl1;

if (uc != null)
{
   //Get the text from uc
}
RQDQ
  • 15,461
  • 2
  • 32
  • 59