|
yeah its workin man....
u rock buddy..
thanks a lot..
can i have yr ID..?
|
|
|
|
|
That code only appears to work but has a nasty consequence. Your just hiding the origninal forms and then createing a new instance every time you go back. So you have form1 which creates and shows form2 while hiding itself. You click to go back on form2 which creates a NEW instance of form1 and hides itself. Now you have TWO form1's and 1 form2. However only 1 instance of form1 is actually visible, but there are definitly two and the number of instances will continue to increase every time you go back and forth. As proof put this code in form1.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim output As String = "Open forms: " & vbCrLf
For Each frm As Form In My.Application.OpenForms
output &= frm.Text & vbCrLf
Next
MsgBox(output)
End Sub
When form1 is shown the second time you'll see there are actually two in existence.
-- modified at 14:29 Sunday 25th February, 2007
|
|
|
|
|
You can load, unload or hide a form according to form's properties. If we were unloading the form, how we could easily reload it again? If forms were not a class actually, we wouldn't need to create a new object instance.
You are right about it but what do you suggest? The common language runtime (CLR) throws an OutOfMemoryException error if there is insufficient memory to create the new instance. You can control the exception. If user doesn't want to minimize/maximize the form, it becomes unfortunate to go that way.
Thanks for your code!
What a curious mind needs to discover knowledge is noting else than a pin-hole.
|
|
|
|
|
The first form needs to have a global variable to track the form it wants to call. If it already exists then we just show it instead. The only reason the following code is a problem is because in your case instead of closing form2 you hid it. Thus everytime you clicked the button a new form was created and the hidden forms stayed hidden.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim frm As Form = New Form2
frm.Show()
me.hide
End Sub
It's probably better to close form2 if that's possible which avoid the problem altogether. In that case this code is just fine.
If the form creating form2 is the main form you can't close it so hiding it is your only option. If you can close form1 then again you could avoid the whole problem of many instances by doing so. That way when form2 makes a new instance we don't leave hidden form1's all over the place. If you do need to hide form1 then you could pass the current instance onto form2 so form2 knows which form called it and can show that form again. This sample demonstrates all of this. It's not complex and I'm sure there are other ways. I think the technique used would depend on the situation. But here is one option.
Public Class Form1
Dim frm As Form2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Create a new instance of Form2 if we don't have one
'Notice I passed this instance in the constructor
If frm Is Nothing OrElse frm.IsDisposed Then frm = New Form2(Me)
frm.Show()
Me.Hide()
End Sub
End Class
Public Class Form2
Private m_Caller As Form 'The calling form
Public Sub New(ByVal caller As Form)
InitializeComponent()
m_Caller = caller
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
m_Caller.Show()
Me.Hide() 'You could also call me.close instead which may be more appropriate
End Sub
End Class
|
|
|
|
|
Thanks for your advice, your advice is very acceptable but what I think is that tracking the each form like that would make coding little bit annoying. May be you are right about it there should be some other ways (more practical).
Thanks again!
What a curious mind needs to discover knowledge is noting else than a pin-hole.
|
|
|
|
|
Well forms are classes and you need to create an instance so you can show it or work with it. If another form needs to interact with that form then one way or another it needs to know which instance it's dealing with, because there could be many. If you had a form where only one instance should exist you could create a singleton form. Try looking that term up and you should find some examples. That's certainly another approach which may be appropriate for a given situation. There are also other scenarios. For instance if you didn't hide form1 you wouldn't need to show it again and form2 wouldn't have to know about it. But if that's the way you want it to work then form2 will need to know which instance it should show. Truth be told though it is still possible to use forms the old VB6 way. I'm sure many people would frown upon that and I am not going to encourage it but it's an option. For example create a new project and add a a second form with the default name form2. Replace the code in both forms with the following.
Code for Form1
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Form2.Show()
Me.Hide()
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Set the text to the time of creation so we can tell this
'we are dealing with the same instance
Me.Text = Now.ToLongTimeString
End Sub
End Class Code for Form2
Public Class Form2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Form1.Show()
Me.Hide()
End Sub
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Set the text to the time of creation so we can tell this
'we are dealing with the same instance
Me.Text = Now.ToLongTimeString
End Sub
End Class
|
|
|
|
|
This cannot be applied for forms because a form instance can be triggered from different procedures at same time. Therefore, sticking with creation time is not appropriate. Think about two timer components. Each timer shows the same form in every second because the each timer works independently.
The most practical way should be the set the caller form as parent form and then in 2nd form redirect user to parent form.
I wish .net would have a single statement to solve this problem instead of bunch of coding.
What a curious mind needs to discover knowledge is noting else than a pin-hole.
|
|
|
|
|
B Journey wrote: The most practical way should be the set the caller form as parent form and then in 2nd form redirect user to parent form.
That's exactly what I did in the first example. Form2 excepts a caller, 'the parent', so it knows who to talk to. The thing that seems to bother you is the 4 lines of code it takes to do that. Sorry, I just don't consider that a "bunch of coding". It's a pretty simple straighforward solution. Not to mention the fact that you could actually create your own extended form class with this code in it. Then whenver you need a form like this you would inherit from this class. You would have no need to write that code ever again
Also there is another way. There always seems to be another way, huh. The show method is overloaded and excepts an 'owner' form. You could pass your forms instance when you call the show method. Every form has an 'Owner' property. You could then access the caller through this property from form2. One thing to note about an owned form is that it will minimize whenever the owner minimizes and restore whenver it's restored. This may be desired, and in your case it doesn't even matter because the owner would be hidden and couldn't be minimized. So for example
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim frm As New Form2
frm.Show(Me)
End Sub
Form2 could then use the code Me.Owner.Show to show it's parent.
B Journey wrote: This cannot be applied for forms because a form instance can be triggered from different procedures at same time. Therefore, sticking with creation time is not appropriate. Think about two timer components. Each timer shows the same form in every second because the each timer works independently.
I'm not sure why you think this (the code I posted last time) can't be applied to forms. It most definitly can, and does work just fine. The reason I showed the creation time was to clearly demonstate the same instance of the form was being shown whenver you called Form1.show or Form2.show. The point of the code was to demonstrate that in vb.net there is already a built in way to show a form without having to worry about creating a new instance or tracking the old instance. Form2.Show will create a new instance for you if one doesn't already exist, and if it does that same instance will be shown (sound familiar). It should because that's exactly what I did in the original post I made. Now if you meant it wasn't appropriate because you thought each timer needed to show a different instance of the other form then yes of course that code woulnd't be appropriate. As I've stated from the begining different situation call for different solutions.
-- modified at 15:44 Tuesday 27th February, 2007
|
|
|
|
|
what is 192 bit encryption and how we encorporate in our programming
namita
|
|
|
|
|
192 bit encryption is an encryption that uses a key with a size of 192 bits.
Look in the System.Security.Cryptography namespace for encryption classes.
---
single minded; short sighted; long gone;
|
|
|
|
|
Sir/Madam,
I wanted to implement the events and delegates in my program.Can somebody please provide a code link
Thanks and regards
Pankaj
|
|
|
|
|
In some cases you may want to declare an event to use an existing delegate type as its underlying delegate. The following syntax demonstrates how:
Delegate Sub MyDelegateType()
Event MyEvent As MyDelegateType
Here is the link:
http://www.codeproject.com/vb/net/Delegate.asp[^]
What a curious mind needs to discover or penetrate a knowledge is noting else than a pin-hole.
|
|
|
|
|
Sir/Madam
I wanted to do code in the application event file .Can somebody please provide help.
Thanks and regards
Pankaj
|
|
|
|
|
An application has more than one evets such as StartupNextInstance, ThreadException,ApplicationExit and etc...
Try to be specific which application event you want to use. I strongly suggest you to examine MSDN about Application events.
What a curious mind needs to discover or penetrate a knowledge is noting else than a pin-hole.
|
|
|
|
|
Sir/Madam,
I wanted to know something about ApplicationExit ,ThreadException,StartupNextInstance
Please help
Thanks and regards
Pankaj
|
|
|
|
|
There are many events related to application. That is why you should browser application events under MSDN to examine each of them.
Application.Exit ()
Informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed.
Application.ThreadException
Occurs when an untrapped thread exception is thrown.
please apply to MSDN for others
What a curious mind needs to discover knowledge is noting else than a pin-hole.
|
|
|
|
|
Sir/Madam
There are two terms in Authenticatin mode for desktop application namely
Windows<br />
Aplication defined
Can i get to know the basic difference between them.By default the application provides Window Authentication mode.
Thanks and Redards
Pankaj
|
|
|
|
|
Sir/Madam
I wanted to know the basic difference between option explicit and option strict.I meant that how should i use these terms in my coding
Thanks and Regards
Pankaj
|
|
|
|
|
OPTION EXPILICIT Forces explicit declaration of all variables in a file.
Option Explicit { On | Off }
OPTION STRICT Restricts implicit data type conversions to only widening conversions.
Option Strict { On | Off }
Define these statements on top of the page.
What a curious mind needs to discover or penetrate a knowledge is noting else than a pin-hole.
|
|
|
|
|
Sir/Madam,
If u please explain with the help of an example.
Thanks and Regards
Pankaj
|
|
|
|
|
' FOR OPTION EXPLICIT ON
Dim thisVar As Integer
thisVar = 10
' The following assignment produces a COMPILER ERROR because
' the variable is not declared and Option Explicit is On.
thisInt = 10 ' causes ERROR
'FOR OPTION STRICT
Dim thisVar As Integer
Dim thisObj As Object = New widget
thisVar = 1000 ' Declared variable does not generate error.
' Attempting to convert Double to Integer generates a COMPILER ERROR.
thisVar = 1234567890.9876542 ' causes ERROR
' Late-bound call generates a COMPILER ERROR.
Call thisObj.Method1() ' causes ERROR
Please admit to MSDN..
What a curious mind needs to discover knowledge is noting else than a pin-hole.
|
|
|
|
|
Sir/Madam,
I was trying a compression program in vb.net.I did it well in C++.But it is not getting possible in vb.net.Can somebody please send the code project link that illustrate the utility of single and double link list
in vb.net
Thanks and regards
Pankaj
|
|
|
|
|
Hi there, I am new to VB.NET and I would like to create a data grid on a form, which allows me to add data, and save to a database.
The fields on the Grid will be text box, Combo box, and another to allow the user to browse to find a file location.
I have been unable to successfully create this, and I also am unable to find any decent tutorials. Can anyone help please?!
Your help is appreciated,
Maz
|
|
|
|
|
Hi there,
I Need to embed virus checking into my appliction.
If any1 has any clue / info on this topic please let me know
Thank you!
M. Nauman Yousuf
|
|
|
|
|
Virus checking is a big deal. Companies make their entire business on doing it well. It's doubtful that you can add it to your app as an afterthought. To compete, you need to dedicate many man years of development.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|