How I can switch between Forms with VB? - vb.net

hi i have a vb application with two forms. With Form A I can start Form B and Form A is then visible=false. If I click on the red "X" on the right corner I want that form B close and Form A is visible true.
How I can do this?

You can setup something like this in FormB:
Private objFromForm As FormA
Sub New(FromForm As FormA)
InitializeComponent()
objFromForm = FromForm
End Sub
Private Sub FormB_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
objFromForm.Visible = True
End Sub
Basically, when FormA calls the constructor for FormB, it passes a reference to itself. Then, in FormB's "FormClosing" event, you can use that reference to make FormA visible again before FormB closes. Here's an example of this from FormA's side, with a button that makes a new instance of FormB visible and passes the reference to itself (FormA) to the FormB constructor:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim objNewForm As New FormB(Me)
objNewForm.Show()
Me.Visible = False
End Sub

Basically you can do like this ..
When calling FormB form FormA
Me.Visible = False
FormB.Showdialog
In FormB FormClosing event
FormA.Visible = True

Related

How to Open Form 2 from Form 1 and close Form 1 in vb.net

When I open Order Form from Dashboard Form, Order Form appears, Dashboard Form is hidden. But when I want to close it. Dashboard Form is still running. I need to manually stop it in vb.net. Anyone help!!
Public Class frmDashboard
Private Sub btnDashboard_Click(sender As Object, e As EventArgs) Handles btnDashboard.Click
Me.Show()
End Sub
Private Sub btnOrders_Click(sender As Object, e As EventArgs) Handles btnOrders.Click
frmOrders.Show()
Me.Close()
End Sub
Well, you shouldn't do that... but if it's really what you want...
On Form2, add something like that :
public Parent as Windows.Forms.Form
On Form1, when you open Form2, set Parent = Form1
Private Sub btnOrders_Click(sender As Object, e As EventArgs) Handles btnOrders.Click
Form2.Parent = Me
Form2.Show()
End Sub
So, on Form2, you can close Form1 when Form2 is closing.
Private Sub Form2_FormClosing(ByVal sender As Object, _
ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Parent.Close()
End Sub
But, as I wrote, I think you shouldn't do that... it's not a common behaviour.
Instead of closing Form1, I suggest you to refresh it / reload / make it visible again (by using the Parent variable)

How to enable parent form using ShowDialog in Vb.Net

Hi I've migrated VB6 code to VB.Net where in VB6 one of the functionality uses modal dialogue which allows user to copy few text from parent form. But in Vb.Net ShowDialog doesn't allow user to copy anything as you guys know it just disables the parent form.
My question is, Is there a way I can enable parent form or else minimize child form to copy few text from parent form?
please don't suggest to use show instead of ShowDialog because I want to achieve this only using ShowDialog.
This VB6 Code.
Form.Show vbModal, objParent
migration wizard has below code
Form.ShowDialog
The answer may be one of design, instead of a technical workaround to .ShowDialog(). Let's take your parent form, for example, with text that may be copied for pasting within a popup modal form. I don't know the data in your parent form, so let's call it a Widget.
Public Class Widget
Public Property ID As Integer = 0
Public Property TextThatMayBeCopied As String = String.Emtpy
End Class
In your parent form's code, you would load this data into a Widget object from a database, a file, whatever.
Private _widget As Widget = Nothing
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
' Assume we want the Widget with ID of 123
_widget = MyFunction.WhichLoadsWidgetDataAndReturnsWidgetObject(123)
DisplayData()
End Sub
Private Sub DisplayData()
txtID.Text = _widget.ID
txtTextThatMayBeCopied.Text = _widget.TextThatMayBeCopied
End Sub
Private Sub btnShowDialog_Click(sender As Object, e As EventArgs) Handles btnShowDialog.Click
_widget.TextThatMayBeCopied = txtTextThatMayBeCopied.Text.Trim
Dim f As New MyShowDialogForm(_widget)
f.ShowDialog
End Sub
Your target form MyShowDialogForm would take in it's own constructor an object of type Widget:
Private _widget As Widget = Nothing
Public Sub New(widget As Widget)
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
_widget = widget
End Sub
You can now access the data passed to form MyShowDialogForm via the _widget object, for example, in a button click event for btnCopyText, or however you need.
The key takeaway here is to use a method of exchanging data within different forms. Typically it becomes very messy code to use the Form classes themselves as the encapsulation for data. Instead, use classes for encapsulating data and moving it around your app.
'For example we have 2 form, form1 (main) and form2 (child)
'This Form as Main
Public Class Form1
Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
'if you don't have timer in child form, you must click again this button
'after you form back awhile to this form as main form
Form2.ShowDialog()
End Sub
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
Me.Close()
Me.Dispose()
End Sub
End Class
'This Form as Child
Public Class Form2
Private Sub btnToMainAWhile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnToMainAWhile.Click
'you hide this current form to caller form, you must back here again,
'if not this form always active in background
'or if you have timer1 here with enabled property =false here, you can add this:
'Timer1.Interval = 10000
'Timer1.Enabled = True
Me.Hide()
End Sub
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
Me.Close()
Me.Dispose()
End Sub
'if you have timer1 and you will wait for many seconds back to main form, add this:
'Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
' Timer1.Enabled = False
' Me.ShowDialog()
'End Sub
End Class

Reset form with original state

I would like to reset my form using vb.net i would try to below code but form is close can't open new form.
Private Sub resert_button_Click(sender As Object, e As EventArgs) Handles resert_button.Click
Dim client = New client_entry
client.Show()
Me.Close()
End Sub
Try Me.Hide and swap the order
Me.Hide()
client.Show()
There could be multiple ways to handle this one but myapproach would be like calling form_shown even where I will load required data and during reset i will call this event.
Private Sub Me_Shown(sender As Object, e As EventArgs) Handles Me.Shown
LoadData() 'This function/sub will load data for this form
End Sub
Private Sub resert_button_Click(sender As Object, e As EventArgs) Handles resert_button.Click
Me_Shown(sender,e)
End Sub
Use Me.Dispose() instead of Me.Close() it will dispose the form then when you call it again Yourform.Show(), It will Generate a new one.
One way of doing this would be to add a property to Form2. I'm assuming that you have two forms Lets call them Form1 and Form2. Somewhere in the code for Form1, you declare an instance of Form2 ..
Dim frm2 As New Form2
and at some point you want to show Form2 as a modal window ..
frm2.ShowDialog()
For the moment lets look at the Form2 code
I'm assuming here that you have a button to reset the form and to close the form, you just click on the form close button in the top right hand corner and maybe you have a button to close the form as well. Consider the following code for Form2
Public Class Form2
Friend Property resetOnClose As Boolean = False
Private Sub btnReset_Click(sender As Object, e As EventArgs) Handles btnReset.Click
resetOnClose = True
Me.Hide()
End Sub
Private Sub btnclose_Click(sender As Object, e As EventArgs) Handles btnclose.Click
resetOnClose = False
Me.Hide()
End Sub
End Class
There is a property called resetOnclose which is a Boolean type. If you click on the reset button, this property is set to True If you click on the close button, the resetOnClose property is set to false.
In all these bits of code, frm2 is Hidden - not closed. This means that the form and its resetOnclose property is still available to Form1. OK now to look at the Form1 code ..
Public Class Form1
Dim frm2 As New Form2
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Shown
frm2.ShowDialog()
Do
If frm2.resetOnClose Then
frm2.Close()
frm2 = New Form2
frm2.ShowDialog()
Else
frm2.Close()
End If
Loop Until frm2.resetOnClose = False
End Sub
End Class
For this example, frm2 is opened as soon as Form1 is shown, but you can put the relevant code anywhere you need
In the Form1.Shown code you will see a loop. The loop will continue looping as long as resetOnClose is True. When frm1 is shown modally, execution in this Form1 code waits until frm2 is hidden or closed. Next, the Form1 code checks to see if the resetOnClose property is true or false. If it's false, frm2 is closed and the loop terminates. If the property is true, frm2 is closed and reassigned a new instance of Form2 in its default state.
Voila!

calling one form from another when button is click

I am having two forms in vb where main form contains button and on button click event form2 will load.
I wrote this code:
Private Sub Button_Click_1(sender As Object, e As RoutedEventArgs) Handles newCustomer.Click
Dim d As BlankPage1 = New BlankPage1
d.show()
End Sub
where Blankpage1 is the name of second form. The problem is after clicking button, Blankpage1 doesn't appear.
Try BlankPage1.show() only, don't initialize first.
Private Sub Button_Click_1(sender As Object, e As RoutedEventArgs) Handles newCustomer.Click
form2.show() //name of the form
End Sub
Try like this :
Dim d As New BlankPage1
d.MdiParent = Me 'or the main form's name(MDI Parent Form)
d.StartPosition = FormStartPosition.CenterScreen
d.Show()
d.Focus()

Code linking a form back to previous form in Windows Forms

Having a problem linking my form3 back to form2 in windows forms. I want the "back" button to take me back to form 2 but it doesnt do so. I am trying form2.show() but it doesnt work.
My current form3 code:
Public Class Form3
Private Sub CheckedListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CheckedListBox1.SelectedIndexChanged
MessageBox.Show("Developer Succsessfully Added to Sprint", "Developer Added")
End Sub
Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
**Form2.Show()**
End Sub
End Class
What happening is most likely you already closed your form and can't open it again.
Assuming, you have both instances well and alive
In Form2 you should have
me.Hide()
Form3.Show()
And In Form3 you should have
me.Hide()
Form2.Show()
It can be something like this
shared sub Main
dim f2 as new Form2()
dim f3 as new Form3()
f2.Next = f3
f3.previous = f2
end sub
To link forms you creating properties, Next and Previous
And then use that as way to operate the form that should open
In form code do
private sub BtnNext_Click(....).....
Me.Hide()
Me.Next.Show()
End Sub
and the same way for the previous. If you have wizard, you could chain all your forms this way.
and of course, to accomplish this, minimum, you need an interface that contracts your forms to implement Properties Next and Previous or you can have a base class with implementation of the buttons and properties and then it will all work.
simple code redirect one form1 to form2 Using C#
Form2 f2=new Form2();
f2.show() OR f2.ShowDialog();