Tuesday, August 21, 2007

Programmatically open Access and show a table

Just a note on one way of accomplishing this:

Dim objAccess As Object = CreateObject("Access.Application")
objAccess.Visible = True

Wednesday, August 15, 2007

Console and Windows Form in the same application

I recently needed to write a program that had an attended and unattended mode:

1. Create a console application in Visual Studio .NET.

2. Use this as a template:

Module Module1

    Sub Main()
        If Command$() = "-winform" Then
            Dim frm As New testform()
            Console.WriteLine("this is the console option")
        End If
    End Sub

End Module

Saving window settings in My.Settings

This is useful for saving window position and size when an application closes and then restoring these settings when the application starts again.

1. In app.config, create three new settings:
- IsMaximized (Boolean)
- MainFormPosition (System.Drawing.Point)
- MainFormSize (System.Drawing.Size)

2. In the form's Load event handler, add the following code:

If My.Settings.MainFormPosition <> Nothing _
        AndAlso My.Computer.Screen.Bounds.Contains(My.Settings.MainFormPosition) Then
    Me.StartPosition = FormStartPosition.Manual
    Me.Location = My.Settings.MainFormPosition
    Me.StartPosition = FormStartPosition.CenterScreen
End If
If My.Settings.IsMaximized <> Nothing _
        AndAlso My.Settings.IsMaximized Then
    Me.WindowState = FormWindowState.Maximized
    Me.WindowState = FormWindowState.Normal
End If
If My.Settings.MainFormSize <> Nothing Then
    Me.Size = New Size(Math.Max(100, My.Settings.MainFormSize.Width), Math.Max(100, My.Settings.MainFormSize.Height))
End If

3. In the form's FormClosing event handler, add the following code:

If Me.WindowState = FormWindowState.Maximized Then
    My.Settings.IsMaximized = True
    My.Settings.MainFormPosition = New Point(Me.RestoreBounds.Left, Me.RestoreBounds.Top)
    My.Settings.MainFormSize = New Size(Me.RestoreBounds.Width, Me.RestoreBounds.Height)
    My.Settings.IsMaximized = False
    My.Settings.MainFormPosition = Me.Location
    My.Settings.MainFormSize = Me.Size
End If

Turning off auto-scrolling in a bound DataGridView

I was running into an issue where I didn't want the grid to auto-scroll or change the selected row when the underlying DataSource property was changed. Here's what I came up with:

' dgv is a DataGridView control with SelectionMode set to FullRowSelect and MultiSelect set to False.
Dim intSelectedIndex As Integer = 0
If dgv.SelectedRows.Count > 0 Then
    intSelectedIndex = dgv.SelectedRows(0).Index
End If
Dim intDisplayIndex As Integer = dgv.FirstDisplayedScrollingRowIndex
' Set dgv.DataSource here
If intSelectedIndex < dgv.Rows.Count Then
    dgv.Rows(intSelectedIndex).Selected = True
    dgv.Rows(dgv.Rows.Count - 1).Selected = True
End If
If intDisplayIndex > -1 Then
    If intDisplayIndex <>
        dgv.FirstDisplayedScrollingRowIndex = intDisplayIndex
        dgv.FirstDisplayedScrollingRowIndex = dgv.Rows.Count - 1
    End If
End If