Show File Content
Displays the contents of multiple files in a custom form, with associated captions.
- Instantiates a new FormViewContentEx using the provided file paths and captions.
- Applies the optional form title and icon if provided.
- Displays the form in non-modal mode using Show().
Notes:
- If the content is recognized as HTML, it’s shown in a WebBrowser object
- The number of items in fileList and captionList should match to ensure proper display.
- Each file in fileList should exist and be readable; otherwise, the form may show an error or fallback message.
The wrapper (below the form):
VB
Public Sub ShowFileContent(fileList As List(Of String),
captionList As List(Of String),
Optional formTitle As String = "",
Optional formIcon As Icon = Nothing,
Optional layoutKey As String = "")
Dim frm As New FormViewContentEx(fileList, captionList, formTitle, formIcon, layoutKey)
frm.Show()
End SubThe form FormViewContentEx:
VB
Imports System.Drawing
Imports System.IO
Imports System.Reflection
Imports System.Windows.Forms
Public Class FormViewContentEx
Public Property LayoutKey As String = ""
Private ReadOnly MinWidth As Integer = 500
Private ReadOnly MinHeight As Integer = 300
Private WithEvents TabControlContent As New TabControl()
Private WithEvents RichTextBox As New RichTextBox()
Private WithEvents WebBrowser As New WebBrowser()
Private pFileList As New List(Of String)
Private pCaptionList As New List(Of String)
Private pFormTitle As String = ""
Private pFormIcon As System.Drawing.Icon = Nothing
Private TabHeight As Integer = 33
Public Sub New(fileList As List(Of String),
captionList As List(Of String),
Optional formTitle As String = "",
Optional formIcon As Drawing.Icon = Nothing,
Optional szLayoutKey As String = "")
Me.DoubleBuffered = True
LayoutKey = szLayoutKey
pFileList = fileList
pCaptionList = captionList
pFormTitle = formTitle
pFormIcon = formIcon
' This call is required by the designer.
InitializeComponent()
If formIcon IsNot Nothing Then
Me.Icon = formIcon
Else
Me.Icon = iuCommonResources.GetIcon("SilverIceLogo")
End If
If Not String.IsNullOrWhiteSpace(formTitle) Then
Me.Text = formTitle
Else
Dim callingAssembly = Assembly.GetCallingAssembly()
If callingAssembly IsNot Nothing Then
Dim attr = CType(callingAssembly.GetCustomAttribute(GetType(AssemblyProductAttribute)), AssemblyProductAttribute)
Me.Text = attr?.Product
End If
End If
InitializeUI()
End Sub
Private Sub FormViewContentEx_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If pFileList.Count = 0 Then
MessageBox.Show("Specify at least one content to show!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Me.Close()
Return
End If
RestoreLayout(Me)
ShowContent(pFileList(0))
End Sub
Private Sub FormViewContentEx_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
SaveLayout(Me)
End Sub
Private Sub InitializeUI()
' TabControl setup
TabControlContent.Dock = DockStyle.Top
TabControlContent.Height = TabHeight
TabControlContent.Padding = New Point(15, 7)
TabControlContent.Margin = New Padding(4, 3, 4, 3)
TabControlContent.Appearance = TabAppearance.Normal
TabControlContent.SizeMode = TabSizeMode.Normal
TabControlContent.DrawMode = TabDrawMode.OwnerDrawFixed
'TabControlContent.Font = New System.Drawing.Font("Consolas", 10.0F, Drawing.FontStyle.Regular, Drawing.GraphicsUnit.Point, CByte(0))
TabControlContent.Font = New Font("Microsoft Sans Serif", 9.0F, FontStyle.Regular, GraphicsUnit.Point, CByte(0))
AddHandler TabControlContent.DrawItem, AddressOf TabControlContent_DrawItem
Me.Controls.Add(TabControlContent)
' Viewer shared (below tabs)
'RichTextBox.Dock = DockStyle.Fill
RichTextBox.Visible = False
RichTextBox.Font = New System.Drawing.Font("Consolas", 11.0F, Drawing.FontStyle.Regular, Drawing.GraphicsUnit.Point, CByte(0))
RichTextBox.BackColor = Drawing.Color.Black
RichTextBox.ForeColor = Drawing.Color.Silver
RichTextBox.ReadOnly = True
Me.Controls.Add(RichTextBox)
'WebBrowser.Dock = DockStyle.Fill
WebBrowser.Visible = False
WebBrowser.ScriptErrorsSuppressed = True
Me.Controls.Add(WebBrowser)
' Add tabs
For i = 0 To pFileList.Count - 1
Dim caption = If(pCaptionList.Count > i, pCaptionList(i), $"File {i + 1}")
Dim tp As New TabPage(caption) With {
.Tag = pFileList(i),
.Font = New Font(TabControlContent.Font, FontStyle.Regular)
}
TabControlContent.TabPages.Add(tp)
Next
AddHandler TabControlContent.SelectedIndexChanged, AddressOf TabControlContent_SelectedIndexChanged
End Sub
Private Sub TabControlContent_SelectedIndexChanged(sender As Object, e As EventArgs)
Dim tab = TabControlContent.SelectedTab
If tab IsNot Nothing AndAlso tab.Tag IsNot Nothing Then
ShowContent(tab.Tag.ToString())
End If
End Sub
Private Sub ShowContent(path As String)
If String.IsNullOrWhiteSpace(path) Then Exit Sub
Try
If path.Contains("://") Then
WebBrowser.Url = New Uri(path)
WebBrowser.Visible = True
RichTextBox.Visible = False
ElseIf File.Exists(path) Then
Dim textContent As String = File.ReadAllText(path)
If IsHtmlContent(textContent) Then
WebBrowser.Url = New Uri("file:///" & path.Replace("\", "/"))
WebBrowser.Visible = True
RichTextBox.Visible = False
Else
RichTextBox.Text = textContent
RichTextBox.Visible = True
WebBrowser.Visible = False
End If
Else
MessageBox.Show($"File not found: {path}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
Catch ex As Exception
MessageBox.Show($"Error loading content: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub FormViewContentEx_Resize(sender As Object, e As EventArgs) Handles MyBase.Resize
If Me.Width < MinWidth Then Me.Width = MinWidth
If Me.Height < MinHeight Then Me.Height = MinHeight
ResizeViewer()
End Sub
Private Sub ResizeViewer()
Dim h As Integer
Dim w As Integer
w = Me.Width
h = Me.Height - TabHeight
RichTextBox.Width = w
RichTextBox.Height = h
RichTextBox.Top = TabHeight
WebBrowser.Width = w
WebBrowser.Height = h
WebBrowser.Top = TabHeight
End Sub
Private Sub TabControlContent_DrawItem(sender As Object, e As DrawItemEventArgs)
Dim g As Graphics = e.Graphics
Dim tabPage = TabControlContent.TabPages(e.Index)
Dim tabBounds = TabControlContent.GetTabRect(e.Index)
' Check if tab is selected
Dim isSelected As Boolean = (e.Index = TabControlContent.SelectedIndex)
' Custom colors
Dim backColor As Color = If(isSelected, Color.FromArgb(255, 255, 192), SystemColors.Control)
Dim textColor As Color = If(isSelected, Color.DarkRed, Color.Black)
Dim fontToUse As Font = If(isSelected, New Font(TabControlContent.Font, FontStyle.Bold), TabControlContent.Font)
' Background
Using b As New SolidBrush(backColor)
g.FillRectangle(b, tabBounds)
End Using
' Use TextRenderer for native-looking text (GDI)
TextRenderer.DrawText(g, tabPage.Text, fontToUse, tabBounds, textColor,
TextFormatFlags.HorizontalCenter Or TextFormatFlags.VerticalCenter)
' Optional border
ControlPaint.DrawBorder(g, tabBounds, SystemColors.ControlDark, ButtonBorderStyle.Solid)
End Sub
End Class
The FormViewContentEx.Designer
VB
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()>
Partial Class FormViewContentEx
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()>
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()>
Private Sub InitializeComponent()
SuspendLayout()
'
' FormViewContentEx
'
AutoScaleDimensions = New System.Drawing.SizeF(7F, 15F)
AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
ClientSize = New System.Drawing.Size(1217, 537)
Margin = New System.Windows.Forms.Padding(4, 3, 4, 3)
Name = "FormViewContentEx"
Text = "Show File Content"
ResumeLayout(False)
End Sub
End Class