|
-
Feb 8th, 2012, 10:59 AM
#1
Thread Starter
Member
Call Sub in Parent Form
Hi, I am trying to create a project in VB 2010 like I have in VB6. In VB6 I have a parent form which opens a child form. The child form displays calls (customers) on a spread sheet control that are on hold. The user selects the call in the child form, it calls a sub in the parent form passing a record number to identify the customer. The parent form then "pulls" up the call and and connects the user to the caller with all of the customers information.
So, I haven't done this in VB NET and I have searched to see if this would be done the same way in .NET as it was in VB6, but I haven't found much decisive information. Is a parent/child form the way to do this?
-
Feb 8th, 2012, 12:42 PM
#2
Re: Call Sub in Parent Form
aww man... geezes.... bad developer... bad... bad developer....
that's a bad design... even by VB6 standards...
Create a property on the child form, heck, it can even be a readonly property... when the user then selects the item, store the id in the property and set the dialogresult of the form (to DialogResult.OK)... the calling form should then do this: create instance of child form... show it using ShowDialog ... get the result from it, if it is DialogResult.OK, then get the ID out of the form's property... the code continues and you can then call your sub, passing it the ID.
-tg
-
Feb 8th, 2012, 01:31 PM
#3
Thread Starter
Member
Re: Call Sub in Parent Form
Let me first say that I did not write it, I am just converting it, so please don't hold that part against me. 
Thank you for your help! I looked up what you referenced and I am going to try it, but in my case a user can select an OK button or double click on the spread sheet control. Is there a way to capture that by doing it this way or is there a better way to do it?
-
Feb 8th, 2012, 01:52 PM
#4
Thread Starter
Member
Re: Call Sub in Parent Form
After looking at this some more, I don't think that this will work for me. My child form is basically open all day long right long with my parent form. The child form cannot be modal as I need to access each form. All the child form does is query a DB for a call. If a call exists, it displays it. The user will then choose a call by double clicking on a row in a spread sheet or selecting it and pressing OK.
Does this change the way this should be done?
-
Feb 8th, 2012, 02:29 PM
#5
Re: Call Sub in Parent Form
yes... actually it does... the old design makes some sense now... or at least I can see why it was done that way. There is still a more elegant way to achieve this though... I need to go look up an old thread for it though...
-tg
-
Feb 8th, 2012, 02:47 PM
#6
Re: Call Sub in Parent Form
I really need to bookmark that thread... everytime I need it, I can't find it... but find it I did....
http://www.vbforums.com/showthread.php?t=615412 (post #4) has the code example...
the example was used to show how two child forms can talk using the common parent form...
here's the quick run down... you have two forms that need to communicate with each other, more specifically, one form that needs to be able to send messages to the other... so... you main form needs to open the child form, then register with it so that it can recieve messages from it. Then when your child form does something, it can raise an event, through which you can send data and have the main form react to it. The code sample that I linked to then takes it a step further and does message routing to other forms... but I think the key part will be the delegates and events that are setup on the child forms which I use to send the messages to the parent form.
-tg
-
Feb 10th, 2012, 11:52 AM
#7
Thread Starter
Member
Re: Call Sub in Parent Form
Hey tg,
I have been working on your example, which is neat (thanks!!). I haven't done this type of programming before. Now that I am in it more I have been thinking about the overall project. Like I said, I have a parent form which open the child form. Calls are displayed on the child form and they will open in the parent form. I have the messaging working great and I love it.
Now the new part. I also have other forms that show past calls, or establishing new calls just from searching for the customer in a customer search form. With that being said, I will have additional multiple child forms that will be opening and closing all day long. I know I can open additional forms, but I assume the portion of the code below is incorrect. I am not very experienced with this type of programming.
Basically, I removed some of the code that you wrote as the child forms will be opening at different times, but again, I don't understand how the rest of the code works.
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
myObject = New List(Of ObjectClass)
Dim newObj As New ObjectClass
myObject.Add(newObj)
newObj.ShowForm()
AddHandler newObj.RecieveMessage, AddressOf MessageRecieved
End Sub
-
Feb 10th, 2012, 12:45 PM
#8
Re: Call Sub in Parent Form
here's a break down of what is going on. The first line creates the list that will be used to track the different objects/forms that can send/receive messages.
becareful with that... you only want to create that list ONCE... it's in the button event simply to keep the code condensed in the example. Other wise it would probably be created when the parent form is created.
The dim line is hopefully self-explanitory.
the next thing that's done is to add our new object to the list. And then we show the object (which really shows the form the object created.)
The very last thing that it does is to wire up an event handler to the the RecieveMessage event of the object. Basically we're saying, hey, anytime this object raises the RecieveMessage event, call this event handler over here.
It's not different than wiring up the click event of a button.
-tg
-
Feb 10th, 2012, 12:56 PM
#9
Thread Starter
Member
Re: Call Sub in Parent Form
Okay great, it is kind of what I thought. So, on the form load I should put this
Code:
myObject = New List(Of ObjectClass)
And then on each places that I open up new child forms I will have this (but it would open the different forms)?
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
myObject = New List(Of ObjectClass)
myObject.Add(newObj)
newObj.ShowForm()
AddHandler newObj.RecieveMessage, AddressOf MessageRecieved
End Sub
Two more things, as I am thinking about it, do I need this in the form load, not just the one line?
Code:
MsgQueue.Items.Clear()
If myObject IsNot Nothing Then
For Each o As ObjectClass In myObject
o = Nothing
Next
myObject.Clear()
Else
myObject = New List(Of ObjectClass)
End If
Last thing, I think. I assume that it doesn't matter if the main program has multiple instances as the objects relate to the parent form. Is that true?
Thanks so much for your help!!
-
Feb 10th, 2012, 01:34 PM
#10
Re: Call Sub in Parent Form
 Originally Posted by UncleCake
Okay great, it is kind of what I thought. So, on the form load I should put this
Code:
myObject = New List(Of ObjectClass)
correct... or you can also have it all on the same line where you define the list:
Code:
Private myObject as New List(Of ObjectClass)
That will declare and instanciate it all in one shot. I prefer that to instanciating it in the Load event.
 Originally Posted by UncleCake
And then on each places that I open up new child forms I will have this (but it would open the different forms)?
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
myObject = New List(Of ObjectClass)
myObject.Add(newObj)
newObj.ShowForm()
AddHandler newObj.RecieveMessage, AddressOf MessageRecieved
End Sub
Again, yep... ... no... remove the New List(of) line... the ONLY thing that should be in the place where you want to open you child forms is JUST THE THREE LINES... the add, the show, the addhandler...
 Originally Posted by UncleCake
Two more things, as I am thinking about it, do I need this in the form load, not just the one line?
Code:
MsgQueue.Items.Clear()
If myObject IsNot Nothing Then
For Each o As ObjectClass In myObject
o = Nothing
Next
myObject.Clear()
Else
myObject = New List(Of ObjectClass)
End If
No... over kill... you're jsut now instanciating the form, and subsequently just instanciated the list... it's not going to have anything in it. In fact, if you go with the uber one-liner I mentioned at the time... then you won't have any code in the form load (other than what other code you need in there for other things).
 Originally Posted by UncleCake
Last thing, I think. I assume that it doesn't matter if the main program has multiple instances as the objects relate to the parent form. Is that true?
Thanks so much for your help!!
That is correct... in fact it goes beyond that, as the list would only be a part of the app instance in which it's running... It's possible to get some cross app instance communication by utilizing a public shared object... but that's king out outside the scope of what you need.
-tg
-
Feb 10th, 2012, 02:16 PM
#11
Thread Starter
Member
Re: Call Sub in Parent Form
I had a little scare and I was wondering if I did this correctly. As I said, there will be multiple child forms calling the same sub in the parent form. Two of the forms are the CallsOnHoldForm and the CustomerSearchForm, as you can see I used in the ObjectClass.
So, is this done correctly? It does work, but it is correct?
I hate to do this again, but as I am working through this, we use a Citrix server so that our sales staff can access our internal programs using the VPN and iPad. Is there going to be any problems using the Citrix server? I also assume that you may not have experience with that.
Code:
Public Class ObjectClass
Public Event RecieveMessage(ByVal sender As Object, ByVal e As ObjectMessageEvent)
Public Delegate Sub SendMessageDelegate(ByVal CallRecordNumberToSend As Long)
Public Delegate Sub AcceptMessageDelegate(ByVal CallRecordNumberText As Long)
Private _MyCallsOnHoldForm As New CallsOnHoldForm
Private _MyCustomerSearchForm As New CustomerSearchForm
Public Sub SendMessage(ByVal CallRecordNumberToSend As Long)
Dim objEventArgs As ObjectMessageEvent = New ObjectMessageEvent(CallRecordNumberToSend)
RaiseEvent RecieveMessage(Me, objEventArgs)
End Sub
Public Sub ShowCallsOnHoldForm()
_MyCallsOnHoldForm.SendMessage = AddressOf Me.SendMessage
_MyCallsOnHoldForm.Show()
End Sub
Public Sub ShowCustomerSearchForm()
_MyCustomerSearchForm.SendMessage = AddressOf Me.SendMessage
_MyCustomerSearchForm.Show()
End Sub
End Class
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|