I am working on a vb app and am getting this error when calling a form (frmCaseList) from another form(LoginForm). the frmCaseList wasat one time in a different solution where it worked fine (it was the startup form). frmCaseList is also inside a namespace. If I commentout the .show() line in the following block the app doesn't fail (but of course, it also doesn't show the form).

Private Sub btnProceed_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnProceed.Click
        Dim frmCL As DataGridButton.frmCaseList = New DataGridButton.frmCaseList()
        frmCL.Show()
    End Sub

The above code is from LoginForm. Below is the code for the frmCaseList.

Option Strict Off
Option Explicit On 

Imports Microsoft.VisualBasic
Imports System
Imports System.ComponentModel
Imports System.Data
Imports System.Data.Common
Imports System.Data.OleDb
Imports System.Drawing
Imports System.Windows.Forms

Namespace DataGridButton
    Public Class frmCaseList
        Inherits Form
        Private WithEvents dataGrid1 As DataGrid
        Private components As Container
        'Fields
        'Constructors
        'Events
        'Methods
        Public Sub New()
            InitializeComponent()
        End Sub
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub
        Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
        Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox
        Friend WithEvents Button1 As System.Windows.Forms.Button
        Private Sub InitializeComponent()
            Me.dataGrid1 = New System.Windows.Forms.DataGrid()
            Me.TextBox1 = New System.Windows.Forms.TextBox()
            Me.ComboBox1 = New System.Windows.Forms.ComboBox()
            Me.Button1 = New System.Windows.Forms.Button()
            CType(Me.dataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
            Me.SuspendLayout()
            '
            'dataGrid1
            '
            Me.dataGrid1.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                        Or System.Windows.Forms.AnchorStyles.Left) _
                        Or System.Windows.Forms.AnchorStyles.Right)
            Me.dataGrid1.DataMember = ""
            Me.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
            Me.dataGrid1.Location = New System.Drawing.Point(32, 48)
            Me.dataGrid1.Name = "dataGrid1"
            Me.dataGrid1.Size = New System.Drawing.Size(568, 208)
            Me.dataGrid1.TabIndex = 0
            '
            'TextBox1
            '
            Me.TextBox1.Location = New System.Drawing.Point(32, 8)
            Me.TextBox1.Name = "TextBox1"
            Me.TextBox1.Size = New System.Drawing.Size(224, 20)
            Me.TextBox1.TabIndex = 1
            Me.TextBox1.Text = ""
            '
            'ComboBox1
            '
            Me.ComboBox1.Items.AddRange(New Object() {"CaseID", "Case Name", "Defendant"})
            Me.ComboBox1.Location = New System.Drawing.Point(264, 7)
            Me.ComboBox1.Name = "ComboBox1"
            Me.ComboBox1.Size = New System.Drawing.Size(184, 21)
            Me.ComboBox1.TabIndex = 2
            Me.ComboBox1.Text = "Select Search Field"
            '
            'Button1
            '
            Me.Button1.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
            Me.Button1.Location = New System.Drawing.Point(456, 8)
            Me.Button1.Name = "Button1"
            Me.Button1.Size = New System.Drawing.Size(144, 20)
            Me.Button1.TabIndex = 3
            Me.Button1.Text = "Search"
            '
            'frmCaseList
            '
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            Me.ClientSize = New System.Drawing.Size(640, 269)
            Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1, Me.ComboBox1, Me.TextBox1, Me.dataGrid1})
            Me.Name = "frmCaseList"
            Me.Text = "Case List"
            Me.WindowState = System.Windows.Forms.FormWindowState.Maximized
            CType(Me.dataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
            Me.ResumeLayout(False)

        End Sub


        <STAThread()> _
        Public Shared Sub Main()
            Application.Run(New frmCaseList())
        End Sub

        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
            ' Set the connection and sql strings
            ' assumes your mdb file is in your root
            Dim connString As String = "Provider=sqloledb;data source=ds; initial catalog=cat;uid=user;pwd=pass;" '

            Dim sqlString As String = "SELECT CaseID, CaseDocketNumber,CaseName,TrialDate, EffectiveDate FROM Cases Order By EffectiveDate DESC"

            Dim dataAdapter As OleDbDataAdapter = Nothing
            Dim _dataSet As DataSet = Nothing

            Try
                ' Connection object
                Dim connection As New OleDbConnection(connString)

                ' Create data adapter object
                dataAdapter = New OleDbDataAdapter(sqlString, connection)

                ' Create a dataset object and fill with data using data adapter's Fill method
                _dataSet = New DataSet()
                dataAdapter.Fill(_dataSet, "customers")
                connection.Close()
            Catch ex As Exception
                MessageBox.Show(("Problem with DB access-" + ControlChars.Lf + ControlChars.Lf + "   connection: " + connString + ControlChars.Cr + ControlChars.Lf + ControlChars.Cr + ControlChars.Lf + "            query: " + sqlString + ControlChars.Cr + ControlChars.Lf + ControlChars.Cr + ControlChars.Lf + ControlChars.Cr + ControlChars.Lf + ex.ToString()))
                Me.Close()
                Return
            End Try





            ' Create a table style that will hold the new column style 
            ' that we set and also tie it to our customer's table from our DB
            Dim tableStyle As New DataGridTableStyle()
            tableStyle.MappingName = "customers"

            ' since the dataset has things like field name and number of columns,
            ' we will use those to create new columnstyles for the columns in our DB table
            Dim numCols As Integer = _dataSet.Tables("customers").Columns.Count

            '	//add an extra column at the end of our customers table
            '	_dataSet.Tables["customers"].Columns.Add("Button");
            Dim aColumnTextColumn As DataGridTextBoxColumn = Nothing
            Dim buttonColStyle As DataGridButtonColumn = Nothing
            Dim textButtonColStyle As DataGridTextButtonColumn = Nothing
            Dim i As Integer = 0

            While i < numCols
                'add 2 button columns
                If i = 0 Then
                    textButtonColStyle = New DataGridTextButtonColumn(i) 'pass the column#
                    textButtonColStyle.HeaderText = _dataSet.Tables("customers").Columns(i).ColumnName
                    textButtonColStyle.MappingName = _dataSet.Tables("customers").Columns(i).ColumnName

                    'hookup our cellbutton handler...
                    AddHandler textButtonColStyle.CellButtonClicked, AddressOf HandleCellButtonClick

                    tableStyle.GridColumnStyles.Add(textButtonColStyle)

                    'hook the mouse handlers
                    AddHandler dataGrid1.MouseDown, AddressOf textButtonColStyle.HandleMouseDown
                    AddHandler dataGrid1.MouseUp, AddressOf textButtonColStyle.HandleMouseUp
                    'ElseIf i = 5 Then
                    'buttonColStyle = New DataGridButtonColumn(i) 'pass the column#
                    'buttonColStyle.HeaderText = _dataSet.Tables("customers").Columns(i).ColumnName
                    'buttonColStyle.MappingName = _dataSet.Tables("customers").Columns(i).ColumnName

                    'hookup our cellbutton handler...
                    'AddHandler buttonColStyle.CellButtonClicked, AddressOf HandleCellButtonClick

                    'tableStyle.GridColumnStyles.Add(buttonColStyle)

                    'hook the mouse handlers
                    'AddHandler dataGrid1.MouseDown, AddressOf buttonColStyle.HandleMouseDown
                    'AddHandler dataGrid1.MouseUp, AddressOf buttonColStyle.HandleMouseUp
                Else ' add standard textbox columns for the other columns
                    aColumnTextColumn = New DataGridTextBoxColumn()
                    aColumnTextColumn.HeaderText = _dataSet.Tables("customers").Columns(i).ColumnName
                    aColumnTextColumn.MappingName = _dataSet.Tables("customers").Columns(i).ColumnName
                    tableStyle.GridColumnStyles.Add(aColumnTextColumn)
                End If
                i = i + 1
            End While

            ' make the dataGrid use our new tablestyle and bind it to our table
            dataGrid1.TableStyles.Clear()
            dataGrid1.TableStyles.Add(tableStyle)



            'bind the table to the datagrid
            dataGrid1.DataSource = _dataSet.Tables("customers")
            AutoSizeTable()

        End Sub
        Private Sub HandleCellButtonClick(ByVal sender As Object, ByVal e As DataGridCellButtonClickEventArgs)
            'MessageBox.Show(("row " + e.RowIndex.ToString() + "  col " + e.ColIndex.ToString() + " clicked."))
            'MessageBox.Show(dataGrid1(dataGrid1.CurrentCell))
            Dim frm As frmCaseInfo = New frmCaseInfo(dataGrid1(dataGrid1.CurrentCell))
            frm.Show()
        End Sub

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim connString As String = "Provider=sqloledb;data source=testsvr2; initial catalog=solaria;uid=acclaim;pwd=master;" '
            Dim sqlString As String

            If TextBox1.Text <> "" Then
                If ComboBox1.SelectedItem = "Case Name" Then
                    'MsgBox(ComboBox1.SelectedItem)
                    sqlString = "SELECT CaseID, CaseDocketNumber,CaseName,TrialDate, EffectiveDate FROM Cases WHERE CaseName Like '%" & TextBox1.Text & "%'  Order By EffectiveDate DESC"
                ElseIf ComboBox1.SelectedItem = "Defendant" Then
                    sqlString = "SELECT CaseID, CaseDocketNumber,CaseName,TrialDate, EffectiveDate FROM Cases WHERE " & ComboBox1.SelectedItem & " Like '%" & TextBox1.Text & "%'  Order By EffectiveDate DESC"
                Else
                    'MsgBox(ComboBox1.SelectedItem)
                    sqlString = "SELECT CaseID, CaseDocketNumber,CaseName,TrialDate, EffectiveDate FROM Cases WHERE " & ComboBox1.SelectedItem & " = " & TextBox1.Text & " Order By EffectiveDate DESC"
                End If
            Else
                sqlString = "SELECT CaseID, CaseDocketNumber,CaseName,TrialDate, EffectiveDate FROM Cases"
            End If

            Dim dataAdapter As OleDbDataAdapter = Nothing
            Dim _dataSet As DataSet = Nothing

            Try
                ' Connection object
                Dim connection As New OleDbConnection(connString)

                ' Create data adapter object
                dataAdapter = New OleDbDataAdapter(sqlString, connection)

                ' Create a dataset object and fill with data using data adapter's Fill method
                _dataSet = New DataSet()
                dataAdapter.Fill(_dataSet, "customers")
                connection.Close()
            Catch ex As Exception
                MessageBox.Show(("Problem with DB access-" + ControlChars.Lf + ControlChars.Lf + "   connection: " + connString + ControlChars.Cr + ControlChars.Lf + ControlChars.Cr + ControlChars.Lf + "            query: " + sqlString + ControlChars.Cr + ControlChars.Lf + ControlChars.Cr + ControlChars.Lf + ControlChars.Cr + ControlChars.Lf + ex.ToString()))
                Me.Close()
                Return
            End Try





            ' Create a table style that will hold the new column style 
            ' that we set and also tie it to our customer's table from our DB
            Dim tableStyle As New DataGridTableStyle()
            tableStyle.MappingName = "customers"

            ' since the dataset has things like field name and number of columns,
            ' we will use those to create new columnstyles for the columns in our DB table
            Dim numCols As Integer = _dataSet.Tables("customers").Columns.Count

            '	//add an extra column at the end of our customers table
            '	_dataSet.Tables["customers"].Columns.Add("Button");
            Dim aColumnTextColumn As DataGridTextBoxColumn = Nothing
            Dim buttonColStyle As DataGridButtonColumn = Nothing
            Dim textButtonColStyle As DataGridTextButtonColumn = Nothing
            Dim i As Integer = 0

            While i < numCols
                'add 2 button columns
                If i = 0 Then
                    textButtonColStyle = New DataGridTextButtonColumn(i) 'pass the column#
                    textButtonColStyle.HeaderText = _dataSet.Tables("customers").Columns(i).ColumnName
                    textButtonColStyle.MappingName = _dataSet.Tables("customers").Columns(i).ColumnName

                    tableStyle.GridColumnStyles.Add(textButtonColStyle)

                    'hook the mouse handlers
                    AddHandler dataGrid1.MouseDown, AddressOf textButtonColStyle.HandleMouseDown
                    AddHandler dataGrid1.MouseUp, AddressOf textButtonColStyle.HandleMouseUp

                Else ' add standard textbox columns for the other columns
                    aColumnTextColumn = New DataGridTextBoxColumn()
                    aColumnTextColumn.HeaderText = _dataSet.Tables("customers").Columns(i).ColumnName
                    aColumnTextColumn.MappingName = _dataSet.Tables("customers").Columns(i).ColumnName
                    tableStyle.GridColumnStyles.Add(aColumnTextColumn)
                End If
                i = i + 1
            End While

            ' make the dataGrid use our new tablestyle and bind it to our table
            dataGrid1.TableStyles.Clear()
            dataGrid1.TableStyles.Add(tableStyle)



            'bind the table to the datagrid
            dataGrid1.DataSource = _dataSet.Tables("customers")
            AutoSizeTable()


        End Sub
        Public Sub AutoSizeTable()

            Dim numCols As Integer
            numCols = CType(dataGrid1.DataSource, DataTable).Columns.Count
            Dim i As Integer
            i = 0

            Do While (i < numCols)
                AutoSizeCol(i)
                i = (i + 1)

            Loop

        End Sub
        Public Sub AutoSizeCol(ByVal col As Integer)

            Dim width As Single
            width = 0
            Dim numRows As Integer
            numRows = CType(dataGrid1.DataSource, DataTable).Rows.Count
            Dim g As Graphics
            g = Graphics.FromHwnd(dataGrid1.Handle)
            Dim sf As StringFormat
            sf = New StringFormat(StringFormat.GenericTypographic)
            Dim size As SizeF
            Dim i As Integer
            i = 0

            Do While (i < numRows)
                size = g.MeasureString(dataGrid1(i, col).ToString, dataGrid1.Font, 500, sf)

                If col = 0 Then
                    width = size.Width + 40


                ElseIf (size.Width > width) Then
                    width = size.Width
                End If
                i = (i + 1)

            Loop
            g.Dispose()
            dataGrid1.TableStyles("customers").GridColumnStyles(col).Width = CType(width, Integer)

        End Sub

    End Class
End Namespace

As a bonus here are the messages in the output window.

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
   at WindowsApplication2.DataGridButton.DataGridTextButtonColumn.Paint(Graphics g, Rectangle bounds, CurrencyManager source, Int32 rowNum, Brush backBrush, Brush foreBrush, Boolean alignToRight) in C:\Documents and Settings\Administrator\My Documents\Visual Studio Projects\WindowsApplication2\DataGridTextButtonColumn.vb:line 107
   at System.Windows.Forms.DataGridRelationshipRow.PaintCellContents(Graphics g, Rectangle cellBounds, DataGridColumnStyle column, Brush backBr, Brush foreBrush, Boolean alignToRight)
   at System.Windows.Forms.DataGridRow.PaintData(Graphics g, Rectangle bounds, Int32 firstVisibleColumn, Int32 columnCount, Boolean alignToRight)
   at System.Windows.Forms.DataGridRelationshipRow.Paint(Graphics g, Rectangle bounds, Rectangle trueRowBounds, Int32 firstVisibleColumn, Int32 numVisibleColumns, Boolean alignToRight)
   at System.Windows.Forms.DataGrid.PaintRows(Graphics g, Rectangle& boundingRect)
   at System.Windows.Forms.DataGrid.PaintGrid(Graphics g, Rectangle gridBounds)
   at System.Windows.Forms.DataGrid.OnPaint(PaintEventArgs pe)
   at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
   at System.Windows.Forms.Control.WmPaint(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.ComponentManager.System.Windows.Forms.UnsafeNativeMethods+IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
   at WindowsApplication2.LoginForm.Main() in C:\Documents and Settings\Administrator\My Documents\Visual Studio Projects\WindowsApplication2\loginForm.vb:line 13The program '[2092] WindowsApplication2.exe' has exited with code 0 (0x0).

I dont know what u r doing this

Dim frmCL As DataGridButton.frmCaseList = New DataGridButton.frmCaseList()
frmCL.Show()

To call one form from another

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Form2.Show()
    End Sub

& if u want to show the from as modal dialog box then use

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Form2.ShowDialog()
    End Sub

I hope u know the diff b/w modal dialog box & modeless dialog box

I dont know what u r doing this

Dim frmCL As DataGridButton.frmCaseList = New DataGridButton.frmCaseList()
frmCL.Show()

To call one form from another

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Form2.Show()
    End Sub

Because this

frmCL.Show()

Or this

frmCaseList.show()

Gives me this.

Name 'frmCL' is not declared.

and yes I know what modal is.

Use shared method.

Form1.Show

R u initialising any components in frmCL.means r u using this code-

Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub

R u initialising any components in frmCL.means r u using this code-

Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub

Yes, the long block of code in my code for this form. There is a statement like yours (exactly) in it. also as i said in its original project it worked fine it was when i added it to this one that the problems began.

I m also facing same probs sometimes

I dont know y r u facing this probs,frmCl is not declared-
see mine code working 100% correctly-
FORM1 CODE

Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Form2.Show()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Class

FORM2 CODE

Public Class Form2

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Class

I thk so cab try the foll code to call a frmCL form,may be it works..In the below code just replace form2 with frmCL

Public Class Form1

 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Dim obj As New Form2
            obj.Show()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub
End Class

Reply it back if it will not or not....

Not very different from how I was doing it.I tried it that way as well with no change. It appears that the error is not actually happening at that point in the code. Perhaps somewhere deeper in the code being called. I note the error code I included in my original code refers to this line of code which is in a different file called by the the form I am trying to open.

g.DrawImage(bm, bounds.Right - bm.Width, bounds.Y)

which is contained in this block..

'draw the button
            Dim bm As Bitmap
            If _pressedRow = rowNum Then bm = Me._buttonFacePressed Else bm = Me._buttonFace
            g.DrawImage(bm, bounds.Right - bm.Width, bounds.Y)

Also the form frmCL starts rendering but the datagrid that makes up most of the form never does.

I have some new information. Through multiple debugging runs with various breakpoints I have found out why I am getting the error. It starts earlier in the code, here:

w(ByVal colNum As Integer)
            _columnNum = colNum
            _pressedRow = -1

            Try
                Dim strm As System.IO.Stream = Me.GetType().Assembly.GetManifestResourceStream("buttonface.bmp")_buttonFace = New Bitmap(strm)
                strm = Me.GetType().Assembly.GetManifestResourceStream("buttonfacepressed.bmp")_buttonFacePressed = New Bitmap(strm)
            Catch exg As Exception
                MsgBox(exg.Message)
            End Try
        End Sub 'New

I see here that the values for _buttonface and _buttonfacepressed are being set to the appropriate bitmaps. The problem is it doesn't happen. stepping through this code I saw that at the catch line both of those bitmaps were set to 'nothing'. This explains quite well why I get the error i was getting later in the code 'nothing' was being assigned.
Next question is why is the GetManifestResourceStream not working? I have the files in the same location (relatively speaking) as i did when they were working.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.