Imports C1.Win.C1FlexGrid
Imports C1.Win.C1SuperTooltip

Public Class Form1

    ' C1SuperLabel control used to render Html cells
    Dim _html As New C1.Win.C1SuperTooltip.C1SuperLabel()

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'adding the image in the Images property that will be used by the html <img> tag
        _html.Images.Add(New ImageEntry("select.ico", Image.FromFile("..\..\select.ico")))

        ' set up grid
        _flex.AutoResize = False
        _flex.AllowResizing = AllowResizingEnum.Both
        _flex.DrawMode = DrawModeEnum.OwnerDraw
        ''_flex.Rows.DefaultSize = 80
        _flex.Rows(0).Height = 20
        _flex.Cols(0).Width = 20
        _flex.ShowCursor = True
        _flex.Rows.MaxSize = 0

        ' create a data table with an HTML field
        Dim dt As New DataTable()
        dt.Columns.Add("HTML", GetType(String))
        dt.Columns.Add("HTML2", GetType(String))
        dt.Columns.Add("HTML3", GetType(String))
        dt.Columns.Add("HTML4", GetType(String))
        dt.Columns.Add("Plain Text", GetType(String))
        Dim i As Integer
        For i = 0 To 30
            dt.Rows.Add(GetHtml(8, "This is <u>underlined</u> text in an HTML string"), GetHtml(9, "This is <i>italic</i> text in an HTML string"), GetHtml(12, "This is <b>bold</b> text in an HTML string"), GetHtml(8, "This is <u>underlined</u> text in an HTML string"), "Plain text...")
            dt.Rows.Add(GetHtml(8, "This is <u>underlined</u> text in an HTML string"), GetHtml(9, "This is <i>italic</i> text in an HTML string"), GetHtml(9, "This is <i>italic</i> text in an HTML string"), GetHtml(9, "This is <i>italic</i> text in an HTML string"), "Plain text...")
            dt.Rows.Add(GetHtml(8, "This is <u>underlined</u> text in an HTML string"), GetHtml(12, "This is <b>bold</b> text in an HTML string"), GetHtml(12, "This is <b>bold</b> text in an HTML string"), GetHtml(12, "This is <b>bold</b> text in an HTML string"), "Plain text...")
        Next

        ' bind grid
        _flex.DataSource = dt

        ' make Html field conspicuous
        Dim colHtml As Column = _flex.Cols("HTML")
        ''colHtml.Width = 500

        ' use HtmlEditor class to edit values in the "C1Editor" column
        Dim editor As New HtmlEditor()
        editor.BorderStyle = BorderStyle.None
        colHtml.Editor = editor

        ' use a different backcolor
        colHtml.StyleNew.BackColor = Color.LightGoldenrodYellow
        _flex.BackColor = Color.Bisque

    End Sub

    Function GetHtml(ByVal fontSize As Single, ByVal html As String) As String
        Dim bgColor As String = ColorTranslator.ToHtml(Color.LightGoldenrodYellow)

        'using the <img> tag in html code and setting its source to image key
        Return String.Format(
                   "<html><body style='margin:0pt;padding:0pt;font-family:{0};font-size:{1}pt;background-color:{2}'><img src='res://select.ico' height=15 width=15><p>{3}</p></body></html>",
                   Font.Name,
                   fontSize,
                   bgColor,
                   html)
    End Function

    Private Sub _flex_OwnerDrawCell(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.OwnerDrawCellEventArgs) Handles _flex.OwnerDrawCell
        If (Me._flex.Cols.Item(e.Col).Name = "HTML") Or _flex.Cols.Item(e.Col).Name = "HTML2" Or _flex.Cols.Item(e.Col).Name = "HTML3" Or _flex.Cols.Item(e.Col).Name = "HTML4" Then


            ' draw background
            e.DrawCell(DrawCellFlags.Background)

            ' use the C1SuperLabel to draw the html text
            If ((e.Bounds.Width > 0) AndAlso (e.Bounds.Height > 0)) Then
                Me._html.Text = Me._flex.GetDataDisplay(e.Row, e.Col)
                Me._html.BackColor = Color.Transparent
                Me._html.DrawToGraphics(e.Graphics, e.Bounds)

                'update the row height
                _flex.Rows(e.Row).Height = GetCellHeight(e.Row, e.Col)

            End If

            ' and draw border last
            e.DrawCell(DrawCellFlags.Border)

            ' we're done with this cell
            e.Handled = True
        End If
    End Sub

    ''' <summary>
    ''' Calculate the HTML content height
    ''' </summary>
    Private Function GetCellHeight(row As Integer, col As Integer) As Integer
        Using html As New C1SuperLabel
            html.Text = _flex.GetDataDisplay(row, col)
            html.Width = _flex.Cols(col).WidthDisplay
            Return html.Measure(_flex.Cols(col).WidthDisplay).Height + (_flex.GetCellStyleDisplay(row, col).Border.Width * 2)
        End Using
    End Function
End Class
