Vb.net Using Public Sub to Open Instance of MDI Child - vb.net

I am trying lessen the line of code by creating a Public Sub calling in one line. However the form.MdiParent = Me generates error
frmParatemers is a Mdi Child form. frmMain is the MDI parent form.
From frmMain form
Dim MyCtrl As MenuClickOperations
MyCtrl.showChildDialog(New frmParameters)
This is my Class MenuClickOperations
Public Sub showChildDialog(ByVal form As Form)
Dim form2 As Form
For Each form2 In frmMain.MdiChildren
form2.Close()
Next
form.StartPosition = FormStartPosition.CenterScreen
form.MinimizeBox = False
form.MaximizeBox = False
form.MdiParent = Me
form.Show()
End Sub
Help me..Thanks

You should replace form.MdiParent = Me with form.MdiParent = formInstance. You can use frmMain, or the instance of frmMain (if different). If showChildDialog is called from frmMain, you can pass the frmMain instance as a parameter using Me in the call.

in frmMain
Dim MyCtrl As MenuClickOperations
MyCtrl.showChildDialog(New frmParameters, Me)
This is my Class MenuClickOperations
Public Sub showChildDialog(ByVal form As Form, ByVal Itself As Control)
Dim form2 As Form
For Each form2 In frmMain.MdiChildren
form2.Close()
Next
form.StartPosition = FormStartPosition.CenterScreen
form.MinimizeBox = False
form.MaximizeBox = False
form.MdiParent = Itself
form.Show()
End Sub

Related

To run code from both main function and class event

I'm calling a form from my main function, this form has a button on it; this form is defined inside its own class. My goal is that every time I click that button, the program will perform a task defined in the "click" event, but it will also perform a task in the main function at the moment of clicking that button.
I tried switching between oForm.Show(), but the main function runs the code before even starting the form. I also used oForm.Show(vbModeless), but this causes the opposite: the code in the main function runs after the form was closed.
This is my code:
Imports System.Windows.Forms
Public Sub Main()
Dim oForm As New WinForm()
oForm.Show(vbModeless)
' I want to show this when I click the button
MsgBox("TASK 2")
End Sub
Public Class WinForm
Inherits System.Windows.Forms.Form
Public Sub New()
oForm = Me
With oForm
.FormBorderStyle = FormBorderStyle.FixedToolWindow
.StartPosition = FormStartPosition.CenterScreen
.Width = 300
.Height = 400
.Text = "MYFORM"
.ShowInTaskbar = False
End With
Dim oButton1 As New Button()
With oButton1
.Text = "CLICK ME!"
.Top = oForm.Bottom - 80
.Left = (oForm.Width/2) - oButton1.Width
.Enabled = True
.AutoSize = True
End With
Me.Controls.Add(oButton1)
AddHandler oButton1.Click, AddressOf oButton1_Click
End Sub
Public Sub oButton1_Click(ByVal oSender As System.Object, ByVal oEventArgs As System.EventArgs)
MsgBox("TASK 1")
End Sub
End Class
How can I achieve this?
Thanks in advance

How to access parent form properties from a child form in vb.net

I have pretty much the same problem as described in this, but using VB.NET. There is a Form1 which is opened automatically as start window, so I cannot find the instance to use for accessing it. There is a Form2 opened from within Form1. I try to pass the instance of Form1 using keyword "Me":
Private Sub Button1_click(...) Handles Button1.Click
Dim childform as new Form2(Me)
childform.show()
End Sub
In Form2 I have:
Public Sub New(parentform As System.Windows.Forms.Form)
InitializeComponents()
MessageBox.Show(parentform.Button1.Text)
End Sub
Upon compiling I get the error: "Button1 is not a member of Form".
So how to pass the Form1 instance correctly to Form2?
Also I want to change some properties of the Button1 of Form1 from Form2. Button1 is declared in a Private Sub, will I nevertheless be able to access it from Form2 if I pass the instance correctly? If not, can I declaring a sub in Form1, e.g.
Public Shared Sub ChangeText(newtext As Sting)
Me.Button1.Text=newtext
End Sub
that will do the job?
I'm not 100% sure about what you are trying to achieve, but, you can pass data between forms. So for example you can do something like:
Public Class Form1
Private Sub Button1_click(...) Handles Button1.Click
Dim newForm2 as New Form2()
newForm2.stringText = ""
If newForm2.ShowDialog() = DialogResult.OK Then
Button1.Text = newForm2.stringText
End If
End Sub
End Class
And in Form2 you have
Public Class Form2
Dim stringText as string
Private Sub changeStringText()
'your method to change your data
Me.DialogResult = DialogResult.OK 'this will close form2
End Sub
End Class
I hope this is what you need, if not let me know
Thanks for your answer and comment. So I declared the wrong class for the parentform, means in Form2 it needs to be "parentform as Form1":
Public Sub New(parentform As Form1)
InitializeComponents()
MessageBox.Show(parentform.Button1.Text)
End Sub
and yes, I need to skip the "shared" in the ChangeText:
Public Sub ChangeText(newtext As Sting)
Me.Button1.Text=newtext
End Sub
This way it worked for me.

How to pass value of a label from one form to another form in VB.NET?

I am new in Vb.net. I'm still studying the logics in this language. I want to output data in a label.text from form 1 to form 2 with the use of a button. How can I do that while both forms are running?
PS. label.text may change value every time I click the button.
Here are two options.
Use a property setter
Use a method
Note: The code below assumes the following:
Form1: Button (name: Button1)
Form2: Label (name: Label1)
When the button is clicked on Form1, if Form2 isn't open, it opens it. Additionally, the value of the label on Form2 is set.
Option 1: (use a property setter)
Form1.vb
Public Class Form1
Dim counter As Integer = 0
Dim f2 As Form2 = Nothing
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If f2 Is Nothing Then
'create new instance
f2 = New Form2
'show form
f2.Show()
Else
'show window
'if window is minimized, it will "unminimize" it
'f2.WindowState = FormWindowState.Normal
'bring window into focus
'also brings the window to front
'f2.Activate()
End If
'set value
Dim username As String = String.Format("user{0}", counter)
'set property value
f2.Username = username
counter += 1
End Sub
End Class
Form2.vb
Public Class Form2
Dim _username As String = String.Empty
Public Property Username As String
Get
Return _username
End Get
Set(value As String)
_username = value
Label1.Text = value
Label1.Refresh()
End Set
End Property
End Class
Option 2: (use a method)
Form1.vb
Public Class Form1
Dim counter As Integer = 0
Dim f2 As Form2 = Nothing
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If f2 Is Nothing Then
'create new instance
f2 = New Form2
'show form
f2.Show()
Else
'show window
'if window is minimized, it will "unminimize" it
'f2.WindowState = FormWindowState.Normal
'bring window into focus
'also brings the window to front
'f2.Activate()
End If
'set value
Dim username As String = String.Format("user{0}", counter)
'set value using method
f2.SetLabelText(username)
counter += 1
End Sub
End Class
Form2.vb
Public Class Form2
Public Sub SetLabelText(ByVal username As String)
Label1.Text = username
Label1.Refresh()
End Sub
End Class
Resources:
How to: Create a Property (Visual Basic)

Why i can't pass data from form textbox to form textbox inside panel VB.NET

This is the code when calling form and showing inside panel
Dim frmLubesInterface As LubesInterface = New LubesInterface
with frmLubesInterface
.Text = "frmLubesInterface"
.TopLevel = False
Panel6.Controls.Add(frmLubesInterface)
.StartPosition =
.FormStartPosition.CenterScreen
.Show()
end with
This is code passing data from form and show inside form which is inside of panel
Dim Itemname as string = ""
Itemname = txtItemNameSearch.Text
LubesInterface.txtItem.Text = Itemname - **this part is where i pass the value of data to form textbox inside panel**
To summary i can't pass the value of textbox to form textbox inside the panel, but when showing it as msgbox it show the value.
I am not sure what you mean by "Global" that seems to mean something different to different people.
You could do it in one of two ways as far as I am concerned, you can either pass the values to constructor or create properties and get/set those properties.
Public Class Form1
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
Dim TxtFromTxtBoxOnForm2 As String = String.Empty
Dim Form2 As New Form2
With Form2
TxtFromTxtBoxOnForm2 = .ItmTxt
.TopLevel = False
.StartPosition = FormStartPosition.Manual
Panel1.Controls.Add(Form2)
.Show()
End With
End Sub
End Class
Public Class Form2
Public Property ItmTxt As String
Get
Return TextBoxOnForm2.Text
End Get
Set(value As String)
TextBoxOnForm2.Text = value
End Set
End Property
End Class
I already get it. i should declare global and call the form inside the panel.

Check if form is Opened

I give this question for more knowledge. How can I know if the form is Opened in my application or not, in order not to open it again I mean not to create an instance of the same form while it's running
Dim frmCollection As New FormCollection()
frmCollection = Application.OpenForms()
If frmCollection.Item("Form2").IsHandleCreated Then
MsgBox("Yes Opened")
Else
Dim f As New Form2()
With f
.Text = "form2"
.Show()
End With
End If
if I executes this code many times it will create more instances of the form Form2
How can I check if this form is not already opened
You can try it like this:
Imports System.Linq ' need to add
If Application.OpenForms().OfType(Of Form2).Any Then
MessageBox.Show("Opened")
Else
Dim f2 As New Form2
f2.Text = "form2"
f2.Show()
End If
You can use the following code:
If myForm.IsHandleCreated then
myForm is open
End If
As an extension of the answers given (thank you, all), here's a simple way to activate or show:
Dim frmCollection = System.Windows.Forms.Application.OpenForms
If frmCollection.OfType(Of Form2).Any Then
frmCollection.Item("Form2").Activate()
Else
Dim newForm2 = New Form2
newForm2.Show()
End If
For more simplicity you may create a public static bool variable which will tell whether the form is opened or not. On form load event assign 'true' and on closed event assign 'false' value.
ANOTHER refactoring way from the one initiated by HumbleBeginnings:
Dim xChildWindows = Application.OpenForms.OfType(Of frmForm2)
If xChildWindows.Any Then
xChildWindows.First().Focus() 'Focus if exists
Else
Dim xfrmNew As New frmForm2() 'Open window if doeasn't exists
xfrmNew.MdiParent = Me
xfrmNew.Show()
End If
Hate to be a kill joy but some day some one is going to try and understand your code.
Dim frm as New frmDontknow
Dim frmCollection = System.Windows.Forms.Application.OpenForms
For i As Int16 = 0I To frmCollection.Count - 1I
If frmCollection.Item(i).Name = frm.Name Then
frmCollection.Item(i).Activate()
Exit Sub
End If
Next i
Then do the show etc as required?
Check if form is Opened, To validate if a form is open we use this method and function to be able to invoke from any form and use less code.
Example :
This will use it in a form with mdiContainer and a panel object with 3 buttons that shows the 3 windows form.
Imports System
Imports System.Reflection
Private Sub OpenWindowsForm(ByVal FormName As String)
Dim instForm As Form = Application.OpenForms.OfType(Of Form)().Where(Function(frm) frm.Name = FormName).SingleOrDefault()
If instForm Is Nothing Then
Dim frm As New Form
frm = DirectCast(CreateObjectInstance(FormName), Form)
frm.MdiParent = Me
Me.Panel1.Controls.Add(frm)
Me.Panel1.Tag = frm
frm.Show()
Else
instForm.Select()
instForm.WindowState = FormWindowState.Maximized
instForm.BringToFront()
End If
End Sub
Public Function CreateObjectInstance(ByVal objectName As String) As Object
Dim obj As Object
Try
If objectName.LastIndexOf(".") = -1 Then
objectName = [Assembly].GetEntryAssembly.GetName.Name & "." & objectName
End If
obj = [Assembly].GetEntryAssembly.CreateInstance(objectName)
Catch ex As Exception
obj = Nothing
End Try
Return obj
End Function
How to use in click events
Private Sub btnRegistro_Click(sender As Object, e As EventArgs) Handles btnRegistro.Click
OpenWindowsForm("Registro")
End Sub
Private Sub btnBusqueda_Click(sender As Object, e As EventArgs) Handles btnBusqueda.Click
OpenWindowsForm("Busqueda")
End Sub
Private Sub btnCalendario_Click_1(sender As Object, e As EventArgs) Handles btnCalendario.Click
OpenWindowsForm("Calendario")
End Sub
Here is an image of the Sample code
you can try this
Dim formText As String
Dim prevText As String
Private Sub OpenForm(ByVal frm As Windows.Forms.Form)
formText = frm.Text
If formText = prevText Then Exit Sub
CloseForms()
' Make it a child of this MDI form before showing it.
frm.MdiParent = Me
frm.Show()
frm.Location = New Point(0, 0)
prevText = formText
End Sub
Private Sub CloseForms()
For Each ChildForm As Form In Me.MdiChildren
ChildForm.Close()
Next
End Sub
Private Sub NewToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PayablesToolStripMenuItem.Click
OpenForm(frmPayables)
End Sub
For Each frm As Form In Application.OpenForms
If frm.Name = Form1.Name Then
MessageBox.Show("Opened")
End If
Next