Results 1 to 10 of 10

Thread: [RESOLVED] [2005] Getting data back from a user control to a parent control

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Resolved [RESOLVED] [2005] Getting data back from a user control to a parent control

    Hello,

    I have a user control. See the image below. The tab control is not part of the user control. but everything inside is.

    When a user wants to delete a task the taskID is taken from the tab name and passed to the user control in a public property. So I know what task I want to delete.
    Code:
    'Code in the user control
    Public Property TaskID() As Integer
            Get
                Return _taskID
            End Get
            Set(ByVal value As Integer)
                _taskID = value
            End Set
        End Property
    '
    Code:
    In the parent form I get the TaskID from the tab name and pass it to the public property
    Private Sub TabControl1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabControl1.Click
            Try
                Dim taskID As Integer = 0
    
                taskID = Integer.Parse(Me.TabControl1.SelectedTab.Name)
                newTask.TaskID = taskID
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End Sub
    But the problem is when I delete I want to be sure that the deletion was successful and pass this back to the parent control. And that is something I can't see to do.

    For example: if there was a problem with the delete then I don't want to delete the tab but inform the user there was a problem.

    Code:
    'code in the user control for deleting the task, but if not successful, how do i send this back to the parent control?
    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
            Try
                Dim rowsAffected As Integer = 0
                rowsAffected = Me.TA_IncidentTask1.DeleteTask(_taskID)
                If (rowsAffected = 0) Then
                   'Not successful so how do i inform the user
                End If
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End Sub
    I am ok at passing information to the user control by using public properties as you have seen in the code above. But i need to know if the delete was successful or not.

    Can anyone help and advice me on this. code examples would be most grateful.

    Many thanks,
    Attached Images Attached Images  
    steve

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Getting data back from a user control to a parent control

    Your UserControl can raise an event that the TabPage or TabControl can handle. You can pass information in the EventArgs of that event to indicate whether the delete operation was successful or not.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Re: [2005] Getting data back from a user control to a parent control

    Thanks for the advice,

    However, I understand what you mean, but I am unsure how to put those words into code.

    Thanks,
    steve

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Getting data back from a user control to a parent control

    E.g.

    The class that contains the data for the event:
    vb Code:
    1. Public Class DeleteAttemptedEventArgs
    2.     Inherits System.EventArgs
    3.  
    4.     Private _deleted As Boolean
    5.  
    6.     Public ReadOnly Property Deleted() As Boolean
    7.         Get
    8.             Return _deleted
    9.         End Get
    10.     End Property
    11.  
    12.     Public Sub New(ByVal deleted As Boolean)
    13.         Me._deleted = deleted
    14.     End Sub
    15.  
    16. End Class
    The event and the method that raises it:
    vb Code:
    1. Public Event DeleteAttempted As EventHandler(Of DeleteAttemptedEventArgs)
    2.  
    3. Protected Overridable Sub OnDeleteAttempted(ByVal e As DeleteAttemptedEventArgs)
    4.     RaiseEvent DeleteAttempted(Me, e)
    5. End Sub
    Raising the event with the appropriate data when a delete operation is attempted:
    vb Code:
    1. Me.OnDeleteAttempted(New DeleteAttemptedEventArgs(Me.TA_IncidentTask1.DeleteTask(_taskID) > 0))
    In a handler for the event:
    vb Code:
    1. If e.Deleted Then
    2.     'A successful deletion was carried out.
    3. Else
    4.     'An unsuccessful attempt was made to delete.
    5. End If
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Re: [2005] Getting data back from a user control to a parent control

    Hello,

    Thanks for writing the code for me. However, I am having problems. All the above code I have entered into the user control and tested and works fine.

    However, I am having problem trying to get the parent form to listen to the event. Can you provide more help to point me in the right direction.

    Many thanks for your patience,
    steve

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Re: [2005] Getting data back from a user control to a parent control

    Hello,

    I have put this could in my form1 that I hope will catch the event.
    vb Code:
    1. Public Sub DeleteAttempted(ByVal sender As Object, ByVal e As TaskControl.DeleteAttemptedEventArgs) Handles newTask.DeleteAttempted
    2.         MessageBox.Show("Delete attempted - success at last")
    3.     End Sub

    have not dragged my user control on the form. As I create one at run-time. For example
    I add a reference to it.
    The solution the user control was created in, is called TaskControl.

    So in my new project i right click add reference, browse and select TaskControl.

    Then in my form1 i write imports TaskControl.

    In the code designer of the form1 i write:

    Friend WithEvents newTask As TaskControl.QuickTasks

    For some reason it does not fire in form1, so will never know if a delete happened or not.

    Any more guidance would be truly grateful, i think i am out of all ideas.

    Many thanks for your patience,
    steve

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Getting data back from a user control to a parent control

    How do you normally create event handlers? It's no different in this case. Either select the UserControl in the designer, open the Properties, click the Events button then double-click the desired event, or else open the code window, select the variable from the drop-down list at the top left and the event from the top right. An event is an event, whether it was declared by the Framework creators at Microsoft, some third-party vendor or yourself.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Getting data back from a user control to a parent control

    So, you declare a variable like this:
    vb Code:
    1. Friend WithEvents newTask As TaskControl.QuickTasks
    and you have an event handler like this:
    vb Code:
    1. Public Sub DeleteAttempted(ByVal sender As Object, ByVal e As TaskControl.DeleteAttemptedEventArgs) Handles newTask.DeleteAttempted
    2.     MessageBox.Show("Delete attempted - success at last")
    3. End Sub
    That means that that method is handling the DeleteAttempted event for the object referred to by the 'newTask' variable. When you create your QuckTasks object at run time, do you assign it to that variable?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Re: [2005] Getting data back from a user control to a parent control

    Hello Jmc,

    I always first try the code way before i try the designer way. It gives me greater understanding when learning this.

    I created a new project and dragged the user control over and added the event by the properties window. It generated the code and worked fine.
    vb Code:
    1. Private Sub QuickTasks1_DeleteAttempted(ByVal sender As System.Object, ByVal e As TaskControl.DeleteAttemptedEventArgs) Handles QuickTasks1.DeleteAttempted
    2.         MessageBox.Show("Delete attempted - success at last: " & e.Deleted)
    3.     End Sub
    Now I do the same at run-time.
    vb Code:
    1. Imports TaskControl
    2. Public Class Form1
    3.  
    4.     Friend WithEvents newTask As TaskControl.QuickTasks
    5.  
    6.     Public Sub DeleteAttempted(ByVal sender As Object, ByVal e As TaskControl.DeleteAttemptedEventArgs) Handles newTask.DeleteAttempted
    7.         MessageBox.Show("Delete attempted - success at last: " & e.Deleted)
    8.     End Sub
    9.  
    10.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    11.         newTask = New TaskControl.QuickTasks
    12.         Me.Controls.Add(newTask)
    13.     End Sub
    14. End Class

    This also worked as well. So i have learnt how to do this at run-time and design time.

    However, none of the above worked in my current project, i had to create a new one to get this to work. Any possibly reason for this?

    I know it might be too much to ask, but could you possibly explain in easy terms what the code you gave me in post #4

    many thanks for your help,
    Last edited by steve_rm; Apr 15th, 2007 at 08:39 PM.
    steve

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Getting data back from a user control to a parent control

    Quote Originally Posted by steve_rm
    could you possibly explain in easy terms what the code you gave me in post #4
    The first code block is a class that inherits EventArgs. The EventArgs class exists specifically to pass data to event handlers. Any event that doesn't require any data receives an EventArgs object, which is the base class. If you are declaring an event that DOES require data then you need to inherit the EventArgs class and add properties for that data. In our case our event handlers need to know whether the delete operation was successful or not, so we added a Boolean property that provides that information.

    The second code snippet is the declaration for the event itself and the method that raises it. The convention is to ALWAYS have the event raised by one and only one method, which has the same name as the event prefixed with "On". The method is also declared Protected Overridable so that it cannot be invoked form outside the class but any derived types can modify the behaviour. The event declaration means that DeleteAttempted is an event whose handlers will receive a DeleteAttemptedEventArgs object. This:
    vb Code:
    1. #
    2. Public Event DeleteAttempted As EventHandler(Of DeleteAttemptedEventArgs)
    is equivalent to this:
    vb Code:
    1. #
    2. Public Event DeleteAttempted(ByVal sender As Object, ByVal e As DeleteAttemptedEventArgs)
    The third code snippet shows how you raise the event. Like I said, you should raise the event from one and only one method. That way if you ever change the implementation, or a derived class does, everything will remain consistent. That means that whenever you want to raise the DeleteAttempted event you call the OnDeleteAttempted method. That method has one argument that contains the DeleteAtemptedEventArgs object that will be passed to any methods that handle the event. You need to create that object and set its Deleted property with a value that indicates whether the operation succeeded or not.

    The final code snippet is what you'd put inside an event handler for that event. The object create the DeleteAttemptedEventArgs object to indicate whether the deletion was successful. It then raises that event and passes that object. Every event handler then receives that object. The whole point of the event is to know whether the deletion was successful or not, so you need to check the appropriate property to find out.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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