Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting data back from a user control to a parent control 1

Status
Not open for further replies.

steve1rm

Programmer
Aug 26, 2006
255
GB
Hello,

VS 2005

I have a user control. On the parent form i have placed my user control inside a tab control. Inside the user control i have fields for saving and deleting tasks.

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,

Steve
 
I believe that there are many ways in achieving that goal.
My favourite one is adding a public event to the usercontrol.
For example:
Code:
Public Event DeleteStatus(result As Boolean)

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
               RaiseEvent DeleteStatus(False)
            Else
               RaiseEvent DeleteStatus(True)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
 
Hello Mansi,

Thanks for your reply.

Yes the event works fine.

However, the problem i have currently is that i am creating many object of the user control.

So when I click delete it doesn't know which object it should raise the event for.

Normally if i add 5 objects of the user control, if i click on the 5th it will raise the event. However, if I click any of the other 4 will will not raise the event.

The way my program works is that I have a tab control on the parent form. The user will click add and the object will be created on each of the tabs. if i want to delete one object of the user control on the tab it will only work if is the last one i added.

Is there any solution to this problem?

Many thanks for your help, I gave you a star.

Steve
 
In addition, due to the try-catch block:
Code:
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
Dim delResult As Boolean 'False is its default value
Try
   Dim rowsAffected As Integer = 0
   rowsAffected = Me.TA_IncidentTask1.DeleteTask(_taskID)
   If (rowsAffected = 0) Then
      delResult = False
   Else
      delResult = True
   End If
Catch ex As Exception
   MessageBox.Show(ex.Message)
Finally 
   RaiseEvent DeleteStatus(delResult)
End Try
End Sub

Anyways, thank's for the star.
 
I am sorry, didn't read your last post.

Well, from your original post, you have only one button? Or 5 button on 5 pages?
Code:
Handles btnDelete.Click

You can add some parameters to the event handler, such as:
Code:
Public Event DeleteStatus(result As Boolean, PageNumber as Integer, sender As Object)
'PageNumber is the TabPage.Index
'sender is taken from the Button_Click params.
'and so on
 
Hello Mansi

I think the problem is i haven't explained enough.

On my parent form i have tab control. I also have a button that will add the user control object to the first tab.

When they click add again the another object is created and added to the second tab, etc.

The user control contains records in text boxes of customer information.

The delete on the user control will delete the customer information and raise the event so that I can know if the delete was successful so i can delete that tab that the user control was on.

So in my in the parent i have this:
Code:
  Friend WithEvents newTask As TaskControl.QuickTasks
Code:
Public Event DeleteStatus(result As Boolean)handles newTask.quickTasks
'if result is true the delete the tab the user control (object) is on
end sub
Thanks for your patience,

Steve
 
Sorry that last bit of code should be

Code:
Public Event DeleteStatus(result As Boolean)handles newTask.DeleteStatus
'if result is true the delete the tab the user control (object) is on
end sub
 
I guess that you declare the newTask as a new TaskControl.QuickTasks for every TabPage beeing created.

Yes, only the last instance of it will be used (as you said, only the last usercontrol is fired).

A control array is more suitable for this purpose. Unfortunately, at least in VS 2003, we cannot declare control array with the WithEvents keyword.

I'll get back to you with possible solution (I hope).
Meanwhile, try another strategy.

Sorry, Steve.
See you later.
 
The Observer pattern is applicable for this. Using Observer, you can notify any number of observers changes in the observed. In this case, your parent form would observe a child control.
 
Thanks mansii you have been most helpful. I hope a solution is found.

I have never used a observer before, any code or websites where this is used.

Many thanks for all your help,

Steve
 
Okay, I think I found one (I hope):

First, forget about raising the custom event.
Just declare
Code:
Friend newTask As UserControl1

1. In the usercontrol, just remove the ParentControl (TabPage) if successful:
Code:
If (rowsAffected = 0) Then
   Me.Parent.Dispose()
End If

2. Then utilize the ControlRemoved event of TabControl1:
Code:
Private Sub TabControl1_ControlRemoved(ByVal sender As Object, ByVal e As System.Windows.Forms.ControlEventArgs) Handles TabControl1.ControlRemoved
   MsgBox(e.Control.Text & " was removed")
End Sub

Got the idea?
 
Hello Mansii,

That worked great. I think you must be a very good programmer.

For me I am just starting out.

I am not sure how that works and why it should, i would happy to learn if you could explain that code. In easy terms. I might want to do something like that in the future and would like to know more.

Many thanks for your help,

Steve
 
First, thank's, but I'm not that good.

Well, I believe that you already know this code
Code:
Friend newTask As UserControl1
It was an object declaration.

Then, since the usercontrol is put into a TabPage of the TabControl, then it's Parent must be the TabPage.
Your code, perhaps, something like this:
Code:
newTask = New TaskControl.QuickTasks
Dim tp as New TabPage("TabPageText") 'TabPageText is the _taskID
tp.Controls.Add(newTask)
TabControl1.TabPages.Add(tp)

See? The Parent control of each instance of newTask's is one instance of TabPage.
And each tp's Parent control is TabControl1.

Code:
Me.Parent.Dispose()
The above code ask your application to remove one instance of TabPage (tp).
Then the TabControl1.ControlRemoved event is triggered every time a TabPage that belongs to it is disposed (removed).

Play arround with TabControl1.ControlAdded and other events. You will be surprised that you can take full control of your application.

Hope that I made myself clear.
Good luck.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top