Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have third party control called TopicBar which looks like window xp navigation bar that you can see on the left side of a window

In my application I've 71 forms I have created a DB table to store all my forms Name and Text,A partcular user can add which forms(by searching form's name that I have already stored in a table) should add in topicbar as item.

For example, A form called FrmBill and its name is Bill once a user addes Bill to Topicbar it will successfully creates an item with name Bill,Now what I'm facing is I need to open the form FrmBill when that user clicks on Bill item in topicbar

I can get form name ie FrmBill is a user clicks in Bill but I couldnt open the form becuase I need to dynamically create instance or call Form here frmBill

what I have done so far

 Dim formName As String
    Dim frm As Form

Using conn As New SqlConnection(conn_str)
    conn.Open()
    Dim cmd As SqlCommand
    cmd = New SqlCommand("", conn)
    cmd.CommandText = "select top 1 formname from winforms where id=" & winformid & ""
    formName = cmd.ExecuteScalar
    'output: FrmBill
    frm = New Form
    frm.Name = formName '(FrmBill)

    With frm
        .MdiParent = FrmMain
        .Show()
        .Focus()
    End With

End Using

Note: in this function I can get form name ie FrmBill from the DB, but its not showing the actual form FrmBill

So is there anyways to declare form name like below

dim frm as new "& formname &"
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
145 views
Welcome To Ask or Share your Answers For Others

1 Answer

It's not so easy as "dim frm as new "& formname &"", but it can be done doing something like this:

Dim frmNewForm As Form = Nothing
Dim frmNewForm_Type As Type = Type.GetType("your_assembly.type_name")
frmNewForm = CType(Activator.CreateInstance(frmNewForm_Type), Form)

I mean, you have to store the Type of the class form.

Edit:

You can open a form so:

Dim oFr as New frmBill
oFr.Show()

ok? Well. You can open a Form so:

 Dim oFr as Form 
 oFr = New frmBill ' Because frmBill inherits System.Windows.Forms.Form class. All forms do it
 oFr.Show()

Here, frmBill its the Type of the class. You have no frmBill explicit reference, so you have to load it dinamically.

How can be do? Using a "NET mechanism" called Reflection. You can create a class dinamically, specifing the class Type.

Dim frmNewForm As Form = Nothing
Dim frmNewForm_Type As Type = Type.GetType("project_name.frmBill")
frmNewForm = CType(Activator.CreateInstance(frmNewForm_Type), Form)

What is Plutonix saying? Suppose you have a public "Print" method in frmBill.
With my answer, you can't do this:

frmNewForm.print()

Because you only can access to System.Windows.Forms.Form methods (close, show, showdialog...)

You can improve this, using a custom base class, or using Interfaces, or base abstract class... as you need. You can combine different ideas, depending on what you need. For example, you can combine an Interface with a Superclass:

public class frmMyForms
   inherits System.Windows.Forms.Form 

   public sub store_data ()  

....

public interface IInterface_common_methods

 sub print ()

...

public class frmBill
   inherits frmMyForms
   implements IInterface_common_methods

   public overloads sub store_data ()  
     msgbox ("store")
   end sub

 public sub print  ()  implements IInterface_common_methods.print
     msgbox ("print")
   end sub

Now, you could do things like:

Dim frmNewForm As frmMyForms= Nothing
Dim frmNewForm_Type As Type = Type.GetType("project_name.frmBill")
frmNewForm = CType(Activator.CreateInstance(frmNewForm_Type), frmMyForms)       

frmNewForm.Show()
frmNewForm.store_data() 
ctype(frmNewForm, IInterface_common_methods).Print() 

I don't know if this is wthat you're looking for, but I hope this can help you to learn more about NET possibilities.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...