Cannot call Close() while doing CreateHandle()

Steven McDonald

Well-known member
Joined
Jun 14, 2005
Messages
48
Location
South Africa
Programming Experience
Beginner
Cannot call Close() while doing CreateHandle (RESOLVED)

I get this error 'Cannot call Close() while doing CreateHandle()' when executing a form.

I check if combo boxes have values and if they don't I close the form then this error is shown.
 
Last edited:
post the code from within Load() event (i suspect mostly that) ... but also post the code that checks combo boxes

Cheers ;)

edit: btw, are you maybe open new form right after you close the current form ... maybe something like this?

VB.NET:
 'i'm just improvising 
newF.Show()
newF.Focus()
Me.Close()
 
Heres the code for Load Event()

General_Use_Subs.PopulateComboBox("SELECT * FROM BLACompany", "BLACompany", "BLACompany.CompanyDesc", "BLACompany.CompanyID", cboCompany, sqlConn)

General_Use_Subs.PopulateComboBox("SELECT * FROM BLABusiness WHERE CompanyID = " & cboCompany.SelectedValue, "BLABusiness", "BLABusiness.BusinessName", "BLABusiness.BusinessID", cboBusiness, sqlConn)

General_Use_Subs.PopulateComboBox("SELECT * FROM BLABusinessArea WHERE BusinessID = " & cboBusiness.SelectedValue, "BLABusinessArea", "BLABusinessArea.BusinessAreaName", "BLABusinessArea.BusinessAreaID", cboMainArea, sqlConn)

General_Use_Subs.PopulateComboBox("SELECT * FROM OccupationDiscipline", "OccupationDiscipline", "OccupationDiscipline.OccupationDisciplineName", "OccupationDiscipline.OccupationDisciplineID", cboDiscipline, sqlConn)

General_Use_Subs.PopulateComboBox("SELECT * FROM DepartmentOccupation WHERE OccupationDisciplineID = " & cboDiscipline.SelectedValue, "DepartmentOccupation", "DepartmentOccupation.DepartmentOccupationDesc", "DepartmentOccupation.DepartmentOccupationID", cboOccupation, sqlConn)

General_Use_Subs.PopulateComboBox("SELECT * FROM DepartmentOccupationLevel WHERE DepartmentOccupationID = " & cboOccupation.SelectedValue, "DepartmentOccupationLevel", "DepartmentOccupationLevel.DepartmentOccupationLevelDesc", "DepartmentOccupationLevel.DepartmentOccupationLevelID", cboLevel, sqlConn)

General_Use_Subs.PopulateComboBox("SELECT * FROM CPRDocumentDepartment", "CPRDocumentDepartment", "CPRDocumentDepartment.DocumentDepartmentDesc", "CPRDocumentDepartment.DocumentDepartmentID", cboDocDepartment, sqlConn)

General_Use_Subs.PopulateComboBox("SELECT * FROM CPRDocumentCategory WHERE DocumentDepartmentID = " & cboDocDepartment.SelectedValue, "CPRDocumentCategory", "CPRDocumentCategory.DocumentCategoryDesc", "CPRDocumentCategory.DocumentCategoryID", cboDocCategory, sqlConn)

General_Use_Subs.PopulateComboBox("SELECT * FROM CPRRiskDepartment", "CPRRiskDepartment", "CPRRiskDepartment.RiskDepartmentDesc", "CPRRiskDepartment.RiskDepartmentID", cboRiskDepartment, sqlConn)

General_Use_Subs.PopulateComboBox("SELECT * FROM CPRRiskCategory WHERE RiskDepartmentID = " & cboRiskDepartment.SelectedValue, "CPRRiskCategory", "CPRRiskCategory.RiskCategoryDesc", "CPRRiskCategory.RiskCategoryID", cboRiskCategory, sqlConn)

General_Use_Subs.PopulateComboBox("SELECT * FROM CPRRiskParameter WHERE RiskCategoryID = " & cboRiskCategory.SelectedValue, "CPRRiskParameter", "CPRRiskParameter.RiskParameterDesc", "CPRRiskParameter.RiskParameterID", cboRiskParameter, sqlConn)

General_Use_Subs.PopulateComboBox("SELECT * FROM CPRDocumentProtocol", "CPRDocumentProtocol", "CPRDocumentProtocol.DocumentProtocolDesc", "CPRDocumentProtocol.DocumentProtocolID", cboProtocol, sqlConn)

CheckComboBoxes()

Heres the Populate ComboBox Code:

Public Sub PopulateComboBox(ByVal sqlQueryString As String, ByVal TableName As String, ByVal DisplayMemeber As String, ByVal ValueMember As String, ByVal Field As ComboBox, ByVal Connection As SqlConnection)

Try

Cursor.Current = Cursors.WaitCursor

Field.DataSource =
Nothing

Dim sqlDA As New SqlDataAdapter(sqlQueryString, Connection)

Dim sqlDS As New DataSet

sqlDA.Fill(sqlDS, TableName)

Field.DisplayMember = DisplayMemeber

Field.ValueMember = ValueMember

Field.DataSource = sqlDS

Cursor.Current = Cursors.Default

Catch ex As Exception

MsgBox(ex.Message, MsgBoxStyle.Information, "ERROR")

End Try

End Sub

Then Heres the check comboBox code:

Private Sub CheckComboBoxes()

If cboBusiness.Items.Count = 0 Then

MsgBox("Please enter a Business at the Setup Page", MsgBoxStyle.Information, "No Business")

Me.Close()

ElseIf cboCompany.Items.Count = 0 Then

MsgBox("Please enter a Company at the Setup Page", MsgBoxStyle.Information, "No Company")

Me.Close()

ElseIf cboDiscipline.Items.Count = 0 Then

MsgBox("Please enter a Discipline at the Setup Page", MsgBoxStyle.Information, "No Discipline")

Me.Close()

ElseIf cboDocCategory.Items.Count = 0 Then

MsgBox("Please enter a Document Category at the Setup Page", MsgBoxStyle.Information, "No Document Category")

Me.Close()

ElseIf cboDocDepartment.Items.Count = 0 Then

MsgBox("Please enter a Document Department at the Setup Page", MsgBoxStyle.Information, "No Document Department")

Me.Close()

ElseIf cboLevel.Items.Count = 0 Then

MsgBox("Please enter an Occupation Level at the Setup Page", MsgBoxStyle.Information, "No Occupation Level")

Me.Close()

ElseIf cboMainArea.Items.Count = 0 Then

MsgBox("Please enter a Main Area at the Setup Page", MsgBoxStyle.Information, "No Main Area")

Me.Close()

ElseIf cboOccupation.Items.Count = 0 Then

MsgBox("Please enter an Occupation at the Setup Page", MsgBoxStyle.Information, "No Occupation")

Me.Close()

ElseIf cboProtocol.Items.Count = 0 Then

MsgBox("Please enter a Protocol at the Setup Page", MsgBoxStyle.Information, "No Protocol")

Me.Close()

End If

End Sub

 
the main problem is that you are trying to close the form in load process and the error occurs because a close event has not been formed yet.

looks like the best way to do this is to create a main function in your app.

VB.NET:
 Shared Sub Main() 
Dim form2 As Form2 
form2 = New Form2()
 
If (form2.ShowDialog() = System.Windows.Forms.DialogResult.OK) Then
Application.Run(New Form1())
End If
End Sub
and also don't forget to set up the startup object to Sub main

Also i "guess" this can be solved if you move the CheckComboBoxes() to Activated event as it fires up a bit later than load event ... give it a try before you go for 1st advice

Cheers ;)
 
Back
Top