HTML 文字列を含む datagridview があります。CellDoubleClick イベントを使用して、HTML 文字列を WebBrowser コントロールに表示しています。
Form1 で
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
try
{
if (e.ColumnIndex != 0 && e.RowIndex != -1)
{
string s = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
this.f2 = new Form2(s);
f2.ShowDialog();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Form2 で
private IHTMLDocument2 doc;
string reply;
public Form2(string reply)
{
InitializeComponent();
this.reply = reply;
}
private void Form2_Load(object sender, EventArgs e)
{
webBrowser1.DocumentText = reply; <--- string from DataGridView
IHTMLTxtRange range = doc.selection.createRange() as IHTMLTxtRange;
range.pasteHTML(webBrowser1.DocumentText);
range.collapse(false);
range.select();
doc = webBrowser1.Document.DomDocument as IHTMLDocument2;
doc.designMode = "On";
}
上記のコードを使用すると、HTML 文字列をプレーンテキストとして正常に表示できますが、編集することはできません。または、このコードを使用する場合:
private IHTMLDocument2 doc;
private void Form2_Load(object sender, EventArgs e)
{
webBrowser1.DocumentText = reply; <--- string from DataGridView
doc = webBrowser1.Document.DomDocument as IHTMLDocument2;
doc.designMode = "On";
IHTMLTxtRange range = doc.selection.createRange() as IHTMLTxtRange;
range.pasteHTML(webBrowser1.DocumentText);
range.collapse(false);
range.select();
}
空白のフォームになりますが、書き込めるようになります。
range.pasteHTML(webBrowser1.DocumentText);
Form2_Load メソッドに関係していると感じていますが、Form2 を開いたときに DataGridView から HTML 文字列を表示できる他の方法を知りません。
ユーザーが HTML 文字列をプレーンテキストとして編集できるようにしたいと考えています (その後、HTML に変換されて datagridview に表示されます)。