How to find parent form name ? vb.net - vb.net

I want the name of the parent form in vb.net . Where I came from.
Like
`if parent-form.name = 'something' then
do something.
else
do something else.`
It's not a MDI form.
Edit:
I want exactly like that:
parentForm.vb:
chidform.showdialog()
In childform there is textbox
childForm.vb:
if parentForm is parentForm1 than fill textbox.text = 2
else fill textbox.text = 3
You will understand what I want.

Assumption: I worked under the assumption that this is an MDI application and you would want to read the name of the top parent and for reasons unknown you cannot refer to a singleton reference to the form (for instance current form runs from a library).
I wrote an extension mode to get the top parent since the immediate parent of an MDI child window is an MDIClient without a name.
<Extension>
Public Function GetTopParent(currentControl As Control) As Control
Dim parent As Control = Nothing
Do While currentControl IsNot Nothing
parent = If(currentControl.Parent, parent)
currentControl = currentControl.Parent
Loop
Return parent
End Function
Then when you need the name of the parent you can do the following.
MessageBox.Show(Me.GetTopParent().Name) ' This just shows the name but you can do your comparison here.

Related

MS Access 2013, Subform operations destroy the class module

I have a form in my Access project called MainForm, there is a sub form called subForm, also many buttons on the MainForm, at the same time, I created a class module to handle the OnClick event for all the buttons and the module name is classButtons.
Code in the class module:
Public WithEvents cButtons as Access.CommandButton
Dim tmpValue as String
Private Sub cButtons_Click()
Select Case cButton.Name
Case "ButtonA"
MainForm.subForm.Requery
Case "ButtonB"
Let tmpValue = subForm.ComboBox1.Value
DoCmd.RunSQL "update sometable set somefield='" & tmpValue & "'"
Case "ButtonC"
DoCmd.RunCommand acCmdUnhideColumns
End Select
End Sub
In the Open event of the MainForm, I have the following code:
For i = 0 to Me.Controls.Count - 1
If Left(Me.Controls(i).Name,6) = "cmdbtn" Then
set btnClass = New classButtons
set btnClass.cButtons = Me.Controls(i)
btnClass.cButtons.OnClick = "[Event Procedure]"
mdPublic.buttonColl.Add btnClass 'buttonColl is a collection variable declared in another module called "mdPublic"
End If
Next
Then once the MainForm is opened, all the 3 buttons works well, but once ButtonA or ButtonB is clicked, all the 3 buttons will stop working.
I tried to remove the subForm operations from ButtonA and ButtonB, and found that the problem is disappeared, so I guess the subForm operations just "destroy" the class module.
But I do need those operations, anyone has any ideas? Thank you !!!!!
What I do is make a function behind the form to handle the button behaviors, call it HandleButtonClick(x As String), then in each button Click event property:
=HandleButtonClick("ButtonA") -- change the button designation as appropriate
Also, I always name a subform container different from the subform it holds, such as ctrDetails. Code behind main form must reference the subform container name.

How to check MDI child form is already opened

i have an application that contain MDI parent and child form. I have to show the different type of data in grid. so i created a common form with grid and pass the dataset. now how do i check the specific data form is already opened or not. here my code.
Public Function FindMdiChild(ByVal Title As String) As Boolean
If Application.OpenForms.Count = 0 Then Exit Function
For Each t As Form In Application.OpenForms
If t.Text.ToString = Title.ToString Then
t.Activate()
Return True
End If
Next
End Function
The problem is some times its stuck on this specific line "If t.Text.ToString = Title.ToString Then".
please help me to resole this issue.

VB.NET Call Sub of another form

I know, that there are many questions related to this, but still I cannot find a workable solution.
Usually, it would work like this: A form creates an instance of another form in it's container like this:
Dim PolInstIn As New SubForm1
Private Sub LoadDetail()
PolInstIn.TopLevel = False
PolInstIn.Name = "Sub From"
PolInstIn.FormBorderStyle = Windows.Forms.FormBorderStyle.None
PolInstIn.Dock = DockStyle.Fill
Me.GroupBox6.Controls.Add(PolInstIn)
PolInstIn.Show()
End Sub
Then it's easy to call a Public Sub from the sub form like this:
Call PolInstIn.MyPublicSubInSubForm1()
However, this doesn't work for me in this case. When I run MyPublicSubInSubForm1() it doesn't throw any error, but does no action. If I write a value to SubForm1 textbox and read it back, it reads, but I don't see it on the screen, so I suspect it is written to some other accidental instance.
I suspect it is because my parent form is also an instance of an form created in very similar way like SubForm1. Basically the ParentForm is a form loaded into tabPage and SubForm1 is a module loaded into ParentForm. It can exist in many copies (tabs).
Could you point to any simple solutions?
Regards,
Libor
I see this question got a lot of views, so here is an answer.
1) No visual response of child form (only results) - this could have happened if I created more then 1 instances of the form. The example is just an example, but if one use it (accidentally) this way, it may result in new definition of a child form every time (and consequent symptoms like the ones described). In practice, I split form loading from loading data into to the form (done by a public sub in that form).
2) If you want also a back reference (to i.e. parent grid form), define a Public ParentFormGrid as GridName (note ParentForm is a reserved name) and on loading a child form, set
PollInstIn.ParentFormGrid = Me
This way you can alway asccess the parent form, i.e. reload the grid when you save changes on a line edited in child form.
make Private Sub LoadDetail() to a public :
Public Sub LoadDetail()
It work on my project. Hopely its what you want
Dim PolInstIn As New SubForm1
Private Sub LoadDetail()
PolInstIn.Name = "Sub From"
PolInstIn.Show()
PolInstIn.TopLevel = False
PolInstIn.FormBorderStyle = Windows.Forms.FormBorderStyle.None
PolInstIn.Dock = DockStyle.Fill
PolInstIn.Update()
PolInstIn.Refresh()
Me.GroupBox6.Controls.Add(PolInstIn)
End Sub

Passing the data between windows forms

I have a windows application.
In which I want to call one form from another and take Yes/No option from user
and that Yes/No choice again passed to the parent form.
How to do this ?
I have tried by creating object but it dont work.
Please check below image...
I have Call conformation form on click of final button, when user choose ok/Cancel that value passed to the again parent form and will take desired action depends on choice.
how to take input from this child form ?
The easiest way is to set a variable to Public when you instantiate it.
Public myVariable as String = ""
Then you would access it from anywhere.
From your own form
Me.myVariable = "" 'Whatever you would like to set
From another Form
Form1.myVariable = "" 'Whatever you would like to set
'or
FormName.Variable = ""
Dim confirmModal = new ConfirmModal
Dim result = confirmModal.ShowDialog()
If result = OK then resltValue = confirmModal.ResultValue
ResultValue is whatever you want to pass to the parent.
You can use a public variable in a module or in the parent form that is accessed by both forms to store the yes/no value.
public returnCode as boolean
If the variable is in the parent form, it can be referenced in the child form using the parent form's name:
form1.returnCode = True

Pass variable From child to parent, Like a Function VB.NET

I have created 2 forms (Parent & Child), i want to store the unique textbox value to a variable into the parent form.
Also like this:
Parent Code:
dim passed_value = new childform()
passed_value.show()
On close:
refresh passed_value variable using childform textbox value.
You can Do the Following to accomplish the Task :
Declare a String variable in the child form.
Public value As String
Use ShowDialog() in the Main Form to show the Child form.
Dim frm As New Form2
frm.ShowDialog()
[Set the value in your form as per your needs]
value = "New Value"
Now get the value variable from the Child form and set the textbox text according to it.
TextBox1.Text = frm.value
I would make it a property and give it the necessary access, which to me looks to be ReadOnly.
Private _myValue As DataType
Public ReadOnly Property MyValue() As DataType
Get
Return _myValue
End Get
You could access it as such: myForm.MyValue.