2016-12-16 74 views
0

我想知道是否可以使用vb.net或C#將類型字符串變量轉換爲windows.form。是否可以將字符串轉換爲vb.net中的窗體?

我使用的功能和子。

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對不起,我感到困惑。

「要形成的字符串」是已經存在的表單的名稱。

例子:frmListCustomer.vb

我存儲在數據庫菜單信息如下:

「BtnListCustomer |客戶| 0 |資源/ customers.png | frmListCustomers」

at:

BtnListCustome R =按鈕
顧客的名稱=按鈕標籤
0 =如果啓用或禁用
資源/ customers.png =所述
按鈕FrmListCustomers的圖標=時顯示的形式的名稱時 點擊按鈕

使用createButton()函數,我拆分這個字符串,安裝按鈕並在字典中保存按鈕名稱和表單名稱。

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

重點= 「btnListCustomer」
值= 「frmListCustomer」

起初我宣佈字典作爲(字符串,字符串)

對我來說,問題是這裏。我需要一種方法(如果有的話)將由「btnListCustomer」鍵引用的值「frmListCustomer」轉換爲表單類型。因此,我聲明的變量:

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

哪些應符合下列條件:

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() 

對不起,在解釋的混亂。這是我的第一篇文章。

+2

一個字符串到整個表單?字符串的格式是什麼?你如何期望將其轉換爲表單? – Carcigenicate

+2

你究竟在做什麼? – Plutonix

+0

我想你想要的是Form.Name或Form.Text屬性。哪些是字符串已經不需要鑄造 – tinstaafl

回答

3

我解決了使用反射。

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 
相關問題