events

hi,. i have the method newform. now when i add a form , i want to add an event the form disposed method to all forms that i create,. i do not want to rewrite the code each time i have to create a form., what am i looking at how i attach an event to any form in vb

Public Sub NewForm(ByRef i As Form)

Forms.Add(i)

i.MdiParent = Me

m_ChildFormNumber += 1

i.Show()

End Sub

Public Sub Form_Disposed(ByRef f As Form) - - -- something similar but general.,

Forms.Remove(f)

myNavigator.BindingSource = Nothing

End Sub




Answer this question

events

  • JAAmutio

    Well, I think I misunderstood, it seems the FormClosing and FormLostFocus already exist and no need to add them. What i told is to add your own events and delegates.

  • Artie Sluka

    Okay i dont know why needing delegates here., the code is working fine now, the error was sittingin my custom dgv where i had a dgv_lostfocus that was removing my navigators bindings. therefore when i clickd on the navigator, it was losing binding to the grid. but y u told me delegates now

  • RC COLA

    The following is a simple example of declaring and using a delegate.

    Imports System
    
    Public Class SamplesDelegate
    
      ' Declares a delegate for a method that takes in an int and returns a String.
      Delegate Function myMethodDelegate(myInt As Integer) As [String]
    
      ' Defines some methods to which the delegate can point.
      Public Class mySampleClass
    
       ' Defines an instance method.
       Public Function myStringMethod(myInt As Integer) As [String]
         If myInt > 0 Then
          Return "positive"
         End If
         If myInt < 0 Then
          Return "negative"
         End If
         Return "zero"
       End Function 'myStringMethod
    
       ' Defines a static method.
       Public Shared Function mySignMethod(myInt As Integer) As [String]
         If myInt > 0 Then
          Return "+"
         End If
         If myInt < 0 Then
          Return "-"
         End If
         Return ""
       End Function 'mySignMethod
      End Class 'mySampleClass
    
      Public Shared Sub Main()
    
       ' Creates one delegate for each method.
       Dim mySC As New mySampleClass()
       Dim myD1 As New myMethodDelegate(AddressOf mySC.myStringMethod)
       Dim myD2 As New myMethodDelegate(AddressOf mySampleClass.mySignMethod)
    
       ' Invokes the delegates.
       Console.WriteLine("{0} is {1}; use the sign ""{2}"".", 5, myD1(5), myD2(5))
       Console.WriteLine("{0} is {1}; use the sign ""{2}"".", - 3, myD1(- 3), myD2(- 3))
       Console.WriteLine("{0} is {1}; use the sign ""{2}"".", 0, myD1(0), myD2(0))
    
      End Sub 'Main
    
    End Class 'SamplesDelegate


  • ROLIVIER

    Public Sub NewForm(ByRef F As Form)

    F.Show()

    AddHandler F.FormClosing, AddressOf Form_Closing

    AddHandler F.LostFocus, AddressOf Form_LostFocus

    End Sub

    Public Sub Form_LostFocus(ByVal s As Object, ByVal e As System.EventArgs)

    Try

    mynav.BindingSource = Nothing

    Catch ex As Exception

    End Try

    End Sub

    Public Sub Form_Closing(ByVal s As Object, ByVal e As FormClosingEventArgs)

    mynav.BindingSource = Nothing

    End Sub

    for now i have this,, and it doesnt seem to work nicely.



  • Alexei Moudretsov

    How do you difine the type of FormClosing and LostFocus I think they should be like

    Delegate Function ClosingDelegate(ByVal s As Object, ByVal e As FormClosingEventArgs) As [String]

    Delegate Function LostFocusDelegate(ByVal s As Object, ByVal e As System.EventArgs) As [String]

    Dim FormClosingAs New ClosingDelegate(AddressOf Form_Closing)

    Dim LostFocus As New LostFocusDelegate(AddressOf Form_LostFocus)




  • events