|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Q: closing formsHi
I'm wondering if anybody can help me with the following problem. Suppose I have 3 forms: A, B and C. Pressing a button on form A, shows (using ShowDialog) form B. Pressing a button on form B, shows (using ShowDialog) form C. I want to have a button on form C which, when pressed, closes form C and form B, revealing form A. Can anybody help? Some example code would be great. Thanks in advance G There are several ways to do this.
You could publicly declare your forms in a module too make them accessible anywhere. On Sub Main open the first form. Then write a sub called "OpenForm(byRef f as form)" Then write another called "CloseForm(ByRef f as form)" You get the idea. OR You could pass form B too form C using ByRef in the New event of form C. Then on closing of form C call B.Close() Izzy G .Net wrote: Show quoteHide quote > Hi > > I'm wondering if anybody can help me with the following problem. > > Suppose I have 3 forms: A, B and C. > > Pressing a button on form A, shows (using ShowDialog) form B. > > Pressing a button on form B, shows (using ShowDialog) form C. > > I want to have a button on form C which, when pressed, closes form C and > form B, revealing form A. > > Can anybody help? Some example code would be great. > > Thanks in advance > > G Thanks Izzy. That's a great idea!
Show quoteHide quote "Izzy" <israel.rich***@gmail.com> wrote in message news:1159806496.962206.184680@b28g2000cwb.googlegroups.com... > There are several ways to do this. > > You could publicly declare your forms in a module too make them > accessible anywhere. > On Sub Main open the first form. > > Then write a sub called "OpenForm(byRef f as form)" > Then write another called "CloseForm(ByRef f as form)" > > You get the idea. > > OR > > You could pass form B too form C using ByRef in the New event of form > C. Then on closing of form C call B.Close() > > > Izzy > > > G .Net wrote: >> Hi >> >> I'm wondering if anybody can help me with the following problem. >> >> Suppose I have 3 forms: A, B and C. >> >> Pressing a button on form A, shows (using ShowDialog) form B. >> >> Pressing a button on form B, shows (using ShowDialog) form C. >> >> I want to have a button on form C which, when pressed, closes form C and >> form B, revealing form A. >> >> Can anybody help? Some example code would be great. >> >> Thanks in advance >> >> G > Izzy wrote:
> You could pass form B too form C using ByRef in the New event of form Incidentally, and this seems to be a very common misconception, there is no > C. Then on closing of form C call B.Close() need to pass form B to form C ByRef (by reference). You can (and most likely should) pass it ByVal (by value). When you declare an object parameter by value, it is only the pointer to the object that is passed by value. It is still the same object that it points to. So in this instance, passing form B to form C by value would not create a new instance of form B, it would just create a new pointer to the existing instance. The only real difference between passing by reference and by value takes place if the code to which it is passed makes a modification to the pointer, in which case the original reference will be changed too if passed by reference, or left unchanged if passed by value. For example: \\\ 'Class-level form declaration so we can see it at all times Public a As MyForm Public Sub Test 'Create a new instance of a form a = New MyForm 'Call a procedure, passing the object reference by value MyProcByValue(a) 'Do we still have a valid form reference? If a Is Nothing Then MsgBox("After call using ByVal, a is Nothing") 'Call a procedure, passing the object reference by reference MyProcByReference(a) 'Do we still have a valid form reference? If a Is Nothing Then MsgBox("After call using ByRef, a is Nothing") End Sub Private Sub MyProcByValue(ByVal f As MyForm) 'Is this the form we created earlier? If f Is a Then MsgBox("ByVal: This is our existing form") 'Set f to nothing f = Nothing End Sub Private Sub MyProcByReference(ByRef f As MyForm) 'Is this the form we created earlier? If f Is a Then MsgBox("ByRef: This is our existing form") 'Set f to nothing f = Nothing End Sub /// Try pasting that into VB and running it. You'll see that in both of the "MyProc..." procedures, form "f" really is the same form as form "a", regardless of the ByRef or ByVal parameter. However after returning from the ByVal procedure, the original object reference "a" will be unchanged, but after returning from the ByRef procedure it will have been set to nothing. Hopefully that makes sense anyway. In my experience there are very few occasions where you need to pass parameters by reference. It is only when you need to be able to modify the value in the calling procedure that you should ever do this, and it is well worth documenting the fact that you have declared a variable in this way and the reason for doing so. Unnecessary use of ByRef parameters can easily introduce difficult-to-diagnose errors into your code. I hope that was of some use. -- (O)enone Interesting....
Let me ask you this, if we pass form B into form C using ByVal: A) Does this NOT use any additional memory? B) Does calling B.Close() and B.Dispose() within form C, actually close and dispose of the original object? Just like calling it from form B itself. Thanks, for jumping in. Because I was under the impression using ByVal creates a seperate object like the first. This is good to know. Thanks, Izzy Oenone wrote: Show quoteHide quote > Izzy wrote: > > You could pass form B too form C using ByRef in the New event of form > > C. Then on closing of form C call B.Close() > > Incidentally, and this seems to be a very common misconception, there is no > need to pass form B to form C ByRef (by reference). You can (and most likely > should) pass it ByVal (by value). > > When you declare an object parameter by value, it is only the pointer to the > object that is passed by value. It is still the same object that it points > to. So in this instance, passing form B to form C by value would not create > a new instance of form B, it would just create a new pointer to the existing > instance. > > The only real difference between passing by reference and by value takes > place if the code to which it is passed makes a modification to the pointer, > in which case the original reference will be changed too if passed by > reference, or left unchanged if passed by value. > > For example: > > \\\ > > 'Class-level form declaration so we can see it at all times > Public a As MyForm > > Public Sub Test > 'Create a new instance of a form > a = New MyForm > > 'Call a procedure, passing the object reference by value > MyProcByValue(a) > 'Do we still have a valid form reference? > If a Is Nothing Then MsgBox("After call using ByVal, a is Nothing") > > 'Call a procedure, passing the object reference by reference > MyProcByReference(a) > 'Do we still have a valid form reference? > If a Is Nothing Then MsgBox("After call using ByRef, a is Nothing") > End Sub > > Private Sub MyProcByValue(ByVal f As MyForm) > 'Is this the form we created earlier? > If f Is a Then MsgBox("ByVal: This is our existing form") > 'Set f to nothing > f = Nothing > End Sub > > Private Sub MyProcByReference(ByRef f As MyForm) > 'Is this the form we created earlier? > If f Is a Then MsgBox("ByRef: This is our existing form") > 'Set f to nothing > f = Nothing > End Sub > /// > > Try pasting that into VB and running it. You'll see that in both of the > "MyProc..." procedures, form "f" really is the same form as form "a", > regardless of the ByRef or ByVal parameter. However after returning from the > ByVal procedure, the original object reference "a" will be unchanged, but > after returning from the ByRef procedure it will have been set to nothing. > > Hopefully that makes sense anyway. > > In my experience there are very few occasions where you need to pass > parameters by reference. It is only when you need to be able to modify the > value in the calling procedure that you should ever do this, and it is well > worth documenting the fact that you have declared a variable in this way and > the reason for doing so. Unnecessary use of ByRef parameters can easily > introduce difficult-to-diagnose errors into your code. > > I hope that was of some use. > > -- > > (O)enone Izzy,
> Not with a reference type, a value is always passed by its value> A) Does this NOT use any additional memory? > B) Does calling B.Close() and B.Dispose() within form C, actually close Why do you not try that yourself, I never do it that way,> and dispose of the original object? Just like calling it from form B > itself. > Cor Izzy wrote:
> Interesting.... It uses the additional memory required to create a new pointer to the object > > Let me ask you this, if we pass form B into form C using ByVal: > > A) Does this NOT use any additional memory? (i.e., 4 bytes on 32-bit Windows). Not a huge overhead. :) > B) Does calling B.Close() and B.Dispose() within form C, actually Yes -- as it is still the original object. It's only the pointer to the > close and dispose of the original object? Just like calling it from > form B itself. object that's different. Let's see if an analogy helps -- hopefully this isn't too twisted. :) Imagine that there is a street full of houses, and one of the houses is 123 Coder Street. You've written the address of 123 Coder Street on a piece of paper. If you pass me the address of the house by reference, you are actually handing me your piece of paper. I can then go to the house and paint the front door blue. If I change what is written on the paper to a different address, when I give it back to you then your piece of paper will contain my modification. Alternatively you can pass me the paper by value, by writing the address on a new piece of paper and giving me that. It still reads "123 Coder Street" and I can still go to the house and paint the front door, it's exactly the same house as before. The difference is that when I've finished with my own piece of paper, I'll throw it away instead of giving it back to you, so if I decide to change the address on the paper, your own piece of paper won't be affected. > Thanks, for jumping in. Because I was under the impression using ByVal No problem -- this does seem to be something that a very large number of > creates a seperate object like the first. This is good to know. people misunderstand. -- (O)enone Very good analogy, now I understand completely. Thanks for the info.
Izzy Oenone wrote: Show quoteHide quote > Izzy wrote: > > Interesting.... > > > > Let me ask you this, if we pass form B into form C using ByVal: > > > > A) Does this NOT use any additional memory? > > It uses the additional memory required to create a new pointer to the object > (i.e., 4 bytes on 32-bit Windows). Not a huge overhead. :) > > > B) Does calling B.Close() and B.Dispose() within form C, actually > > close and dispose of the original object? Just like calling it from > > form B itself. > > Yes -- as it is still the original object. It's only the pointer to the > object that's different. > > Let's see if an analogy helps -- hopefully this isn't too twisted. :) > > Imagine that there is a street full of houses, and one of the houses is 123 > Coder Street. You've written the address of 123 Coder Street on a piece of > paper. > > If you pass me the address of the house by reference, you are actually > handing me your piece of paper. I can then go to the house and paint the > front door blue. If I change what is written on the paper to a different > address, when I give it back to you then your piece of paper will contain my > modification. > > Alternatively you can pass me the paper by value, by writing the address on > a new piece of paper and giving me that. It still reads "123 Coder Street" > and I can still go to the house and paint the front door, it's exactly the > same house as before. The difference is that when I've finished with my own > piece of paper, I'll throw it away instead of giving it back to you, so if I > decide to change the address on the paper, your own piece of paper won't be > affected. > > > Thanks, for jumping in. Because I was under the impression using ByVal > > creates a seperate object like the first. This is good to know. > > No problem -- this does seem to be something that a very large number of > people misunderstand. > > -- > > (O)enone G.
Ever tried it like this. \\\Form B Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load Dim frmB As New Form2 Dim A As Integer = frmB.ShowDialog() If A = Windows.Forms.DialogResult.OK Then 'do what you want to do ElseIf A = Windows.Forms.DialogResult.Yes Then Me.Close() End If End Sub /// \\form C Me.DialogResult = Windows.Forms.DialogResult.Yes Me.Close() /// I hope this helps, Cor Show quoteHide quote "G .Net" <nodamnspam@email.com> schreef in bericht news:feudnWQbZ6JfrbzYRVnygA@pipex.net... > Hi > > I'm wondering if anybody can help me with the following problem. > > Suppose I have 3 forms: A, B and C. > > Pressing a button on form A, shows (using ShowDialog) form B. > > Pressing a button on form B, shows (using ShowDialog) form C. > > I want to have a button on form C which, when pressed, closes form C and > form B, revealing form A. > > Can anybody help? Some example code would be great. > > Thanks in advance > > G > Hi Cor
Yes, that would work. I'll experiment with it. G Show quoteHide quote "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message news:usVUg7p5GHA.1244@TK2MSFTNGP03.phx.gbl... > G. > > Ever tried it like this. > \\\Form B > Private Sub Form1_Load(ByVal sender As System.Object, _ > ByVal e As System.EventArgs) Handles MyBase.Load > Dim frmB As New Form2 > Dim A As Integer = frmB.ShowDialog() > If A = Windows.Forms.DialogResult.OK Then > 'do what you want to do > ElseIf A = Windows.Forms.DialogResult.Yes Then > Me.Close() > End If > End Sub > /// > \\form C > Me.DialogResult = Windows.Forms.DialogResult.Yes > Me.Close() > /// > I hope this helps, > > Cor > > "G .Net" <nodamnspam@email.com> schreef in bericht > news:feudnWQbZ6JfrbzYRVnygA@pipex.net... >> Hi >> >> I'm wondering if anybody can help me with the following problem. >> >> Suppose I have 3 forms: A, B and C. >> >> Pressing a button on form A, shows (using ShowDialog) form B. >> >> Pressing a button on form B, shows (using ShowDialog) form C. >> >> I want to have a button on form C which, when pressed, closes form C and >> form B, revealing form A. >> >> Can anybody help? Some example code would be great. >> >> Thanks in advance >> >> G >> > >
CURRENT INDEX of Iteration on ILIST collection ?
1.1 equivalent for Drawing.Icon.ExtractAssociatedIcon? Help with using bits in an integer Strings, a couple of questions like difference between += and &= Accessing the Class Name of a Shared Method copy and paste into webbrowser new form Set Statement vs Function chm and application.startuppath Can't run Web Services |
|||||||||||||||||||||||