简体   繁体   中英

Is it possible convert a string to windows form in vb.net?

I would like know if is possible convert a type string variable to windows.form using vb.net or C#.

The functions and sub that I used.

Function createButton(dynamicBtn As String) As BarButtonItem
    Dim propButton() As String = Split(dynamicButton, "|", 5) 'Divide data of BD to apply in buttons properties
    buttonCreated = New BarButtonItem With {.Name = propButton(0), .Caption = propButton(1), .Visibility = CType(propButton(2), BarItemVisibility), .LargeGlyph = Image.FromFile(String.Format("{0}\{1}", Application.StartupPath, propButton(3)))} 'Create new button with the properties of the BD
    formOfTarget.Add(propButton(0), "string to form") 'Variable type dictionay(of string, string) declared before to store the name of button and the name of form to de button. 
    AddHandler buttonCreated.ItemClick, AddressOf buttonCreated_itemClick
    Return buttonCreated
End Function

Private Sub buttonCreated_itemClick(sender As Object, e As ItemClickEventArgs)
    If formOfTarget.ContainsKey(CType(e, ItemClickEventArgs).Item.Name) Then
        Dim targetOfButton = formOfTarget.Item(CType(e, ItemClickEventArgs).Item.Name)'Here I need get the value of string in the dictionary thats contains the name of form previous created with all constrols and show
        formOfTarget.MdiParent = Me
        formOfTarget.Show()
        formOfTarget.BringToFront()
    End If
End Sub

@Enigmativity Sorry for the confusion.

The "string to form" is the name of the form that already exists.

Example: frmListCustomer.vb

I store the menu information in the DB as follows:

"BtnListCustomer | Customers | 0 | resources / customers.png | frmListCustomers"

at where:

BtnListCustomer = the name of the button
Customers = the button label
0 = if enable or disabled
Resources / customers.png = the icon of the
button FrmListCustomers = the name of the form that is shown when you click the button

With the createButton () function I split this string, mounting the buttons and saving the button name and form name in the dictionary.

FormOfTarget.Add ("btnListCustomer", "frmListCustomer")

Key = "btnListCustomer"
Value = "frmListCustomer"

At first I declare the dictionary as (Of string, string)

The problem for me is here. I need a way to (if any) convert the value "frmListCustomer" which is referenced by the "btnListCustomer" key to the form type. Therefore I declared the variable:

Dim targetOfButton = formOfTarget.Item (CType (and, ItemClickEventArgs) .Item.Name)

Which should correspond to:

Dim targetOfButton = frmListCustomer 'class of the form frmListCustomer.vb created in the project and so show it as triggered button  

TargetOfButton.mdi_parent = me 'frmListCustomer.mdi_parent = me
TargetOfButton.show () 'frmListCustomer.show ()

Sorry for the confusion in the explanation. It's my first post.

I solved using reflection.

Private Sub buttonCreated_itemClick(sender As Object, e As ItemClickEventArgs)  
        If formOfTarget.ContainsKey(CType(e, ItemClickEventArgs).Item.Name) Then  
             Try
            Dim targetOfButton = formOfTarget.Item(CType(e, ItemClickEventArgs).Item.Name)
            Dim formCreated As Type = Type.[GetType]("namespace." + targetOfButton)
            Dim showForm As Form = TryCast(Activator.CreateInstance(formCreated), Form)
            showForm.MdiParent = Me
            showForm.Show()
            showForm.BringToFront()
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
       End If  
    End Sub  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM