动态打开表单 [英] Dynamically Open form

查看:172
本文介绍了动态打开表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有称为TopicBar的第三方控件,它看起来像窗口左侧的窗口xp导航栏。



在我的应用程序中,我有71表单我创建了一个数据库表来存储我的所有表单名称文本,部分用户可以添加表单通过搜索我已经存储在表格中的表格的名称)应该添加到topicbar中作为项目。

例如,一个名为FrmBill的表单和它的名字是Bill曾经是一个用户addes Bill to Topicbar它将成功创建一个名称为Bill的项目,现在我所面临的是我需要在该用户单击主题栏中的Bill项目时打开表单FrmBill



<我可以得到表单名称,即FrmBill是一个用户点击比尔,但我无法打开表格,因为我需要动态创建实例或调用窗体在这里frmBill



我是什么到目前为止

  Dim formName As String 
Dim frm As Form

使用conn作为新的SqlCo nnection(conn_str)
conn.Open()
Dim cmd As SqlCommand
cmd = New SqlCommand(,conn)
cmd.CommandText =从winforms中选择top 1 formname where id =& winformid&
formName = cmd.ExecuteScalar
'输出:FrmBill
frm =新表单
frm.Name = formName'(FrmBill)

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

结束使用

注意:在这个函数中,我可以从DB获取表单名称,例如 FrmBill ,但是它的不显示实际的表格 FrmBill



那么是否有声明如下所示的表单名称?

  dim frm as new& formname& 


解决方案

&安培; formname&,但可以这样做:

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

我的意思是,你必须存储类型表格。



编辑:



您可以打开一个表单:

  Dim oFr as New frmBill 
oFr.Show()

好吗?好。您可以打开一个表单,如下所示:

  Dim oFr as Form 
oFr = New frmBill'因为frmBill继承了System.Windows .Forms.Form类。所有形式都做到这一点
oFr.Show()

这里,frmBill它的类型类。你没有frmBill明确的引用,所以你必须加载它。



怎么可以做?使用称为反射的NET机制。你可以创建一个类,指定类的类型。

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

什么是Plutonix说的?假设你在frmBill中有一个公共的Print方法。

用我的答案,你不能这样做:

  frmNewForm.print()

因为您只能访问System.Windows.Forms。表单方法(close,show,showdialog ...)

您可以使用自定义基类或使用Interfaces或基本抽象类...你需要。你可以结合不同的想法,这取决于你需要什么。例如,您可以将接口与超类组合:

  public class frmMyForms 
继承System.Windows.Forms。表格

public sub store_data()

....

  public interface IInterface_common_methods 

sub print()

...

 公共类frmBill 
继承frmMyForms
implements IInterface_common_methods

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

public sub print()实现IInterface_common_methods.print
msgbox(print)
end sub

现在,你可以这样做:

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

我不知道这是否是您正在寻找的wthat,但我希望这可以帮助您了解更多关于NET可能性的信息。


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 &"

解决方案

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.

这篇关于动态打开表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆