Can I get the event that Load a WinForm - vb.net

I have two WinForm from the first WinForm1 I call the other WinForm2 using a button with the well-known instanceWinForm2.ShowDialog. From the Load event in the second WinForm2 I want to know if the WinForm2 was called by the button in the WinForm1, because the WinForm2 can be called by other procedures.

Create a public boolean property on Form2, then call as follows:
Dim f as New Form2
f.CalledFromButton = True
f.Show
Then, check the property in the form load event of Form2, and it will be true when it was called from the button.

Related

How do I do stuff with my form objects from another module?

First of all, Why are forms classes?
But now on to the main question.
I have:
Form1.vb
Module1.vb
On the form there is a textbox, progress bar, labels etc.
I want to be able to change the properties of these objects on my form from module1, but I cant seem to access them.
These things on the form are objects, right? So do they have a certain scope? and how can I change it?
Wait but according to my solution explorer, these things are properties of a class??
But the form shows up when I run the program?? Wouldn't the form class have to be instantiated so that the form1 object is created?
Not that it matters, but here is a snippet from module1
Sub WriteObjectsToCSV()
Dim props As PropertyInfo() = MyCompanies(1).GetType().GetProperties()
Dim sw As StreamWriter =
My.Computer.FileSystem.OpenTextFileWriter(SaveAs, False)
Dim csv As New CsvHelper.CsvWriter(sw)
csv.WriteHeader(Of Company)()
csv.NextRecord()
For Each company In MyCompanies
'>>> want to write to my text box and change my progress bar here <<<
For Each prop In props
csv.WriteField(prop.GetValue(company))
Next
csv.NextRecord()
Next
End Sub
Forms are classes because they are created dynamically. You can instantiate and open the same form class serveral times and leave the instances open at the same time.
VB automatically instantiates the main form.
You can access the open forms through My.Application.OpenForms. The main form is always the first
Dim mainForm As Form1
mainForm = DirectCast(My.Application.OpenForms(0), Form1)
mainForm.txtOutput.Text = "hello"
To be able to access the controls of the form from outside, they must be declared Public or Internal. You can change the access modifier from the properties window (Modifiers property).
Instead, you can also declare a property in the form to make the text accessible outside
Public Property OutputText() As String
Get
Return txtOutput.Text
End Get
Set(ByVal value As String)
txtOutput.Text = value
End Set
End Property
Now you can write this in the module
mainForm.OutputText = "hello"

VB.NET Calling forms by Variable

I'm playing around in vb.net and would like to create a menu for my application. The application has an Main which is the MDI Parent and then has the sub forms that open up as the user browses the menu structure. I would like to track which form the user came from so that when they close their current form, it opens up the previous form they had open.
What I have so far is a public variable "FormTracking" which is a list of Form. The open form function works fine, but when they close the form and it calls the "OpenPreviousForm" function it fails as the previous form has been closed/disposed. How can i create a new instance of the form as calling "NEW" doesn't seem to work
Public FormTracking As New List(Of Form)
Public Sub OpenForm(theNewForm As Object)
'First Declare the new form
Dim f As Form = DirectCast(theNewForm, Form)
'Set the Forms parent
f.MdiParent = frmMain2
'Add the form to the tracking
FormTracking.Add(theNewForm)
'Show the form
f.Show()
End Sub
Public Sub OpenPreviousForm()
If modMenu.FormTracking.Count > 1 Then
Dim f As New Form
f = modMenu.FormTracking(modMenu.FormTracking.Count - 2)
'Set the Forms parent
f.MdiParent = frmMain2
'Remove the last item
modMenu.FormTracking.RemoveAt(modMenu.FormTracking.Count - 1)
'Show the form
f.Show()
Else
MessageBox.Show("Whoops, run out of forms.... ", "modMenu - OpenPreviousForm")
End If
End Sub
When opening a form it it calls:
modMenu.OpenForm(menupage_1)
me.close
And when closing a form, i try to call
modMenu.OpenPreviousForm()
The error i am getting when calling the above code is
"An unhandled exception of type 'System.ObjectDisposedExeption' occured in System.Windows.Forms.dll
Additional Information: Cannot access a disposed object
I understand i need to create the object again, but not sure how to using the functionality as above. Alternatively, if someone can present a better way of doing an application menu, please feel free to let me know.
Cheers
Use Me.Hide instead of Me.Close so you do not dispose the form.

Override button click event from another form

Is there a way to override a forms button click event from a parent form?
For example, here's kind of what I have going on...
Dim newForm As New FormB
newForm.someproperty1 = True
newForm.someprpoerty2 = False
newForm.Show(Me)
I need to know what button was clicked when newForm is closed. I would normally use 'ShowDialog', but I can't since newForm needs to open up a DIFFERENT form that can't open as a Dialog... long story short, without a complete rewrite of our entire software, i can't use the ShowDialog.
Anyways, is there anyway that I can add button click events from another form? Something like...
Dim newForm As New FormB
newForm.btn1.click = SomeNewFunctionOrSubroutine()
newForm.btn2.click = SomeOtherFunctionOrSub()
newForm.Show(Me)
I've been looking at events and eventhandlers, but they aren't making too much sense...
Any tips?
You could call AddHandler on your button in FormB (provided that its Modifier property is Public)
' This code is inside your Parent form
AddHandler newForm.btn1.click, AddressOf Me.SomeOtherFunctionOrSub
' This code is inside your Parent form
Protected Sub SomeOtherFunctionOrSub(sender as Object, e As EventArgs)
......
End Sub
Adding an Handler is a common practice to intercept the actions on a different form (For example, you probably will need also to intercept the Form.Closing event in case you want to have just one instance of newForm open at any time.)
An other approach (more complex) is subscribing to a custom event and requires the cooperation of FormB code. But in this scenario it doesn't seem to be necessary.

How to ensure only one instance of a child form is created by a parent mdi form in VB.NET

I have a button on a parent form that can create new Child forms.
However, I don't want more than one instance of each form to be created. I tried putting a public boolean on the parent MDI form:
Dim ChildForm As Boolean = False
And at the point where the child form is created:
ChildFormThere = True
And in the child form's "Leave" event, I thought I could do this:
Me.MdiParent.ChildFormThere = False
But it doesn't recognize the ChildFormThere variable... How can this be done then?
How about something like this. The idea is that if the form has already been created you switch to it, otherwise create one. This assumes you are setting mdiParent correctly when creating the child forms. This code would need to be run on the mdiParent, or have a reference to it to access the MdiChildren property.
For Each f In Me.MdiChildren
If TypeOf (f) Is Form1 Then
f.Show()
Exit Sub
End If
Next
Dim frm As New Form1
frm.Show()
Perhaps instead of:
dim ChildFormThere as Boolean = False ' Or True
You could do:
dim ChildForm as New ChildFormClass
' On Create Button Click:
ChildForm.Visible = True
This way it's always the same instance, so you simple have to manage if it is visible or not.

Passing Values from modal form to parent form vb.net

I am trying to pass information to parent form from modal form in vb.net winforms application.
1.) I created a copy of a form and displayed it using following code.
dim f=new frmParent()
f.show()
2.) Depending on conditions a button on frmParent opens a modal child form and asks for some informations. I used following code for that:
dim f = new ChildForm()
f.showDialog()
Both code works fine. When user press saves in child form i need to close childForm and use the user types values in parent form. I know how to close the childform but not sure how to pass info from childform to parent form.
Have a public property on your childForm
Public Property MyData As MyType
Then when you show the form you can do
dim f as new ChildForm()
If f.showDialog = DialogResult.OK Then
Data = f.MyData()
End if
If you need to allow them to be able to edit that data again then you might also want to consider passing in the Data to the constructor of the dialog.