wearytraveller
Programmer
Hello, I'm writing an MDI application in vb.net 2002 that connects to a SQLserver 2000 database. The child forms contain menus that allow the user to Add, Edit or Delete a record and I am using the Menumerge so that the child menu appears in the Parent window menu. I've recently downloaded some code written by a guy called Derick Blake which has allowed me to include icons in my menus! fantastic. What I have done now is place thses sames icons on buttons on the frmParent toolbar so that instead of using the drop down menu a user could click on the toolbar button (New) for example to create a new record. My problem is that I don't know how to write the code that will fire the appropriate event in the active child form. presumably I need to write the code so that when the appropriate toolbar button is clicked on the parent form toolbar(New) the mnuNew_Click in the activechild form fires (I think).
Here is my PARENTFRM code for the toolbar, as you can see I have been able to code some of the buttons;
Private Sub ToolBar1_ButtonClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolBarButtonClickEventArgs) Handles ToolBar1.ButtonClick
' Determine the active child form.
Dim activeChild As Form = Me.ActiveMdiChild
Select Case ToolBar1.Buttons.IndexOf(e.Button)
Case 0 'Separator
Case 1 'Exit
mnuExit_Click(Nothing, Nothing)
Case 2 'Print
Case 3 'Print Preview
Case 4 'Separator
Case 5 'Cut
' If there is an active child form, find the active control, which
' in this example should be a TextBox.
If (Not activeChild Is Nothing) Then
Try
Dim objTextBox As TextBox = _
CType(activeChild.ActiveControl, TextBox)
If (Not objTextBox Is Nothing) Then
' Put selected text on Clipboard.
objTextBox.Cut()
End If
Catch
MessageBox.Show("You need to select a TextBox.", "Attention", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
End If
Case 6 'Copy
' If there is an active child form, find the active control, which
' in this example should be a TextBox.
If (Not activeChild Is Nothing) Then
Try
Dim theBox As TextBox = _
CType(activeChild.ActiveControl, TextBox)
If (Not theBox Is Nothing) Then
' Put selected text on Clipboard.
Clipboard.SetDataObject(theBox.SelectedText)
End If
Catch
MessageBox.Show("You need to select a TextBox.", "Attention", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
End If
Case 7 'Paste
' If there is an active child form, find the active control, which
' in this example should be a TextBox.
If (Not activeChild Is Nothing) Then
Try
Dim theBox As TextBox = CType(activeChild.ActiveControl, TextBox)
If (Not theBox Is Nothing) Then
' Create a new instance of the DataObject interface.
Dim data As IDataObject = Clipboard.GetDataObject()
' If the data is text, then set the text of the
' TextBox to the text in the clipboard.
If (data.GetDataPresent(DataFormats.Text)) Then
theBox.SelectedText = data.GetData(DataFormats.Text).ToString()
End If
End If
Catch
MessageBox.Show("You need to select a TextBox.", "Attention", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
End If
Case 8 'Undo
' If there is an active child form, find the active control, which
' in this example should be a TextBox.
If (Not activeChild Is Nothing) Then
Try
Dim objTextBox As TextBox = _
CType(activeChild.ActiveControl, TextBox)
If (Not objTextBox Is Nothing) Then
' Put selected text on Clipboard...
objTextBox.Undo()
End If
Catch
MessageBox.Show("You need to select a TextBox.", "Attention", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
End If
Case 9 'Redo
Case 10 'Separator...
Case 11 'Add Record to Northwind db...
Case 12 'New Record in Northwind db...
Case 13 'Update Record in Northwind db...
Case 14 'Delete Record in Northwind db...
End Select
End Sub
Here is my code for the ADD event on one of my childfrms, how do I call it from the toolbar on my parent form????
Private Sub mnuAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuAdd.Click
'Declare local variables and objects...
Dim intPosition As Integer
Dim objCommand As SqlCommand = New SqlCommand()
'Save the current record position...
intPosition = myCurrencyManager.Position
'Set the SqlCommand object properties...
objCommand.Connection = cnn1
objCommand.CommandText = "INSERT INTO suppliers " & _
"(companyname, contactname, contacttitle, address, city, " & _
"region, country, postalcode, phone, fax, homepage) " & _
"VALUES(@companyname,@contactname,@contacttitle,@address,@city, " & _
"@region,@country,@postalcode,@phone,@fax,@homepage)"
'objCommand.CommandType = CommandType.Text
'Add parameters for the placeholders in the SQL in the
'CommandText property...
'Parameter for the productname field...
objCommand.Parameters.Add("@companyname", txtCompanyName.Text)
'Parameter for the supplierid field...
objCommand.Parameters.Add("@contactname", txtContactName.Text)
'Parameter for the categoryid field...
objCommand.Parameters.Add("@contacttitle", txtContactTitle.Text)
'Parameter for the quantityperunit field...
objCommand.Parameters.Add("@address", txtAddress.Text)
'Parameter for the unit price field...
objCommand.Parameters.Add("@city", txtCity.Text)
'Parameter for the unitsinstock field...
objCommand.Parameters.Add("@region", cboRegion.Text)
'Parameter for the unitsinstock field...
objCommand.Parameters.Add("@country", cboCountry.Text)
'Parameter for the unitsonorder field...
objCommand.Parameters.Add("@postalcode", txtPostalCode.Text)
'Parameter for the reorderlevel column...
objCommand.Parameters.Add("@phone", txtPhone.Text)
'Parameter for the discontinued field...
objCommand.Parameters.Add("@fax", txtFax.Text)
'Parameter for the discontinued field...
objCommand.Parameters.Add("@homepage", txtHomePage.Text)
Try
'Open the connection
cnn1.Open()
'Execute the OleDbCommand object to update the data...
objCommand.ExecuteNonQuery()
'Close the connection
cnn1.Close()
'Fill the DataSet and bind the fields...
FillDataSetAndView()
BindFields()
'Set the record position to the one that we saved...
myCurrencyManager.Position = intPosition
'Show the current record position...
ShowPosition()
'Display a message that the record was updated...
StatusBar1.Text = "Record Added"
Catch excep As Exception
MsgBox("Error: " & excep.Source & ": " & excep.Message, _
MsgBoxStyle.Exclamation, "Unable to Add Record to Suppliers DataTable - check SQL Network Connection")
'Write Exception message to the event log...
Dim log As EventLog = New EventLog()
log.Log = "NWind_Application"
log.Source = Me.Text
log.WriteEntry(excep.ToString, EventLogEntryType.Error)
log.Close()
'Display message in StatusBar...
StatusBar1.Text = excep.ToString
Finally
'Close the connection
cnn1.Close()
End Try
End Sub
Any suggestions or ideas will be gratefully received thank you all.
PS a code sample would be nice!
PPS any CONSTRUCTIVE criticism of my code is welcome as I am here to learn but please be gentle.
Here is my PARENTFRM code for the toolbar, as you can see I have been able to code some of the buttons;
Private Sub ToolBar1_ButtonClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolBarButtonClickEventArgs) Handles ToolBar1.ButtonClick
' Determine the active child form.
Dim activeChild As Form = Me.ActiveMdiChild
Select Case ToolBar1.Buttons.IndexOf(e.Button)
Case 0 'Separator
Case 1 'Exit
mnuExit_Click(Nothing, Nothing)
Case 2 'Print
Case 3 'Print Preview
Case 4 'Separator
Case 5 'Cut
' If there is an active child form, find the active control, which
' in this example should be a TextBox.
If (Not activeChild Is Nothing) Then
Try
Dim objTextBox As TextBox = _
CType(activeChild.ActiveControl, TextBox)
If (Not objTextBox Is Nothing) Then
' Put selected text on Clipboard.
objTextBox.Cut()
End If
Catch
MessageBox.Show("You need to select a TextBox.", "Attention", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
End If
Case 6 'Copy
' If there is an active child form, find the active control, which
' in this example should be a TextBox.
If (Not activeChild Is Nothing) Then
Try
Dim theBox As TextBox = _
CType(activeChild.ActiveControl, TextBox)
If (Not theBox Is Nothing) Then
' Put selected text on Clipboard.
Clipboard.SetDataObject(theBox.SelectedText)
End If
Catch
MessageBox.Show("You need to select a TextBox.", "Attention", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
End If
Case 7 'Paste
' If there is an active child form, find the active control, which
' in this example should be a TextBox.
If (Not activeChild Is Nothing) Then
Try
Dim theBox As TextBox = CType(activeChild.ActiveControl, TextBox)
If (Not theBox Is Nothing) Then
' Create a new instance of the DataObject interface.
Dim data As IDataObject = Clipboard.GetDataObject()
' If the data is text, then set the text of the
' TextBox to the text in the clipboard.
If (data.GetDataPresent(DataFormats.Text)) Then
theBox.SelectedText = data.GetData(DataFormats.Text).ToString()
End If
End If
Catch
MessageBox.Show("You need to select a TextBox.", "Attention", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
End If
Case 8 'Undo
' If there is an active child form, find the active control, which
' in this example should be a TextBox.
If (Not activeChild Is Nothing) Then
Try
Dim objTextBox As TextBox = _
CType(activeChild.ActiveControl, TextBox)
If (Not objTextBox Is Nothing) Then
' Put selected text on Clipboard...
objTextBox.Undo()
End If
Catch
MessageBox.Show("You need to select a TextBox.", "Attention", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
End If
Case 9 'Redo
Case 10 'Separator...
Case 11 'Add Record to Northwind db...
Case 12 'New Record in Northwind db...
Case 13 'Update Record in Northwind db...
Case 14 'Delete Record in Northwind db...
End Select
End Sub
Here is my code for the ADD event on one of my childfrms, how do I call it from the toolbar on my parent form????
Private Sub mnuAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuAdd.Click
'Declare local variables and objects...
Dim intPosition As Integer
Dim objCommand As SqlCommand = New SqlCommand()
'Save the current record position...
intPosition = myCurrencyManager.Position
'Set the SqlCommand object properties...
objCommand.Connection = cnn1
objCommand.CommandText = "INSERT INTO suppliers " & _
"(companyname, contactname, contacttitle, address, city, " & _
"region, country, postalcode, phone, fax, homepage) " & _
"VALUES(@companyname,@contactname,@contacttitle,@address,@city, " & _
"@region,@country,@postalcode,@phone,@fax,@homepage)"
'objCommand.CommandType = CommandType.Text
'Add parameters for the placeholders in the SQL in the
'CommandText property...
'Parameter for the productname field...
objCommand.Parameters.Add("@companyname", txtCompanyName.Text)
'Parameter for the supplierid field...
objCommand.Parameters.Add("@contactname", txtContactName.Text)
'Parameter for the categoryid field...
objCommand.Parameters.Add("@contacttitle", txtContactTitle.Text)
'Parameter for the quantityperunit field...
objCommand.Parameters.Add("@address", txtAddress.Text)
'Parameter for the unit price field...
objCommand.Parameters.Add("@city", txtCity.Text)
'Parameter for the unitsinstock field...
objCommand.Parameters.Add("@region", cboRegion.Text)
'Parameter for the unitsinstock field...
objCommand.Parameters.Add("@country", cboCountry.Text)
'Parameter for the unitsonorder field...
objCommand.Parameters.Add("@postalcode", txtPostalCode.Text)
'Parameter for the reorderlevel column...
objCommand.Parameters.Add("@phone", txtPhone.Text)
'Parameter for the discontinued field...
objCommand.Parameters.Add("@fax", txtFax.Text)
'Parameter for the discontinued field...
objCommand.Parameters.Add("@homepage", txtHomePage.Text)
Try
'Open the connection
cnn1.Open()
'Execute the OleDbCommand object to update the data...
objCommand.ExecuteNonQuery()
'Close the connection
cnn1.Close()
'Fill the DataSet and bind the fields...
FillDataSetAndView()
BindFields()
'Set the record position to the one that we saved...
myCurrencyManager.Position = intPosition
'Show the current record position...
ShowPosition()
'Display a message that the record was updated...
StatusBar1.Text = "Record Added"
Catch excep As Exception
MsgBox("Error: " & excep.Source & ": " & excep.Message, _
MsgBoxStyle.Exclamation, "Unable to Add Record to Suppliers DataTable - check SQL Network Connection")
'Write Exception message to the event log...
Dim log As EventLog = New EventLog()
log.Log = "NWind_Application"
log.Source = Me.Text
log.WriteEntry(excep.ToString, EventLogEntryType.Error)
log.Close()
'Display message in StatusBar...
StatusBar1.Text = excep.ToString
Finally
'Close the connection
cnn1.Close()
End Try
End Sub
Any suggestions or ideas will be gratefully received thank you all.
PS a code sample would be nice!
PPS any CONSTRUCTIVE criticism of my code is welcome as I am here to learn but please be gentle.