|
|
I have two forms form1 and form 2.
Form1 is added as a control to form 2.
There is a listbox (listbox1) on form2 that i want update from form1 but I am unable to this.Actually i am unable to update anything on the main form from the embedded form.
I typed this in form1,
"form2.listbox1.items.add"
but it isn't working.Please advise
|
|
|
|
|
I would think you would have to use events to trigger changes to a parent form...
|
|
|
|
|
ok....how would the code look like for such an event?
I tried this snippet which i found online but it didnt work
I am quite new to this so i dont know exactly what it does
In child form...
Public Class
Public Event addtolist as EventHandler
'
Private sub()
'
' some code
'
'
RaiseEvent addtolist 'where i want to add to the listbox in parent form
'
'
End Sub
End Class
But I don't know what to do after this.Where do I add the code to add text to the listbox in parent form?
|
|
|
|
|
In your parent class you have to subscribe or handle that event and then trigger whatever you want done. Good use of research, you're making great progress.
|
|
|
|
|
Ok...but how would i handle or subscribe to that event from the child class?
Do i create a sub in the parent class with the same name as the event?
|
|
|
|
|
check this[^], this[^], and this[^].
Specifically do research on the handles[^] keyword. You are throwing an event, you need to handle it.
Best of luck you're almost there.
|
|
|
|
|
Youve got it backwards. You expose the event in the child form, then subscribe to it in the parent form. Look up AddHandler and RemoveHandler to subscribe and unsubscribe to events of objects.
|
|
|
|
|
Yep, sorry I missed over that, those links will still help you out (to the OP)
|
|
|
|
|
ok..so should it be something like this
in child class..
In child form...
Public Class1
Public Event addtolist (Byval s as string) '' s is what is to printed in the listbox in parent class
'
Private sub()
dim obj as new class1
addhandler obj.addtolist,address of class2.addtolistbox
' some code
'
'
RaiseEvent addtolist(s) 'where i want to add to the listbox in parent form
'
'
End Sub
End Class1
In parent form.....
public class2
public sub addtolistbox(ByVal sender As Object, _
ByVal e As System.EventArgs)
listbox1.items.add(e.argument)
end sub
end class2
Is this how what you mean Dave?
EliottA,thanks for the comments and links.they were useful
|
|
|
|
|
OK, last time...
The CHILD form exposes the public Event and calls RaiseEvent when it needs a subscriber to know of a change. This much you got right. The child form does NOT use AddHandler and RemoveHandler on it's own events because it's not subscribing to itself.
The PARENT form needs to call AddHandler to subscribe to the events exposed by the child form. But, it can only do this if the parent form has a method that matches the header exposed by the event AND has an child form object (an instance of a class is called an "object"). So, after your parent form creates a child form object, it calls AddHandler to wire up the event exposed by the child form object to the method in the parent form that handles the event.
|
|
|
|
|
|
Thanks for description dave..
ok..so should it be something like this..i am sorry I havent tried it yet because visual studio is on my other laptop that I dont have with me right now..so just doing some research on it..I'll try it as soon as i get have it.
In child form...
Public Class1
Public Event addtolist (Byval s as string) '' s is what is to printed in the listbox in parent class
'
Private sub()
' some code
'
'
RaiseEvent addtolist(s) 'where i want to add to the listbox in parent form
'
'
End Sub
End Class1
In parent form.....
public class2
public obj as new class1
addhandler obj.addtolist,addressof addtolistbox
public sub addtolistbox(ByVal sender As Object, _
ByVal e As System.EventArgs)
listbox1.items.add(e.argument)
end sub
end class2
Just one thing i didn't get..sorry to bother you..
"it can only do this if the parent form has a method that matches the header exposed by the event",I don't know what you meant by this.
Do you mean the method in the parent form that handles the event should have the same name as the event ie; 'addtolist'?
|
|
|
|
|
Yeah, pretty much got it. Except for one thing:
public sub addtolistbox(ByVal sender As Object, _
ByVal e As System.EventArgs)
This is wrong. Like I said, the method signatures have to match. So, you event is defined like this:
Public Event addtolist(ByVal s as string)
so your event handler has to look the same:
Private Sub AddToListbox(ByVal str As String)
You don't have to have the same variable names, just the same types in the same order.
|
|
|
|
|
i tried it the way you said but it isn't working.
In the parent form, I created on object of the child form and put the AddHandler syntax in the form load sub.
|
|
|
|
|
What do you mean "it isnt' working"? What IS happening? Have you steped through the code using the debugger to see where it's going and what it's doing??
|
|
|
|
|
Yes I have done it..when it reaches the RaiseEvent it doesn't go the event handler..it just goes to the next line.
|
|
|
|
|
That would probably be because there is nothing subscribed to the event.
|
|
|
|
|
well I have done exactly as you said.
This is in the parent form,I have added this in the form load event of the parent form
Dim obj1 As New childform
AddHandler obj1.addtolist, AddressOf addtolistbox
and this is sub that is subscribed to the event..
Private Sub addtolistbox(ByVal str As String)
ListBox1.Items.Add(str)
End Sub
In the child form....
Public Event addtolist(ByVal s As String)
and in the sub where I want to add to listbox in parent form,
RaiseEvent addtolist(ds1.Tables(0).Rows(0)("Reader_location").ToString & " Connected ")
|
|
|
|
|
salam sir;
how can made login form in vb6 and vb.net?please send me coding
thanks sir
mohammedali
|
|
|
|
|
1. Cross post
2. We don't do your work for you
3. This can easily be found on the internet.
Blog link to be reinstated at a later date.
|
|
|
|
|
Damn dude, don't spam the boards. Doesn't VB.NET express 2008 have a login form template for Christs sake??
|
|
|
|
|
Awesome question!
snicker..snicker..
Any suggestions, ideas, or 'constructive criticism' are always welcome.
"There's no such thing as a stupid question, only stupid people." - Mr. Garrison
|
|
|
|
|
whats worse is he asked this question months ago. Same project or did he retake the course?
10:1 its the same project
.5:1 its a redo on the course
Any takers?
|
|
|
|
|
I'd bet on the same proj. Sad really...
Any suggestions, ideas, or 'constructive criticism' are always welcome.
"There's no such thing as a stupid question, only stupid people." - Mr. Garrison
|
|
|
|