Showdialog and Focus (incredible) - vb.net

I noticed a strange problem in VB.NET 2010.
If I call showdialog in a TextBox_LostFocus, when I close the form it causes a Loop, but ONLY if I pass from the first textbox to the second USING THE MOUSE! If I use tab it works normally.
I show whith an example:
Create New project and create two forms, form1 and form2
In form1, create two textboxes.
In the textbox1.lostfocus, paste the following code
VB.NET Code:
Dim f As New Form2
f.ShowDialog()
Now: the focus is on textbox1. Right?
If you move to textbox2 with tab key, form2 appears. If you close it it's all normal
BUT if you move to textbox2 using the mouse, form2 appears and if you close it another form2 appears, until 23 times when you get an exception
I workarounded it moving the "dim" statement in declaration area of form1, but anyone has an idea of why of that behaviour?
Thanks in advance.

Related

How to show an existing form in a project

Newbie alert!
I'm trying to show another form when a user successfully inputs the correct username and password. Like a login screen
But on button click event, the code below is highlighted
Form2.Show()
The second form won't show and the code above has wiggly lines below it
This error shows: Property access must assign to the property or use its value
What can be the problem?
You should create an instance of your form.
On button click event,
Dim frm2 As New Form2()
frm2.Show()
Do not use shared (static) method to display form. Use instance method instead.
Code below will display Form2 modally and after that, disposes it.
Using f2 As New Form2
f2.ShowDialog(Me)
End Using

Closing a Dialog Form and Open another form

I've got (what I believe to be) a weird issue with closing a form and opening another.
I have a button on a particular form (that is opened using the .ShowDialog if that makes any difference), when the button is clicked, the following code runs:
Me.Close()
LC.ShowDialog()
I would expect that the form containing the button should close and the LC form should open as a dialog form. What actually happens is that the Me form stays open and the LC form appears behind it with main focus.
Why would this be?
UPDATE 1
Just to clarify the set up of the forms:
Form1 opens the me form as a Dialog (Where Form1 is the main form that is launched on startup)
The me form opens the LC form and should close in the process
Ok it seems you might have a conception issue...
Let's get this through, if you call me.close(), you are asking your form to terminate itself, and to kill all forms he generated.
However, it is not direct, a windows Message is posted to your application that will be treated whenever you function is done.
Then right after that, you create a new form and you say you want to wait for it to close to continue.
I don't know what your purpose is but you have a few solutions :
If you want to go back to your main form (Form1) when done with LC :
Me.Hide()
LC.ShowDialog()
=>Your code will pick up here when LC will be closed
Me.Show()
If you don't want your Form1 to open ever again, then don't make it your starting form, or start working in the Sub Main()
make the form1 a flowmanager of the forms. I mean, form1 should opens the lc and close the me.
You must call both Dialog Forms from a parent form.
You may do something like this:
PARENT FORM
FirstDialog.Showdialog
(the app will open this dialog and wait it close)
SecondDialog.Showdialog
Doing this way the second dialog will open ONLY when the first one be closed.
You cannot start another dialog from a dialog being closed.
Good luck

How do I Restart a Form

form1 has a button on it that goes to form2. Form2 has a picturebox with the image of a bird.
When the picturebox on form2 is clicked the picturebox disappears and form2 hides itself and form one comes back up. This is a model for a more complicated program but has the same problem. After returning to form1 for the second time if i click the button that brings me to form2 the image of the bird will not show up because form2 returns to its last previous state. What can i do to make form2 refresh / restart / run from the beginning again?
Any help would be appreciated.
You have two options here.
Destroy form2 completely when you go back to form1 so the next time you go to form2 it's brand new.
When form2 comes into view set the image view back to visible
Update
Here you go.
Dim frm As New Form1
frm.ANewMethod
Set frm = Nothing ' Form is destroyed.
And here is a link to the documentation http://msdn.microsoft.com/en-us/library/aa242139(v=vs.60).aspx
you can also use Me.Dispose in every closing_event of your form.

Showing form 2 before form 1

I have this VB.NET program that I wrote in Windows Visual Studio 2010 and there are two forms. The form1 will go first before form2 but now I changed my mind and I want form2 to load first when I open my program before form1. I have tried a lot of troubleshooting but no luck form1 is still loading first.
Double click on My Project in Solution Explorer. There you can set the Startup form to form2.
It sounds as if you have a straightforward windows forms project that does not have a sub main procedure (you should read up about sub main procedures as with that you could follow the advice in the in the comment below your question).
For your problem you need to click on 'My Project' in the solution explorer and then open up (if it doesn't open by default) the application tab. There are three drop down controls on the left hand side of the tab the last of which is 'Startup Object'. Under this you will find 'Form2'. Select that and your application will start with form 2 rather than form 1.
Go to the project menu and click " Properties" (the bottom option). In the tab that opens, change the drop down list labelled "Startup Form" to whichever form you want to show first.
Hope this helps!
The following steps may help out:
Click on form1, go to its Topmost property and set it to false.
Click on form2, go to its Topmost property and set it to true.
Double click on form1 and write the following code:
me.hide()
form2.show()
Double click on form2 and write the following code:
me.show()
form1.hide()
I have tried and it worked.
Please navigate on solution Explorer and click program.cs then change Application.Run(new nameoftheformyouwanttostartwith());
on where its written name of the for you want to start it then enter then name

vb.net Prevent Form 1 from activating when form 2 is clicked

im having a similar problem like the solution here Prevent main form from appearing when showing another form . but some of the suggestions were to minimize the main app so it doesnt show, which i cant do because my main app is supposed to be a desktop to be underneath all other apps to replace the windows desktop. And the second forms are supposed to be sticky notes. so i cant minimize the main window cause it has the user background and other controls. i tried making the parent of the notes a Nothing pointer, a pointer to the desktop, creating the form through a dll but i had no success.
My main problem is that when i click a note (form2) form1 comes up, even with form1 having the WS_EX_NOACTIVATE in the createparams. form1 does the form2.show() but they shouldn't be attached.
Another reason im having trouble with the solutions preseted in that post is that they are for delphi and im doing it in vb.net.
All i need is being able to click on the controls and write in the note without bringing the main form behind the note. either making them independent, or making the note not focusing the first form or being able to operate the note without it activating. i dont know. my last resource is to attach my main form to the desktop but i've heard is the worst thing you can do because it can cause problems hanging the system.
If you want both forms to co-exist but don't each to interfere with the other: In this case, then you might want to have a third Form that calls both Form1 and Form2 to be opened, and let me suggest and MDI Form with Form1 and Form2 as children forms of the MDI form
'============== my previous post ========================
You can force the user to first dismiss Form2 then allow him to go back to form1 by showing Form2 as Modal form. Here is how to display Form2 as modal
Dim f2 as New Form2
f2.ShowModal()
If that does not work, try this
Dim f2 as New Form2
f2.Show(True)