none
Document.ExecCommand 未将对象引用设置到对象的实例 RRS feed

  • 问题

  • 程序里要用一个winform下的html编辑器控件,于是自己动手制作。
    但是
    对WebBrowser 执行 Document.ExecCommand 会出现未将对象引用设置到对象的实例
    下面是基本的一写代码,奇怪的是,单独在Visual Studio 里测试可以,但是一放到窗体里就出错误。


    Public Class HtmEdit
        Dim updatingFontName As Boolean = False
        Dim updatingfontSize As Boolean = False
        Private myDocumentText As String
        Private myDesignType As Boolean = True
    
        Private Sub HtmEdit_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            wbsHtmlEdit.DocumentText = myDocumentText
            wbsHtmlEdit.Document.ExecCommand("EditMode", False, "")
            wbsHtmlEdit.Document.ExecCommand("LiveResize", False, "")
            AddHandler wbsHtmlEdit.Document.ContextMenuShowing, AddressOf Document_ContextMenuShowing
    
            Dim ac As AutoCompleteStringCollection = New AutoCompleteStringCollection
    
            For Each Font As FontFamily In FontFamily.Families
                tscbFont.Items.Add(Font.Name)
                ac.Add(Font.Name)
            Next
    
            tscbFont.AutoCompleteMode = AutoCompleteMode.Suggest
            tscbFont.AutoCompleteSource = AutoCompleteSource.CustomSource
            tscbFont.AutoCompleteCustomSource = ac
    
            tscbFont.Text = "Times New Roman"
            tscbFontSize.Text = 3
    
            For i As Integer = 1 To 7
                tscbFontSize.Items.Add(i.ToString())
            Next
    
    
        End Sub
    
        Private Sub tsbDesignType_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbDesignType.Click
            DesignModeChange()
        End Sub
    
        Private Sub tscbFont_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles tscbFont.TextChanged
            If updatingFontName = False Then
                wbsHtmlEdit.Document.ExecCommand("FontName", False, tscbFont.Text)
            End If
        End Sub
    
        Private Sub txtHtmlSource_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtHtmlSource.TextChanged
            wbsHtmlEdit.Document.Body.InnerHtml = txtHtmlSource.Text
        End Sub
    
        Private Sub tscbFontSize_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tscbFontSize.KeyPress
            If Char.IsNumber(e.KeyChar) Then
                e.Handled = True
                If e.KeyChar.ToString <= 7 And e.KeyChar.ToString > 0 Then
                    tscbFontSize.Text = e.KeyChar.ToString()
                End If
            ElseIf Char.IsControl(e.KeyChar) = False Then
                e.Handled = True
            End If
        End Sub
    
        Private Sub tscbFontSize_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles tscbFontSize.TextChanged
            If updatingfontSize = False Then
                wbsHtmlEdit.Document.ExecCommand("FontSize", False, tscbFontSize.Text)
            End If
        End Sub
    
        Private Sub tsbBold_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbBold.Click
            wbsHtmlEdit.Document.ExecCommand("Bold", False, "")
        End Sub
    
        Private Sub tsbItalic_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbItalic.Click
            wbsHtmlEdit.Document.ExecCommand("Italic", False, "")
        End Sub
    
        Private Sub tsbUnderline_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbUnderline.Click
            wbsHtmlEdit.Document.ExecCommand("Underline", False, "")
        End Sub
    
        Private Sub tsbFontColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbFontColor.Click
            Dim FontColor As String = wbsHtmlEdit.Document.DomDocument.queryCommandValue("ForeColor").ToString
    
            If FontColor = "" Then
                FontColor = "000000"
            End If
    
            If FontColor.StartsWith("#") Then
                FontColor = Convert.ToInt32(FontColor.Substring(1), 16)
            Else
                FontColor = Convert.ToInt32(FontColor)
            End If
    
            Dim dlg As New ColorDialog
            Dim c As Color = Color.FromArgb(FontColor)
    
            dlg.Color = Color.FromArgb(c.B, c.G, c.R)
    
            If dlg.ShowDialog() = DialogResult.OK Then
                Dim color As String = dlg.Color.Name
                color = String.Format("#{0:X2}{1:X2}{2:X2}", dlg.Color.R, dlg.Color.G, dlg.Color.B)
                wbsHtmlEdit.Document.ExecCommand("ForeColor", False, color)
            End If
        End Sub
    
        Private Sub tsbBackColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbBackColor.Click
            Dim BackColor As String = wbsHtmlEdit.Document.DomDocument.queryCommandValue("BackColor").ToString
    
            If BackColor = "" Then
                BackColor = "16777215"
            End If
    
            If BackColor.StartsWith("#") Then
                BackColor = Convert.ToInt32(BackColor.Substring(1), 16)
            Else
                BackColor = Convert.ToInt32(BackColor)
            End If
    
            Dim dlg As New ColorDialog
            Dim c As Color = Color.FromArgb(BackColor)
    
            dlg.Color = Color.FromArgb(c.B, c.G, c.R)
    
            If dlg.ShowDialog() = DialogResult.OK Then
                Dim color As String = dlg.Color.Name
                color = String.Format("#{0:X2}{1:X2}{2:X2}", dlg.Color.R, dlg.Color.G, dlg.Color.B)
                wbsHtmlEdit.Document.ExecCommand("BackColor", False, color)
            End If
        End Sub
    
        Private Sub tsbLine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbLine.Click
            wbsHtmlEdit.Document.ExecCommand("InsertHorizontalRule", False, "")
        End Sub
    
        Private Sub tsbCreateLink_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbCreateLink.Click
            wbsHtmlEdit.Document.ExecCommand("CreateLink", True, "")
        End Sub
    
        Private Sub tsbInsertImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbInsertImage.Click
            wbsHtmlEdit.Document.ExecCommand("InsertImage", True, "")
        End Sub
    
        Private Sub tsmiCut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsmiCut.Click
            wbsHtmlEdit.Document.ExecCommand("Cut", False, "")
        End Sub
    
        Private Sub tsmiCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsmiCopy.Click
            wbsHtmlEdit.Document.ExecCommand("Copy", False, "")
        End Sub
    
        Private Sub tsmiPaste_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsmiPaste.Click
            wbsHtmlEdit.Document.ExecCommand("Paste", False, Clipboard.GetDataObject())
        End Sub
    
        Private Sub tsmiDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsmiDelete.Click
            wbsHtmlEdit.Document.ExecCommand("Delete", False, "")
        End Sub
    
        Private Sub tsbJustifyLeft_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbJustifyLeft.Click
    
        End Sub
    
        Private Sub tsbJustifyCenter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbJustifyCenter.Click
    
        End Sub
    
        Private Sub tsbJustifyRight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbJustifyRight.Click
    
        End Sub
    
        Private Sub tsbJustifyFull_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbJustifyFull.Click
    
        End Sub
    
        Private Sub tsbInsertOrderedList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbInsertOrderedList.Click
    
        End Sub
    
        Private Sub tsbInsertUnorderedList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbInsertUnorderedList.Click
    
        End Sub
    
        Private Sub tsbOutdent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbOutdent.Click
    
        End Sub
    
        Private Sub tsbIndent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbIndent.Click
    
        End Sub
    
        Private Sub wbsHtmlEdit_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles wbsHtmlEdit.GotFocus
            tmrUpdateStats.Enabled = True
        End Sub
    
        Private Sub tmrUpdateStats_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrUpdateStats.Tick
            If Not tscbFont.Focused AndAlso Not wbsHtmlEdit.Document.DomDocument.queryCommandValue("FontName") Is DBNull.Value Then
                Dim FontName As String = wbsHtmlEdit.Document.DomDocument.queryCommandValue("FontName")
                If tscbFont.Text <> FontName Then
                    updatingFontName = True
                    tscbFont.Text = FontName
                    updatingFontName = False
                End If
            End If
    
            If Not tscbFontSize.Focused AndAlso Not wbsHtmlEdit.Document.DomDocument.queryCommandValue("FontSize") Is DBNull.Value Then
                Dim FontSize As String = wbsHtmlEdit.Document.DomDocument.queryCommandValue("FontSize")
                If tscbFont.Text <> FontSize Then
                    updatingfontSize = True
                    tscbFontSize.Text = FontSize
                    updatingfontSize = False
                End If
            End If
    
            If Not wbsHtmlEdit.Document.DomDocument.queryCommandValue("Bold") Is DBNull.Value Then
                tsbBold.Checked = wbsHtmlEdit.Document.DomDocument.queryCommandValue("Bold")
            End If
    
            If Not wbsHtmlEdit.Document.DomDocument.queryCommandValue("Italic") Is DBNull.Value Then
                tsbItalic.Checked = wbsHtmlEdit.Document.DomDocument.queryCommandValue("Italic")
            End If
    
            If Not wbsHtmlEdit.Document.DomDocument.queryCommandValue("Underline") Is DBNull.Value Then
                tsbUnderline.Checked = wbsHtmlEdit.Document.DomDocument.queryCommandValue("Underline")
            End If
    
            If wbsHtmlEdit.Focused = False Then
                tmrUpdateStats.Enabled = False
    
            End If
        End Sub
    
        Private Sub Document_ContextMenuShowing(ByVal sender As System.Object, ByVal e As HtmlElementEventArgs)
            e.ReturnValue = False
    
            tsmiCut.Enabled = wbsHtmlEdit.Document.DomDocument.queryCommandEnabled("Cut")
            tsmiCopy.Enabled = wbsHtmlEdit.Document.DomDocument.queryCommandEnabled("Copy")
            tsmiPaste.Enabled = wbsHtmlEdit.Document.DomDocument.queryCommandEnabled("Paste")
            tsmiDelete.Enabled = wbsHtmlEdit.Document.DomDocument.queryCommandEnabled("Delete")
            cmsHtmlMenu.Show(wbsHtmlEdit, e.ClientMousePosition)
        End Sub
    
        Private Sub DesignModeChange()
            If tsbDesignType.Checked Then
                txtHtmlSource.Text = wbsHtmlEdit.Document.Body.InnerHtml
                wbsHtmlEdit.Visible = False
                txtHtmlSource.Visible = True
            Else
                wbsHtmlEdit.Document.Body.InnerHtml = txtHtmlSource.Text
                wbsHtmlEdit.Visible = True
                txtHtmlSource.Visible = False
            End If
        End Sub
    
        Public Property DesignType() As Boolean
            Get
                Return myDesignType
            End Get
            Set(ByVal value As Boolean)
                myDesignType = value
                If myDesignType = True Then
                    tsbDesignType.Checked = False
                Else
                    tsbDesignType.Checked = True
                End If
    
                DesignModeChange()
            End Set
        End Property
    
        Public Property DocumentText() As String
            Get
                Return myDocumentText
            End Get
            Set(ByVal value As String)
                myDocumentText = value
                wbsHtmlEdit.DocumentText = myDocumentText
            End Set
        End Property
    End Class
    

    I am Rayman Zhai
    2009年9月4日 6:57

答案

  • 这个我试过的,放在前面还好,在调试中能运行,
    如果放在之后,连调试都不行了。一运行所有的就全提示 未将对象引用设置到对象的实例


    I am Rayman Zhai
    • 已标记为答案 Rayman Zhai 2009年10月17日 12:14
    2009年9月4日 7:08

全部回复