onChange动态创建的元素

时间:2019-06-14 16:02:07

标签: c# richtextbox

我动态地创建了2个元素。现在,我想在事件onChange上将数据从richTextBox复制到WebBrowser

动态创建数据时如何将数据从richTextBox复制到WebBrowser?

我的代码

void OpenFile(string path, string filename)
        {
            StreamReader read = new StreamReader(path);

            if(richTextBox1.Text.Length == 0)
            {
                return;
            }
            else
            {
                TabPage tp = new TabPage();
                int tc = tabControl1.TabCount + 1;
                tp.Text = filename;
                tabControl1.TabPages.Add(tp);

                RichTextBox rtb = new RichTextBox();
                WebBrowser wb = new WebBrowser();
                rtb.Width = tp.Width / 2;
                rtb.Height = tp.Height;
                rtb.TextChanged += new System.EventHandler(setText);
                rtb.Text = read.ReadToEnd();

                wb.Left = tp.Width / 2;
                wb.Width = tp.Width / 2;
                wb.Height = tp.Height;

                tp.Controls.Add(rtb);
                tp.Controls.Add(wb);

                return;
            }
        }

        private void setText(object sender, EventArgs e)
        {
            MessageBox.Show("TEXT CHANGED");
        }

1 个答案:

答案 0 :(得分:2)

尝试一下

rtb.TextChanged += (s, e) => { wb.DocumentText = rtb.Text; };