vb.net: Using a Button on a Form to open another form, doesn't work - vb.net

I encounter the following scenarios when I try to use 2 forms. My workflow is as follows:
(1) Load Form1.
(2) A click on button1 on Form1 closes Form1 and opens Form2.
Solution A: If I use the following code:
Dim oForm As New Form2
oForm.ShowDialog()
Me.Close()
Then Form1 will be under Form2 (Form1 still opens).
Solution B: If I use the following code:
Dim oForm As New Form2
oForm.Show()
Me.Close()
Then Form1 closes and Form2 opens, but Form1 is not on the top layer.
I have looked through the solutions for this, most propose solution B, but for me, both solutions won't work the way I want. Can anybody tell me the reason?

Try
Dim oForm as New Form2
oForm.Show()
and on Load event of form2
Form1.Hide()

Use form.bringtofront() if you want to see the opening Form in the front, I m little confused though about what you are trying to do

Try doing it this way:
Dim oForm As New Form2()
Me.Hide()
oForm.ShowDialog()
Me.Close()

I suspect your are building a log-in dialogue... if so, or something similar, try this..
Open your main form first... (Form 2), have form2 showdialog (modally) form1... this will put form1 on top of form2.
Add a property to form 1, that gets set depending on what happens there.. sucessfull login for instance.
Close form 1 from its own methods... (after successful authentication), set the property before closing.
On form2, read this property of form1, and then dispose form1, and decide what to do... if unsuccessful login, show the login form again, end app. If successful, just gracefully exit out of the method that showed form1. Your form 2 is now the only form open.
Start with Form2
Form2_load
dim f1 as new form1
f1.showdialog
if f1.someproperty = somevalue then
' do something here, for instance, pop the form again, if you did not get what you were lookign for...
end if
'gracefully let the function end and form2 is now the only open form..
'dispose of form1. form1's close call does not dispose it, because it was opened modally. (showdialog)
f1.dispose
f1 = nothing
in form1, depending on what you are doing, set the custom property and call me.close, this will exit the form, and run the next code in form2.

try this:
Dim oForm As New Form2
oForm.Show()
Me.Visible = False
You would to close your first form and this close your program. If you set him on invisible, he is not closed.

Just go to form2 and write the Form1.hide() . I tried to close form1 but it closed my whole program.
Public Class Form2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Form1.Hide()
End Sub
End Class

Related

Accessing text box on Form1 from Form2

I am new to vb.net so forgive me if this is an easy question.
I have a created a class library project that houses two windows forms, Form1 and Form2. The main class library has the event to open Form1. A button on Form1 launches Form2. The bulk of the code is in Form1, which I don't want to change if I can help it.
What I am trying to do, is access a sub that is on Form1 from Form2. That sub is changing the value of a text box on Form 1. I don't get any errors when I compile the project, however, nothing happens.
Here is an example
Form1:
Public Sub test()
Me.Panel1.Controls("Textbox1").Text = "Test"
End Sub
Form2:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim MainForm As New Form1
Me.Close()
MainForm.test()
End Sub
Don't get too caught up on how I built it out, I have tried about 20 different things and this is where I am at now.
I have tried defining Form1 in the sub test(). I have tried setting sub test() to shared. I have tried closing Form2 and activating Form1. I have tried changing the modifiers property on the text box to public. I have tried making Form1 the parent and Form2 a child (I honestly don't understand MDI very much). All these results end up in a project that will compile but wont give me any results. My code accesses the sub just fine, it wont access the text box's text property.
Any suggestions will help. I am trying to access the text boxes in a way that I can loop through all of them. For example: Me.Panel.Controls("Textbox" & i).Text = "Something". Also I would like to keep the sub in the class for Form1 if I can.
Any suggestions would be great!
You are creating a brand new Form1 in Form2, that is the problem.
Just use :
Call Form1.test()
By the way, I think this code in the sub is an easier way to set the text:
Panel1.TextBox1.Text = "Test"

Loading a form dynamically [duplicate]

It takes two forms to be able to enter all the info on a particular transaction. I want to be able to flip back and forth between these two forms retaining what was entered on each until the 'Save' button is clicked.
I think I should be able to use Form2.Show, Me.Hide and then Form1.Show, Me.Hide.
The first time I go to Form2 it goes thru the Form2 load event (that’s reasonable) but any knowledge of the control contents on Form1 have been lost. Even though Form1 is hidden (and not closed) the contents of its controls are gone. Why?
The second time I go to Form2 the load event does not fire because Form2 is hidden and at this point all the Form1 control contents are available. So, in flipping back and forth between Form1 and Form2 it works as I want it to after I go to Form2 the second time. But, I need it to work the first time and every time.
For days I’ve been trying to understand this. I’ve commented out nearly every line of code, stepped thru code, googled till I’m blue in the face (there has been a lot written about this), and I still can’t figure out why this behavior occurs.
Can anyone explain this phenomenon? Or better yet tell me what I need to do to make this work.
I have this code behind the Form1 button that goes to Form2
If Form2 Is Nothing Then
Dim Form2 As New Form2
End If
Form2.Show()
Me.Hide()
And this code behind the Form2 button to return to Form1
Form1.Show
Me.Hide
This is all you are probably missing:
Class Form1
Private f2 As Form2 ' this is Form1's reference to the
' form2 instance
Later when you click to go to form2, your original code just needs a small tweak:
If f2 Is Nothing Then
f2 = New Form2(Me) ' set declared variable to new instance
End If
F2.Show()
Me.Hide()
In this case Form1 is passing the reference using the trick you were shown before using the constructor:
Sub New(frm As Form1) ' this is in Form2 only
f1 = frm
End Sub
You dont need this in Form1 because he/it is creating his own f2 object reference.
The main problem in your original code, was: Dim Form2 As New Form2. You are creating a new Form2 each time (I suspect that resides in an event or sub). Those new instances can't know the control values in the previous instances. Declaring F1 or F2 as shown gives it module/form level Scope.
Dim declares a variable and its Type. f1 is of Type Form1. It does not create an object if it is an object variable
New creates an instance of an object Type (reference types). This directly relates to the Sub New method in the class. When you use New, Sub New is called so anything special that is needed can take place there. Value types like Integer do not need to be created or instanced, only declared.
Where you declare (Dim) a variable determines its Scope. If you do this in a Sub, the variable or object only exists in that sub. if you do it at the form/class level, it has Form/Class level scope.
I would try something like this. Display the 2 forms modally using the ShowDialog method.
In each form, create a property (call it "OtherForm") which points to an instance of the other form.
Continue the loop by setting the DialogResult of either form to Windows.Forms.DialogResult.Yes
Exit the loop by setting the DialogResult of either form to Windows.Forms.DialogResult.No
This will execute each forms load event each time it is displayed.
So call this from a button click event procedure on your so-called Form0.
Dim Form1 As New Form1
Dim Form2 As New Form2
Dim sFormToShow As String = "Form1"
Form1.OtherForm = Form2
Form1.DialogResult = Windows.Forms.DialogResult.Yes
Form2.OtherForm = Form1
Form2.DialogResult = Windows.Forms.DialogResult.Yes
Do
If sFormToShow = "Form1" Then
Form1.ShowDialog()
ElseIf sFormToShow = "Form2" Then
Form2.ShowDialog()
End If
sFormToShow = IIf(sFormToShow = "Form1", "Form2", "Form1")
Loop While Form1.DialogResult = Windows.Forms.DialogResult.Yes AndAlso Form2.DialogResult = Windows.Forms.DialogResult.Yes
Add this property to Form1:
Private _myOtherForm As Form2
Public Property OtherForm() As Form2
Get
Return _myOtherForm
End Get
Set(ByVal value As Form2)
_myOtherForm = value
End Set
End Property
Add this property to Form2:
Private _myOtherForm As Form1
Public Property OtherForm() As Form1
Get
Return _myOtherForm
End Get
Set(ByVal value As Form1)
_myOtherForm = value
End Set
End Property
From each form, you will then be able to access all of the public properties and methods of the other form using the _myOtherForm variable.
If you were to call this from Form1, it will display the text in TextBox1 in Form2 and vice-versa:
MessageBox.Show(_myOtherForm.TextBox1.Text)
To close the current form, continue the loop and open the other form, add this to a button click event procedure:
Private Sub btnShowOtherForm_Click(sender As Object, e As EventArgs) Handles btnShowOtherForm.Click
Me.DialogResult = Windows.Forms.DialogResult.Yes
Me.Close()
End Sub
To close the current form, exit the loop and to cease showing any further forms, add this to a button click event procedure:
Private Sub btnStopShowingForms_Click(sender As Object, e As EventArgs) Handles btnStopShowingForms.Click
Me.DialogResult = Windows.Forms.DialogResult.No
Me.Close()
End Sub
These two procedures need to be added to each form.

How do I retain a form's control values when flipping between forms

It takes two forms to be able to enter all the info on a particular transaction. I want to be able to flip back and forth between these two forms retaining what was entered on each until the 'Save' button is clicked.
I think I should be able to use Form2.Show, Me.Hide and then Form1.Show, Me.Hide.
The first time I go to Form2 it goes thru the Form2 load event (that’s reasonable) but any knowledge of the control contents on Form1 have been lost. Even though Form1 is hidden (and not closed) the contents of its controls are gone. Why?
The second time I go to Form2 the load event does not fire because Form2 is hidden and at this point all the Form1 control contents are available. So, in flipping back and forth between Form1 and Form2 it works as I want it to after I go to Form2 the second time. But, I need it to work the first time and every time.
For days I’ve been trying to understand this. I’ve commented out nearly every line of code, stepped thru code, googled till I’m blue in the face (there has been a lot written about this), and I still can’t figure out why this behavior occurs.
Can anyone explain this phenomenon? Or better yet tell me what I need to do to make this work.
I have this code behind the Form1 button that goes to Form2
If Form2 Is Nothing Then
Dim Form2 As New Form2
End If
Form2.Show()
Me.Hide()
And this code behind the Form2 button to return to Form1
Form1.Show
Me.Hide
This is all you are probably missing:
Class Form1
Private f2 As Form2 ' this is Form1's reference to the
' form2 instance
Later when you click to go to form2, your original code just needs a small tweak:
If f2 Is Nothing Then
f2 = New Form2(Me) ' set declared variable to new instance
End If
F2.Show()
Me.Hide()
In this case Form1 is passing the reference using the trick you were shown before using the constructor:
Sub New(frm As Form1) ' this is in Form2 only
f1 = frm
End Sub
You dont need this in Form1 because he/it is creating his own f2 object reference.
The main problem in your original code, was: Dim Form2 As New Form2. You are creating a new Form2 each time (I suspect that resides in an event or sub). Those new instances can't know the control values in the previous instances. Declaring F1 or F2 as shown gives it module/form level Scope.
Dim declares a variable and its Type. f1 is of Type Form1. It does not create an object if it is an object variable
New creates an instance of an object Type (reference types). This directly relates to the Sub New method in the class. When you use New, Sub New is called so anything special that is needed can take place there. Value types like Integer do not need to be created or instanced, only declared.
Where you declare (Dim) a variable determines its Scope. If you do this in a Sub, the variable or object only exists in that sub. if you do it at the form/class level, it has Form/Class level scope.
I would try something like this. Display the 2 forms modally using the ShowDialog method.
In each form, create a property (call it "OtherForm") which points to an instance of the other form.
Continue the loop by setting the DialogResult of either form to Windows.Forms.DialogResult.Yes
Exit the loop by setting the DialogResult of either form to Windows.Forms.DialogResult.No
This will execute each forms load event each time it is displayed.
So call this from a button click event procedure on your so-called Form0.
Dim Form1 As New Form1
Dim Form2 As New Form2
Dim sFormToShow As String = "Form1"
Form1.OtherForm = Form2
Form1.DialogResult = Windows.Forms.DialogResult.Yes
Form2.OtherForm = Form1
Form2.DialogResult = Windows.Forms.DialogResult.Yes
Do
If sFormToShow = "Form1" Then
Form1.ShowDialog()
ElseIf sFormToShow = "Form2" Then
Form2.ShowDialog()
End If
sFormToShow = IIf(sFormToShow = "Form1", "Form2", "Form1")
Loop While Form1.DialogResult = Windows.Forms.DialogResult.Yes AndAlso Form2.DialogResult = Windows.Forms.DialogResult.Yes
Add this property to Form1:
Private _myOtherForm As Form2
Public Property OtherForm() As Form2
Get
Return _myOtherForm
End Get
Set(ByVal value As Form2)
_myOtherForm = value
End Set
End Property
Add this property to Form2:
Private _myOtherForm As Form1
Public Property OtherForm() As Form1
Get
Return _myOtherForm
End Get
Set(ByVal value As Form1)
_myOtherForm = value
End Set
End Property
From each form, you will then be able to access all of the public properties and methods of the other form using the _myOtherForm variable.
If you were to call this from Form1, it will display the text in TextBox1 in Form2 and vice-versa:
MessageBox.Show(_myOtherForm.TextBox1.Text)
To close the current form, continue the loop and open the other form, add this to a button click event procedure:
Private Sub btnShowOtherForm_Click(sender As Object, e As EventArgs) Handles btnShowOtherForm.Click
Me.DialogResult = Windows.Forms.DialogResult.Yes
Me.Close()
End Sub
To close the current form, exit the loop and to cease showing any further forms, add this to a button click event procedure:
Private Sub btnStopShowingForms_Click(sender As Object, e As EventArgs) Handles btnStopShowingForms.Click
Me.DialogResult = Windows.Forms.DialogResult.No
Me.Close()
End Sub
These two procedures need to be added to each form.

Open Form as ShowDialog But Close Initation Form

Is there any way to do the following, other than hiding then closing the hidden form later?
Mainform opens SecondForm as show dialog, i need to Open ThirdForm from SecondForm while closing SecondForm while keeping third form acting as "showdialog" on the MainForm?
When you show SecondForm(), pass in MainForm() as the owner to ShowDialog():
Public Class MainForm
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim sf As New SecondForm
If sf.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
' ... do some processing in here ...
End If
End Sub
End Class
Now, in SecondForm(), you can then set the owner of ThirdForm() to that of SecondForm():
Public Class SecondForm
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Hide()
Dim tf As New ThirdForm
tf.ShowDialog(Me.Owner)
Me.DialogResult = Windows.Forms.DialogResult.OK
End Sub
End Class
You could simply open the third form from the main form as soon as the second form returns a dialog result
You also might want to look at MDI this gives you more control over what the user can and can't do.
After trying Idle_mind suggestion it was still giving me problems going back and forth between forms continously showing them as .showdialog. I solved my problem just like tinstaafl suggested. I wish i would have got his message before a couple hours of trying different methods before coming up with this one.
When i close each form, i set a boolean flag in the main form. then i call a sub that is in the main form to show the next form as showdialog from the main form. i use the flag which triggers logic in the form im loading whether or not to bind data from datatable so i can edit it.
sorry with all those forms i know it gets confusing. to sum it up, close the dialog form (me.close), set flag so calling code knows what to do once the showdialog code is satisfied.

Closing winform from button on another winform. Form will not close

I have 2 forms on my application. I have the main form that opens TopMost, CenterScreen and Maximized. Then I have another form on this screen that pops open when I press a button. That second screen has a button that navigates to another screen, so when I press that button the second form closes, and the main form is suppose to close as well and the selected sheet open up.
However, the second screen closes fine, but my main screen remains open and active, while the called sheet opens but does not enable. I track down what was happening and the issue is that form all code runs, but the main screen does not seem to want to close. Here is my code:
Private Sub btnOpenDashboard_Click(sender As Object, e As EventArgs) Handles btnOpenDashboard.Click
Dim welcomeForm As New frmWelcomePage
If lblReportTitle.Text = "Employee Dashboard" Then
Me.Close() 'This works
welcomeForm.Close() 'This one remains open and active
Globals.dsbEmployeeBoard.Select() 'This one opens but is not enabled
End If
End Sub
I assume from your description that you already have a welcome form created and displayed before the form with the button is displayed.
This line of code:
"Dim welcomeForm As New frmWelcomePage"
is creating a new copy of the Welcome Page and closing it.
Instead of creating a new one, you need to reference the original one that is open.
If I recall correctly, you should be able to just remove that line and use frmWelcomPage.Close.
You need to pass a reference of your first form (Form1) to the second form (Form2), so that in the second form you can close the first form, like this:
Public Class Form2 Inherits Form
Private _form1 As Form1
Public Sub New(form1 As Form1)
Me.Form1 = form1
End Sub
End Class
Private Sub btnOpenDashboard_Click(sender As Object, e As EventArgs) Handles btnOpenDashboard.Click
If lblReportTitle.Text = "Employee Dashboard" Then
_form1.Close()
End If
End Sub
Then when you instantiate Form2, you would pass a reference to Form1, like this:
Dim form2 As New Form2(Me)
Note: Me is the instance of Form1.