home


VB .NET Frequently Asked Questions

Automatically Resizing DataGrid Columns
One of the problem with the DataGrid in VB .NET is that no method exists that can be called to force the individual columns widths of a DataGrid to be sized according to the column header and size of the data. The DataGrid does have a property called 'PreferredColumnWidth', however with 'PreferredColumnWidth ' this is applied to all the columns and also does not take into account the size of the data.

To resolve this problem I have created the method below that sizes the widths of the individual columns according to the column header width and also the width of the data. The method simply sets the width of a given column to the maximum width required to displayed the data in the column.

It should be noted that when manually resizing a column on a datagrid, and if the mouse is double clicked that the column automatically sets itself to the correct size. Should anyone please know how to programmable do this, PLEASE LET ME KNOW. Email me at EmailUs@MSDE.BIZ, Thanks.

Public Class MyUtils_DataGrid
'Method: DataGridApplyAutomaticWidths
'Parameters
' DataGrid=DataGrid that is to be formatted.
' NumberOfRowsToScan=Number of data records to be scanned in order to compute the columns widths (Recommend 20).
' MaxPixelWidth=Maximum allowable pixel width of a column.
'
'Takes a DataGrid that should be already assigned a populated dataset,
'and formats the DataGrid so that the column widths reflect the widths of the data.
'This is accomplished by dynamically creating a single DataGridTableStyle and multiple DataGridTextBoxColumn(s).
'For each data column, the width of the column is set to the highest data width within the column.
'In order to reduce computation time where large datasets exist,
'a parameter exists to define how many rows are scanned.
'I would suggest that only the first 20 rows are scanned,
Public Shared Function DataGridApplyAutomaticWidths _
(ByVal DataGrid As System.Windows.Forms.DataGrid, ByVal NumberOfRowsToScan As Integer, ByVal MaxPixelWidth As Integer) _
As DataGridTableStyle
'Create graphics object for measuring widths.
Dim Graphics As Graphics = DataGrid.CreateGraphics()
'Define new table style.
Dim TableStyle As DataGridTableStyle = New DataGridTableStyle()
Try
Dim DataTable As DataTable
'DataTable = DataGrid.DataSource.DataSet.Tables(0)
DataTable = DataGrid.DataSource.Tables(0)
'Can only scan rows if they exist.
NumberOfRowsToScan = System.Math.Min(NumberOfRowsToScan, DataTable.Rows.Count)
'Clear any existing table styles.
DataGrid.TableStyles.Clear()
'Use mapping name that is defined in the data source.
TableStyle.MappingName = DataTable.TableName
'Now create the column styles within the table style.
Dim Column As DataColumn
Dim ColumnStyle As DataGridTextBoxColumn
Dim Width As Integer
For Each Column In DataTable.Columns
ColumnStyle = New DataGridTextBoxColumn()
With ColumnStyle
.TextBox.Enabled = True
.HeaderText = Column.ColumnName
.MappingName = Column.ColumnName
'Set width to header text width.
Width = Graphics.MeasureString(.HeaderText, DataGrid.Font, MaxPixelWidth).Width
End With
'Change width, if data width is wider than header text width.
'Check the width of the data in the first X rows.
Dim iRow As Integer
Dim DataRow As DataRow
For iRow = 0 To NumberOfRowsToScan - 1
DataRow = DataTable.Rows(iRow)
If Not IsDBNull(DataRow(Column.ColumnName)) Then
Width = System.Math.Max(Width, Graphics.MeasureString(DataRow(Column.ColumnName), DataGrid.Font, MaxPixelWidth).Width)
End If
Next
ColumnStyle.Width = Width + 4
'Add the new column style to the table style.
TableStyle.GridColumnStyles.Add(ColumnStyle)
Next
'Add the new table style to the data grid.
DataGrid.TableStyles.Add(TableStyle)
Finally
Graphics.Dispose()
End Try
Return TableStyle
End Function

'This one allows a DataGridTableStyle to all ready exist on the table.
Public Shared Function DataGridApplyAutomaticWidths _
(ByVal DataGrid As System.Windows.Forms.DataGrid, ByVal TableStyle As DataGridTableStyle, _
ByVal NumberOfRowsToScan As Integer, ByVal MaxPixelWidth As Integer) _
As DataGridTableStyle
'Create graphics object for measuring widths.
Dim Graphics As Graphics = DataGrid.CreateGraphics()
Try
Dim DataTable As DataTable
'DataTable = DataGrid.DataSource.DataSet.Tables(0)
DataTable = DataGrid.DataSource.Tables(0)
'Can only scan rows if they exist.
NumberOfRowsToScan = System.Math.Min(NumberOfRowsToScan, DataTable.Rows.Count)
'Now loop through the columns and set the widths.
Dim Column As DataColumn
Dim ColumnStyle As DataGridColumnStyle
Dim Width As Integer
For Each Column In DataTable.Columns
ColumnStyle = TableStyle.GridColumnStyles(Column.ColumnName)
If Not ColumnStyle Is Nothing Then
'Set width to header text width.
Width = Graphics.MeasureString(ColumnStyle.HeaderText, DataGrid.Font, MaxPixelWidth).Width
'Change width, if data width is wider than header text width.
'Check the width of the data in the first X rows.
Dim iRow As Integer
Dim dr As DataRow
For iRow = 0 To NumberOfRowsToScan - 1
dr = DataTable.Rows(iRow)
If Not IsDBNull(dr(Column.ColumnName)) Then
Width = System.Math.Max(Width, Graphics.MeasureString(dr(Column.ColumnName), DataGrid.Font, MaxPixelWidth).Width)
End If
Next
ColumnStyle.Width = Width + 4
End If
Next
Finally
Graphics.Dispose()
End Try
Return TableStyle
End Function

Public Shared Function GetNewDataGridTextBoxColumn(ByVal MappingName As String) As DataGridTextBoxColumn
Dim TextBoxColumn As DataGridTextBoxColumn = New DataGridTextBoxColumn()
With TextBoxColumn
.MappingName = MappingName
.HeaderText = MappingName
.ReadOnly = True
.Width = 200
End With
Return TextBoxColumn
End Function

Public Shared Function GetNewDataGridTextBoxColumn(ByVal MappingName As String, ByVal [ReadOnly] As Boolean) As DataGridTextBoxColumn
Dim TextBoxColumn As DataGridTextBoxColumn
TextBoxColumn = GetNewDataGridTextBoxColumn(MappingName)
TextBoxColumn.ReadOnly = [ReadOnly]
Return TextBoxColumn
End Function

End Class

 

Returning Version Information From The AssemblyInfo File

To return version information at run-time in VB .NET, use code similar to the below.

Function GetVersion() As String
With System.Diagnostics.FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetExecutingAssembly.Location)
Return .FileMajorPart & "." & .FileMinorPart & "." & .FileBuildPart & "." & .FilePrivatePart
End With
End Function

Function GetComments() As String
With System.Diagnostics.FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetExecutingAssembly.Location)
Return .Comments
End With
End Function

How To Print An Array of Strings

In order to print an array of strings in VB .NET the following class can be used:

Public Class PrintArrayOfStrings
Private pArrayOfStrings() As String
Private pPrintFont As Font
Private pCurrentLine As Integer = 0
Private pArrayOfStrings_UpperBound As Integer

Public Sub New(ByVal ArrayOfStrings() As String)
Me.New(ArrayOfStrings, New Font("Arial", 10))
End Sub

Public Sub New(ByVal ArrayOfStrings() As String, ByVal PrintFont As Font)
pArrayOfStrings = ArrayOfStrings
pPrintFont = PrintFont
pArrayOfStrings_UpperBound = pArrayOfStrings.GetUpperBound(0)
Try
Dim PrintDocument As PrintDocument = New PrintDocument() 'Assumes default printer
AddHandler PrintDocument.PrintPage, New System.Drawing.Printing.PrintPageEventHandler(AddressOf PrintPage)
PrintDocument.Print()
Catch ex As Exception
MessageBox.Show("An error occurred printing. " + ex.Message)
End Try
End Sub

Private Sub PrintPage(ByVal sender As Object, ByVal ev As System.Drawing.Printing.PrintPageEventArgs)
Dim LinesPerPage As Single
Dim yPos As Single = 0
Dim Count As Integer = 0
Dim Line As String
Dim leftMargin As Single = ev.MarginBounds.Left
Dim topMargin As Single = ev.MarginBounds.Top
LinesPerPage = ev.MarginBounds.Height / pPrintFont.GetHeight(ev.Graphics)
' Print each line of the file.
While (Count < LinesPerPage) And (pCurrentLine <= pArrayOfStrings_UpperBound)
Line = pArrayOfStrings(pCurrentLine)
yPos = topMargin + Count * pPrintFont.GetHeight(ev.Graphics)
ev.Graphics.DrawString(Line, pPrintFont, Brushes.Black, leftMargin, yPos, New StringFormat())
Count += 1
pCurrentLine += 1
End While
ev.HasMorePages = (pCurrentLine <= pArrayOfStrings_UpperBound)
End Sub

End Class

Using the Char2Format Structure in VB .NET for Highlighting Text

This example shows how to use the Windows Char2Format structure from VB .NET to perform very basic syntax highlighting. The example shows a routine that simply changes the colour (color) of the selected text in a RichTextBox to red.

Private Sub SetCharFormatColour(ByVal RichTextBox As RichTextBox)
'Set-up Char2Format2 Structure
Dim CF2 As WinAPI.RichEditControl.CharFormat2
With CF2
.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(CF2)
.dwMask = WinAPI.RichEditControl.CFM_COLOR
.crTextColor = RGB(255, 0, 0)
End With
Dim Win32Error As Integer
Win32Error = WinAPI.RichEditControl.SendMessageA(RichTextBox.Handle.ToInt32, WinAPI.RichEditControl.EM_SETCHARFORMAT, WinAPI.RichEditControl.SCF_SELECTION, CF2)
'If the operation fails, the return value is zero. So lets get the error description and display it.
If Win32Error = 0 Then
MsgBox(WinAPI.General.GetLastErrorMessageDescription)
End If
End Sub

The latter routine requires the following unit in order to provide the interface to the Windows API:

Option Strict On
Imports System.Runtime.InteropServices

Namespace WinAPI
Public Class General
Public Const WM_USER As Int32 = &H400
Public Const WM_SETREDRAW As Int32 = &HB

'Declare Function SendMessageByString Lib "user32" Alias "SendMessageA" _
' (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long

' Declare Function SendMessageByLong Lib "user32" Alias "SendMessageA" _
' (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

Public Declare Auto Function SendMessage Lib "user32" _
(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As IntPtr

Private Declare Auto Function FormatMessage Lib "kernel32" Alias "FormatMessageA" (ByVal dwFlags As Integer, ByRef lpSource As Object, ByVal dwMessageId As Integer, ByVal dwLanguageId As Integer, ByVal lpBuffer As String, ByVal nSize As Integer, ByRef Arguments As Integer) As Integer

Public Shared Function GetLastErrorMessageDescription() As String
Const FORMAT_MESSAGE_FROM_SYSTEM As Short = &H1000S
Const LANG_NEUTRAL As Short = &H0S
Dim Win32Error As Integer
Win32Error = System.Runtime.InteropServices.Marshal.GetLastWin32Error()
Dim Buffer As String = Space(999)
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, Win32Error, LANG_NEUTRAL, Buffer, 999, 0)
Return Trim(Buffer)
End Function
End Class

Public Class RichEditControl
Public Declare Function SendMessageA Lib "user32.dll" _
(ByVal hWnd As Integer, ByVal Msg As Integer, ByVal wParam As Integer, ByRef lParam As CharFormat2) As Integer

<StructLayout(LayoutKind.Sequential)> Public Structure CharFormat2
Public cbSize As Int32
Public dwMask As Int32
Public dwEffects As Int32
Public yHeight As Int32
Public yOffset As Int32
Public crTextColor As Int32
Public bCharSet As Byte
Public bPitchAndFamily As Byte
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=32)> Public szFaceName As String
Public wWeight As Int16
Public sSpacing As Int16
Public crBackColor As Int32
Public lcid As Int32
Public dwReserved As Int32
Public sStyle As Int16
Public wKerning As Int16
Public bUnderlineType As Byte
Public bAnimation As Byte
Public bRevAuthor As Byte
Public bReserved1 As Byte
End Structure

Public Declare Function SendMessageAv Lib "user32.dll" Alias "SendMessageA" _
(ByVal hWnd As Integer, ByVal Msg As Integer, ByVal wParam As Integer, ByRef lParam As CharRange) As Integer

<StructLayout(LayoutKind.Sequential)> Public Structure CharRange
Public cpMin As Int32
Public cpMax As Int32
End Structure

'RichEdit messages
Public Const EM_GETLIMITTEXT As Int32 = General.WM_USER + 37
Public Const EM_POSFROMCHAR As Int32 = General.WM_USER + 38
Public Const EM_CHARFROMPOS As Int32 = General.WM_USER + 39
Public Const EM_SCROLLCARET As Int32 = General.WM_USER + 49
Public Const EM_CANPASTE As Int32 = General.WM_USER + 50
Public Const EM_DISPLAYBAND As Int32 = General.WM_USER + 51
Public Const EM_EXGETSEL As Int32 = General.WM_USER + 52
Public Const EM_EXLIMITTEXT As Int32 = General.WM_USER + 53
Public Const EM_EXLINEFROMCHAR As Int32 = General.WM_USER + 54
Public Const EM_EXSETSEL As Int32 = General.WM_USER + 55
Public Const EM_FINDTEXT As Int32 = General.WM_USER + 56
Public Const EM_FORMATRANGE As Int32 = General.WM_USER + 57
Public Const EM_GETCHARFORMAT As Int32 = General.WM_USER + 58
Public Const EM_GETEVENTMASK As Int32 = General.WM_USER + 59
Public Const EM_GETOLEINTERFACE As Int32 = General.WM_USER + 60
Public Const EM_GETPARAFORMAT As Int32 = General.WM_USER + 61
Public Const EM_GETSELTEXT As Int32 = General.WM_USER + 62
Public Const EM_HIDESELECTION As Int32 = General.WM_USER + 63
Public Const EM_PASTESPECIAL As Int32 = General.WM_USER + 64
Public Const EM_REQUESTRESIZE As Int32 = General.WM_USER + 65
Public Const EM_SELECTIONTYPE As Int32 = General.WM_USER + 66
Public Const EM_SETBKGNDCOLOR As Int32 = General.WM_USER + 67
Public Const EM_SETCHARFORMAT As Int32 = General.WM_USER + 68
Public Const EM_SETEVENTMASK As Int32 = General.WM_USER + 69
Public Const EM_SETOLECALLBACK As Int32 = General.WM_USER + 70
Public Const EM_SETPARAFORMAT As Int32 = General.WM_USER + 71
Public Const EM_SETTARGETDEVICE As Int32 = General.WM_USER + 72
Public Const EM_STREAMIN As Int32 = General.WM_USER + 73
Public Const EM_STREAMOUT As Int32 = General.WM_USER + 74
Public Const EM_GETTEXTRANGE As Int32 = General.WM_USER + 75
Public Const EM_FINDWORDBREAK As Int32 = General.WM_USER + 76
Public Const EM_SETOPTIONS As Int32 = General.WM_USER + 77
Public Const EM_GETOPTIONS As Int32 = General.WM_USER + 78
Public Const EM_FINDTEXTEX As Int32 = General.WM_USER + 79
Public Const EM_GETWORDBREAKPROCEX As Int32 = General.WM_USER + 80
Public Const EM_SETWORDBREAKPROCEX As Int32 = General.WM_USER + 81
'Richedit v2.0 messages
Public Const EM_SETUNDOLIMIT As Int32 = General.WM_USER + 82
Public Const EM_REDO As Int32 = General.WM_USER + 84
Public Const EM_CANREDO As Int32 = General.WM_USER + 85
Public Const EM_GETUNDONAME As Int32 = General.WM_USER + 86
Public Const EM_GETREDONAME As Int32 = General.WM_USER + 87
Public Const EM_STOPGROUPTYPING As Int32 = General.WM_USER + 88
Public Const EM_SETTEXTMODE As Int32 = General.WM_USER + 89
Public Const EM_GETTEXTMODE As Int32 = General.WM_USER + 90

Public Const CFM_BOLD As Int32 = &H1
Public Const CFM_ITALIC As Int32 = &H2
Public Const CFM_UNDERLINE As Int32 = &H4
Public Const CFM_STRIKEOUT As Int32 = &H8
Public Const CFM_PROTECTED As Int32 = &H10
Public Const CFM_LINK As Int32 = &H20
Public Const CFM_SIZE As Int32 = &H80000000
Public Const CFM_COLOR As Int32 = &H40000000
Public Const CFM_FACE As Int32 = &H20000000
Public Const CFM_OFFSET As Int32 = &H10000000
Public Const CFM_CHARSET As Int32 = &H8000000

Public Const SCF_SELECTION As Int32 = &H1
Public Const SCF_WORD As Int32 = &H2
Public Const SCF_DEFAULT As Int32 = &H0
Public Const SCF_ALL As Int32 = &H4
Public Const SCF_USEUIRULES As Int32 = &H8

'Event notification masks
Public Const ENM_NONE As Int32 = &H0
Public Const ENM_CHANGE As Int32 = &H1
Public Const ENM_UPDATE As Int32 = &H2
Public Const ENM_SCROLL As Int32 = &H4
Public Const ENM_KEYEVENTS As Int32 = &H10000
Public Const ENM_MOUSEEVENTS As Int32 = &H20000
Public Const ENM_REQUESTRESIZE As Int32 = &H40000
Public Const ENM_SELCHANGE As Int32 = &H80000
Public Const ENM_DROPFILES As Int32 = &H100000
Public Const ENM_PROTECTED As Int32 = &H200000
Public Const ENM_CORRECTTEXT As Int32 = &H400000 ' PenWin specific
Public Const ENM_SCROLLEVENTS As Int32 = &H8
Public Const ENM_DRAGDROPDONE As Int32 = &H10
Public Const ENM_PARAGRAPHEXPANDED As Int32 = &H20
End Class

End Namespace

 
 
 
 
DISCLAIMER
'MSDE Query', 'MSDE Backup / Restore' and 'MSDE Admin' are not endorsed in anyway by Microsoft Inc.
 
Copyright © 2002-2006 WWW.MSDE.BIZ. All rights reserved.
If you have a question please email us

Site Design by
For A Reason Media Company

UK Home Information Packs

Winning Roulette Online