I'm developing a winform project using VB.Net. In this project, I make a Custom Button inherits from UserControl and add a FormName property as Form. So that I can assign the FormName property with the form that the program will open when the custom button is clicked. The property is actually showing in Visual Studio Property Panel, but only shows the form itself as the sole option, and we cannot choose other forms from the same project.
Here is the code for that property
<Browsable(True), Description("Set the Form Name")> Public Property FormName As Form
Get
Return _formName
End Get
Set(ByVal value As Form)
_formName = value
End Set
End Property
I've tried to change the above property to String and type in the Form Name in string format. After that, I create a openForm function to open the form by receiving String as parameter, so basically getting string Form Name and create a new instance of the form. But at the end, I'm having problem to interact with the form's control like textbox, because there is two instances of a form. So i give up this idea.
Here is my code for openForm Function to open form from String Name :
Public Function openForm(ByVal frm As String, Optional ByVal focusCtrl As Control = Nothing, Optional ByVal isFullscreen As Boolean = True, Optional ByVal isDialog As Boolean = False) As Form
Dim obj As Form = TryCast(Activator.CreateInstance(Type.GetType("Management_System." + frm)), Form)
Dim myAnimator As New FormAnimator(obj, FormAnimator.AnimationTypes.Blend, fadingTime)
obj.StartPosition = FormStartPosition.CenterScreen
If isDialog Then
obj.ShowDialog()
focusCtrl.Focus()
Else
Dim frms = Application.OpenForms
Dim isOpened As Boolean = False
For Each q In frms
If q.GetType().Name = obj.Name Then
obj = CType(q, Form)
isOpened = True
Exit For
Else
isOpened = False
End If
Next
If isOpened = True Then
If isFullscreen Then
obj.WindowState = FormWindowState.Maximized
Else
obj.WindowState = FormWindowState.Normal
End If
If obj.Visible Then
obj.BringToFront()
Else
obj.Show()
End If
Else
obj.Show()
obj.BringToFront()
End If
End If
Return obj
End Function
The expected output is a Custom Control Property as Form that have options of all forms in the project.
Please help with this problem. Thanks in advance
Edit :
Private Sub Button_Click(sender As Object, e As EventArgs) Handles cbtnPurchase.Click, cbtnPurchaseReturn.Click, cbtnSales.Click, cbtnSalesReturn.Click, cbtnMutationIn.Click, cbtnMutationOut.Click, cbtnSwitchWarehouse.Click, cbtnOpname.Click, cbtnCakery.Click
Try
Dim btn As customButton = CType(sender, customButton)
If ExitForm Then Return
openForm(btn.FormName)
Catch ex As Exception
MsgTryCatch(ex.Message)
End Try
End Sub