1

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
Cashion
  • 21
  • 4

1 Answers1

1

Assuming a string property is good enough for you, in VB.NET project, you can rely on My.Forms and get the form by name and show it:

Public Class MyButton
    Inherits Button
    Public Property Form As String
    Protected Overrides Sub OnClick(e As EventArgs)
        If (Not String.IsNullOrEmpty(Form)) Then
            Dim fp = My.Forms.GetType().GetProperty(Form)
            If (fp IsNot Nothing) Then
                Dim f = fp.GetValue(My.Forms)
                DirectCast(f, Form).ShowDialog()
            End If
        End If
        MyBase.OnClick(e)
    End Sub
End Class

If you are interested to have a better design-time support, take a look at this post:

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • Thanks a lot, get the form by name doesn't help. I can show the form but can't interact with the control properly because there are two instances of the same form. The one I created with String Property and the main instance of the form itself. – Cashion Apr 29 '19 at 15:28
  • It's easy to handle. You can check if `Application.OpenForms` contains the form with specified name, just show the open form, otherwise create a new instance of the form and show it. – Reza Aghaei Apr 29 '19 at 15:35
  • See my openForm Code, I have tried to check if the form is already opened by using Application.OpenForms. If it is opened, then bringtofront, if it doesn't then call frm.Show(). But it doesn't work cuz before this, I already opened the form by creating a new instance. and after calling openForm function, I want to access the form control like frmSupplier.txtSearch.Text = "SupplierCode". It just didn't work because the form I was showing is the new instance of frmSupplier, but my "SupplierCode" is assigned to the main frmSupplier instance. That's my conclusion, cmiiw... – Cashion Apr 29 '19 at 16:38