求教:在C# WinForm程序中添加脚本控件,如何在脚本中像VB一样创建控件对象?

fengji 2005-08-08 11:49:05
控件是用VC.NET开发的

现在不想在窗体上直接加载,想像VB的CreateObject方式一样,在脚本中创建该控件

求教高手,怎样才能实现?!!
...全文
372 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
malingxian 2005-08-16
  • 打赏
  • 举报
回复
我想通过我做的方式应该可以捕获

但是通过CreateObject的方式我没做过。
fengji 2005-08-11
  • 打赏
  • 举报
回复
创建控件我解决了,Set Obj = CreateObject("ProgId.类名")
不过,没办法在脚本中捕获事件!
有没有高手做过类似的程序,能在脚本中捕获对象事件的!
KingSeaMountain 2005-08-09
  • 打赏
  • 举报
回复
可以,这就是用COM的方式来使用使用.NET类,用Regasm注册你的程序集就可以了。
fengji 2005-08-09
  • 打赏
  • 举报
回复
malingxian(马领先),你贴的代码是做脚本引擎的吧,用你的代码就能执行脚本

执行脚本我已经用微软提供的脚本控件。

本来的做法是:在应用程序中添加控件1,然后用微软的脚本控件的AddObject方法将控件1的对象添加到脚本控件中,这样我就能在脚本中直接调用控件1的方法了。

我现在想通过脚本中创建控件1,像在脚本中写如下代码:
Set Obj = CreateObject("Word.Application")
用脚本控件执行,就能创建Word对象

我想了解可不可以用CreateObject方法创建自己VC.NET写的控件对象呢?
malingxian 2005-08-09
  • 打赏
  • 举报
回复
上面类的用法(vb.net示例):
dim _HasCompiler as boolean = false
dim mcCompiler As cCompiler =New cCompiler
mcCompiler.CodeLanguage = mdlPublic.enuCodeLanguage.VisualBasic '设定为vb.net代码
dim strCode as string ="imports system" & vbCrLf & _
"imports Microsoft.VisualBasic" & vbCrLf & _
"namespace Test" & vbCrLf & _
" public class NewClass" & vbCrLf & _
" public shared sub main()" & vbCrLf & _
" MsgBox(""asdasd"")" & vbCrLf & _
" end sub" & vbCrLf & _
" end class" & vbCrLf & _
"end namespace"
Try
_HasCompiler = (Not mcCompiler.Compiler(strCode) Is Nothing)
Catch ex As Exception
MsgBox(ex.Message)
End Try
If _HasCompiler Then mcCompiler.Run() '运行

malingxian 2005-08-09
  • 打赏
  • 举报
回复
楼主说得是动态执行脚本吧?当然没问题啦,用codeDOM,CompileAssemblyFromDom
下面是我很早以前做的一个脚本执行程序,是 vb.net的,稍加修改就可用于C#:
Imports System.Reflection
Imports System.CodeDom
Imports System.CodeDom.Compiler
Imports Microsoft.CSharp
Imports System.Windows.Forms

Public Class cCompiler
#Region " 变量 "
Private _CodeLanguage As enuCodeLanguage = mdlPublic.enuCodeLanguage.Default
Private _lstErrors As ListView
Private _hasError As Boolean = False
Private _oAssembly As [Assembly]
Private _Type As Type

Private Provider As Object
Private oCompiler As ICodeCompiler
Private AsmNameString As String() = {"System.DLL", "System.Data.dll", "System.Windows.Forms.dll", "System.XML.dll", "Microsoft.VisualBasic.dll"}

Private oCompParams As CompilerParameters
#End Region

#Region " 属性 "
Friend Property CodeLanguage() As enuCodeLanguage
Get
Return _CodeLanguage
End Get
Set(ByVal Value As enuCodeLanguage)
_CodeLanguage = Value
End Set
End Property

Friend Property ListViewForErrors() As ListView
Get
Return _lstErrors
End Get
Set(ByVal Value As ListView)
_lstErrors = Value
End Set
End Property

Friend ReadOnly Property HasError() As Boolean
Get
Return _hasError
End Get
End Property

Friend ReadOnly Property OutAssembly() As [Assembly]
Get
Return _oAssembly
End Get
End Property

#End Region

#Region " 私有方法 "
Private Sub ListErrors(ByVal ErrorCollection As CompilerErrorCollection)
If _lstErrors Is Nothing Then Return
Dim i As Integer
_lstErrors.Items.Clear()
Dim pErr As CompilerError
For i = 0 To ErrorCollection.Count - 1
Dim pListItem As Windows.Forms.ListViewItem
pErr = ErrorCollection.Item(i)

If pErr.IsWarning Then
pListItem = _lstErrors.Items.Add((i + 1).ToString, 1)
Else
pListItem = _lstErrors.Items.Add((i + 1).ToString, 0)
End If
'pListItem.SubItems.Add(pErr.FileName)
pListItem.SubItems.Add(pErr.Line)
pListItem.SubItems.Add(pErr.Column)
pListItem.SubItems.Add(pErr.ErrorNumber)
pListItem.SubItems.Add(pErr.ErrorText)
Next

End Sub

Private Sub Init()
If _CodeLanguage = mdlPublic.enuCodeLanguage.CSharp Then
Provider = New Microsoft.CSharp.CSharpCodeProvider
Else
Provider = New Microsoft.VisualBasic.VBCodeProvider
End If
oCompiler = Provider.CreateCompiler()
oCompParams = New CompilerParameters(AsmNameString)
oCompParams.GenerateExecutable = False
oCompParams.GenerateInMemory = True
End Sub
#End Region

#Region " 公共方法 "
Friend Function Compiler(ByVal StrSourceCode As String) As [Assembly]
Dim oCodeUnit As CodeSnippetCompileUnit
Dim oCompResults As CompilerResults
oCodeUnit = New CodeSnippetCompileUnit(StrSourceCode)
oCompResults = oCompiler.CompileAssemblyFromDom(oCompParams, oCodeUnit) '开始编译

_oAssembly = Nothing
If oCompResults.Errors.HasErrors Then
ListErrors(oCompResults.Errors)
_hasError = True
Else
Try
_oAssembly = oCompResults.CompiledAssembly
Return _oAssembly
_hasError = False
Catch ex As Exception
'Throw ex
cMsgBox(ex.Message)
End Try
_lstErrors.Items.Clear()
End If
End Function

Friend Function Run(ByVal ParamArray strParams As String()) As Boolean
If _oAssembly Is Nothing Then Return False
Dim oType As Type
Dim sNameSpace, sTypeName As String
Dim oMeth As MethodInfo
For Each oType In _oAssembly.GetTypes

If oType.IsClass Then

'oMeth = oType.GetMethod("Main")
'If Not oMeth Is Nothing Then
sNameSpace = oType.Namespace
sTypeName = oType.Name
For Each oMeth In oType.GetMethods(BindingFlags.Public)
cMsgBox(oMeth.Name)
Next
Exit For
'End If
End If
Next
Try
If sTypeName <> "" Then
If sNameSpace <> "" Then sTypeName = sNameSpace & "." & sTypeName
Dim oObj As Object = _oAssembly.CreateInstance(sTypeName)
If strParams.Length > 0 Then oObj.main(strParams) Else oObj.main()
End If
Return True
Catch ex As Exception
Throw ex
Return False
End Try
End Function

Friend Function GetOutType(ByVal typeName As String) As Type
Try
If _oAssembly Is Nothing Then Return Nothing
Return _oAssembly.GetType(typeName)
Catch ex As Exception
Throw ex
Return Nothing
End Try
End Function

Friend Function GetOutTypes(ByVal typeName As String) As Type()
Try
If _oAssembly Is Nothing Then Return Nothing
Return _oAssembly.GetTypes
Catch ex As Exception
Throw ex
Return Nothing
End Try
End Function


Friend Function GetMethod(ByVal typeName As String, ByVal MethodName As String) As MethodInfo
Try
If _oAssembly Is Nothing Then Return Nothing
Dim oType As Type = _oAssembly.GetType(typeName)
Return oType.GetMethod("Main", BindingFlags.Public Or BindingFlags.InvokeMethod)
Catch ex As Exception
Throw ex
End Try
End Function

#End Region

#Region " 构造函数 "
Public Sub New()
Init()
End Sub

Public Sub New(ByVal CodeLanguage As enuCodeLanguage)
_CodeLanguage = CodeLanguage
Init()
End Sub
#End Region

End Class
fengji 2005-08-09
  • 打赏
  • 举报
回复
请高手关注,帮忙!
fengji 2005-08-08
  • 打赏
  • 举报
回复
明飞兄,你说的我不大明白!
objectType ObjV
Objv=New ObjectType
this.Controls.Add(Objv)
这些写在脚本里,去调用,不能创建控件对象吧

还有,用脚本动态加入事件,应该怎么做呢?有示例最好。谢谢
  • 打赏
  • 举报
回复
objectType ObjV
Objv=New ObjectType
this.Controls.Add(Objv)

这样仅仅在窗体上加入了控件,如果想动态加入事件,还得用脚本
TechEye 2005-08-08
  • 打赏
  • 举报
回复
pls refer to following..

Button[] btns = new Button[100];

for(int i=0;i<100;++i)
{
btns[i] = new Button();
this.Controls.Add(btns[i]);
}

110,568

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧