2016-12-16 1 views
0

Я хотел бы знать, если возможно преобразовать переменную типа string в windows.form с помощью vb.net или C#.Возможно ли преобразовать строку в форму windows в 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"

, где:

BtnListCustome г = название кнопке
клиентов = надпись на кнопке
0 = если включить или отключить
Resources/customers.png = иконку из
кнопки FrmListCustomers = имя формы, которое отображается, когда вы нажмите кнопку

С помощью функции createButton() я разделил эту строку, установив кнопки и сохранив имя и название кнопки в словаре.

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

Key = "btnListCustomer"
Value = "frmListCustomer"

Сначала я объявляю словаря как (из строки, строки)

Проблема для меня здесь , Мне нужен способ (если есть) преобразовать значение «frmListCustomer», на которое ссылается ключ «btnListCustomer» на тип формы. Поэтому я объявил переменную:

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 
Смежные вопросы