我正在开发windows形式的聊天应用程序,我正在richtextbox中写消息,但是当我点击发送按钮并在webbrowser控件中显示每条消息时。 .i想要动态添加webbrowser控件,因为send.i正在使用以下代码:
namespace WindowsFormsApplication1
{
[ComVisible(true)]
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// this.w1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(w1_DocumentCompleted);
}
WebBrowser w1 = new WebBrowser();
private void button1_Click(object sender, EventArgs e)
{
this.Controls.Add(w1);
HtmlElement div = w1.Document.GetElementById("abc");
div.InnerHtml = richTextBox1.Text;
}
private void Form1_Load(object sender, EventArgs e)
{
w1.Navigate(Path.Combine(Environment.CurrentDirectory, "HTMLPageForScripting.htm"));
}
}
}
答案 0 :(得分:0)
以下是您要求的案例:
Panel
以将浏览器置于其中并将其autoscroll属性设置为true; Click
事件中,创建浏览器,设置其宽度和高度,然后导航到所需的文档。DocumentCompleted
事件并在其中添加内容。我们在此处添加,因为我们应该确保已加载包含<div id="abc"></div>
的所有内容。以下是代码:
private void button1_Click(object sender, EventArgs e)
{
var browser = new WebBrowser();
browser.Height = 100;
browser.Dock = DockStyle.Top;
browser.Navigate(@"D:\test.html");
browser.DocumentCompleted += browser_DocumentCompleted;
this.panel1.Controls.Add(browser);
//Just do it to put it at the end of list and scroll to it
browser.BringToFront();
this.panel1.ScrollControlIntoView(browser);
}
void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
var browser = (WebBrowser)sender;
HtmlElement div = browser.Document.GetElementById("abc");
div.InnerHtml = richTextBox1.Text;
}