Results 1 to 7 of 7

Thread: quick question for any one reading this

  1. #1

    Thread Starter
    Fanatic Member carlblanchard's Avatar
    Join Date
    Sep 2003
    Location
    Bournemouth (UK)
    Posts
    539

    quick question for any one reading this

    quick question for any one reading this

    i have main mdi parant with a function on it , which needs to access the value of a textbox on a child form so i added the form to a module and when i code to acess the form i get all the parameters fine, but when i run the app i get an error message saying "object reference not set not set to an instance of the object"

    i normally access forms in this way but ive never actully accessed a child from a parant but i cant see if being that difficult
    I am curretly building a defect management system for software and web developers,
    If you wana try it out (beta test) and keep it for free just send me a message

  2. #2
    Lively Member
    Join Date
    Oct 2003
    Posts
    88
    no code?

  3. #3

    Thread Starter
    Fanatic Member carlblanchard's Avatar
    Join Date
    Sep 2003
    Location
    Bournemouth (UK)
    Posts
    539
    mdiparant code
    ==================
    'function to control mdi forms
    Public Sub ShowSingleInstance(ByVal childType As Type)
    For Each child As Form In Me.MdiChildren
    If child.GetType Is childType Then
    child.Activate() 'already loaded so bring to foreground
    Return
    End If
    Next
    Dim frm As Form = Activator.CreateInstance(childType) 'not loaded yet so create
    frm.MdiParent = Me
    frm.Show()
    End Sub
    ========= function that controls mdi childs
    ========= function that controls thr clicks on the toolbar

    Private Sub toolbar1_ButtonClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolBarButtonClickEventArgs) Handles ToolBar1.ButtonClick
    Select Case ToolBar1.Buttons.IndexOf(e.Button)
    Case 0
    frmCustomer_Add.DoDataSave("Customer_Add")
    Case 1
    Case 2
    Case 4
    Case 6
    Case 7
    Case 8
    Case 10
    Case 12
    Case 13
    Case 14
    End Select
    End Sub
    ================================
    case 0 the first button frmCustomer_ADD. is defined in my module

    Public frmCustomer_Add As New Customer_Add

    if i define the form as NEW then i dont get the error however i cant seem to pick up any text from the text boxes on the form
    on my child form i have a button calling the same function and it works fine, so it looks like the toolbar button is getting confused
    I am curretly building a defect management system for software and web developers,
    If you wana try it out (beta test) and keep it for free just send me a message

  4. #4
    Lively Member
    Join Date
    Oct 2003
    Posts
    88
    Where's the code for DoDataSave.

  5. #5

    Thread Starter
    Fanatic Member carlblanchard's Avatar
    Join Date
    Sep 2003
    Location
    Bournemouth (UK)
    Posts
    539
    Public Function DoDataSave(ByVal ChildName As String)
    Try
    MsgBox(ChildName)
    Dim Cmd As OleDbCommand = New OleDbCommand
    Cmd.CommandType = CommandType.StoredProcedure
    Cmd.Connection = MyConnection

    Select Case LCase(ChildName)
    Case LCase("Customer_Add")
    MsgBox(frmCustomer_Add.txtFullName.Text & " here")
    With Cmd
    .CommandText = "InsNewCustomer"
    .Parameters.Add("@FullName", frmCustomer_Add.txtFullName.Text)
    .Parameters.Add("@Building", frmCustomer_Add.txtBuilding.Text)
    .Parameters.Add("@Street", frmCustomer_Add.txtStreet.Text)
    .Parameters.Add("@Street2", frmCustomer_Add.txtStreet2.Text)
    .Parameters.Add("@Town", frmCustomer_Add.txtTown.Text)
    .Parameters.Add("@County", frmCustomer_Add.txtCounty.Text)
    .Parameters.Add("@Postcode", frmCustomer_Add.txtPostCode.Text)
    .Parameters.Add("@Country", 1)
    .Parameters.Add("@Company", frmCustomer_Add.txtCompany.Text)
    .Parameters.Add("@vat", frmCustomer_Add.txtVatNumber.Text)

    .Parameters.Add("@Tel", "")
    .Parameters.Add("@Fax", "")
    .Parameters.Add("@FirstEmail", "")
    .Parameters.Add("@SecondEmail", "")
    .Parameters.Add("@Mobile", "")
    .Parameters.Add("@BillingAddress", 1)

    End With
    Case Else
    MsgBox("No data matching this command")
    End Select

    MyConnection.Open()
    Cmd.ExecuteNonQuery()
    MyConnection.Close()
    MsgBox("saved")
    Catch er As Exception
    MsgBox(er.ToString)
    Finally
    MyConnection.Close()
    End Try
    End Function
    I am curretly building a defect management system for software and web developers,
    If you wana try it out (beta test) and keep it for free just send me a message

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You can break out the ShowSingleInstance method into two. One that returns the single instance and one that shows it.
    VB Code:
    1. 'function to return mdi child
    2.     Public Function GetSingleInstance(ByVal childType As Type) As Form
    3.         For Each child As Form In Me.MdiChildren
    4.             If child.GetType Is childType Then
    5.                 Return child 'already loaded so return
    6.             End If
    7.         Next
    8.         Dim frm As Form = Activator.CreateInstance(childType) 'not loaded yet so create
    9.         Return frm
    10.     End Function
    11.  
    12.     'function to show mdi forms
    13.     Public Sub ShowSingleInstance(ByVal childType As Type)
    14.         Dim child As Form = GetSingleInstance(childType)
    15.         'make sure everything is set
    16.         If child.MdiParent Is Nothing Then child.MdiParent = Me
    17.         'didn't test the part below but it should work
    18.         If Not child.Visible Then
    19.             child.Show()
    20.         Else
    21.             child.Activate()
    22.         End If
    23.     End Sub
    24.  
    25.         'syntax for use
    26.         Dim frm As Form1 = GetSingleInstance(GetType(Form1))
    27.         'if you have option strict on use this
    28.         'Dim frm As Form1 = CType(GetSingleInstance(GetType(Form1)), Form1)
    29.         MsgBox(frm.TextBox1.Text)

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    VB Code:
    1. Public Function DoDataSave(ByVal ChildName As String)
    2. Try
    3. MsgBox(ChildName)
    4. Dim Cmd As OleDbCommand = New OleDbCommand
    5. Cmd.CommandType = CommandType.StoredProcedure
    6. Cmd.Connection = MyConnection
    7.  
    8. Select Case LCase(ChildName)
    9. Case LCase("Customer_Add")
    10. Dim frm As frmCustomer_Add=GetSingleInstance(gettype(frmCustomer_Add))
    11. MsgBox(frm.txtFullName.Text & " here")
    12. With Cmd
    13. .CommandText = "InsNewCustomer"
    14. .Parameters.Add("@FullName", frm.txtFullName.Text)
    15. .Parameters.Add("@Building", frm.txtBuilding.Text)
    16. .Parameters.Add("@Street", frm.txtStreet.Text)
    17. .Parameters.Add("@Street2", frm.txtStreet2.Text)
    18. .Parameters.Add("@Town", frm.txtTown.Text)
    19. .Parameters.Add("@County", frm.txtCounty.Text)
    20. .Parameters.Add("@Postcode", frm.txtPostCode.Text)
    21. .Parameters.Add("@Country", 1)
    22. .Parameters.Add("@Company", frm.txtCompany.Text)
    23. .Parameters.Add("@vat", frm.txtVatNumber.Text)
    24.  
    25. .Parameters.Add("@Tel", "")
    26. .Parameters.Add("@Fax", "")
    27. .Parameters.Add("@FirstEmail", "")
    28. .Parameters.Add("@SecondEmail", "")
    29. .Parameters.Add("@Mobile", "")
    30. .Parameters.Add("@BillingAddress", 1)
    31.  
    32. End With
    33. Case Else
    34. MsgBox("No data matching this command")
    35. End Select
    36.  
    37. MyConnection.Open()
    38. Cmd.ExecuteNonQuery()
    39. MyConnection.Close()
    40. MsgBox("saved")
    41. Catch er As Exception
    42. MsgBox(er.ToString)
    43. Finally
    44. MyConnection.Close()
    45. End Try
    46. End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width