关于C#转VB.net的问题(急!!)
有如下代码:
public event PrintPageDelegate PrintPage
{
add
{
_printDocument.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(value);
_printPageValue = value;
}
remove
{
_printDocument.PrintPage -= new System.Drawing.Printing.PrintPageEventHandler(value);
_printPageValue = null;
}
}
把它变换成VB.net代码如下:
[code=VB.net#]
Public Custom Event PrintPage As PrintPageDelegate
AddHandler(ByVal value As PrintPageDelegate)
AddHandler _printDocument.PrintPage, AddressOf value
_printPageValue = value
End AddHandler
RemoveHandler(ByVal value As PrintPageDelegate)
RemoveHandler _printDocument.PrintPage, AddressOf value
_printPageValue = Nothing
End RemoveHandler
End Event
[/code]
总是报错:
错误 13 “AddressOf”操作数必须是某个方法的名称(不带圆括号)。
错误 11 缺少事件“PrintPage”的“RaiseEvent”定义。
请高手指点
--------------------编程问答--------------------
--------------------编程问答--------------------
Public Custom Event PrintPage As PrintPageDelegate
AddHandler(ByVal value As PrintPageDelegate)
AddHandler _printDocument.PrintPage, AddressOf value
_printPageValue = value
End AddHandler
RemoveHandler(ByVal value As PrintPageDelegate)
RemoveHandler _printDocument.PrintPage, AddressOf value
_printPageValue = Nothing
End RemoveHandler
End Event
Public Custom Event PrintPage As PrintPageDelegate
AddHandler(ByVal value As PrintPageDelegate)
AddHandler _printDocument.PrintPage, AddressOf value
_printPageValue = value
End AddHandler
RemoveHandler(ByVal value As PrintPageDelegate)
RemoveHandler _printDocument.PrintPage, AddressOf value
_printPageValue = Nothing
End RemoveHandler
End Event
--------------------编程问答-------------------- http://www.kamalpatel.net/ConvertCSharp2VB.aspx
http://labs.developerfusion.co.uk/convert/csharp-to-vb.aspx --------------------编程问答--------------------
--------------------编程问答-------------------- Public Custom Event PrintPage As PrintPageDelegate
Public Custom Event PrintPage As PrintPageDelegate
AddHandler(ByVal value As PrintPageDelegate)
AddHandler _printDocument.PrintPage, AddressOf value
_printPageValue = value
End AddHandler
RemoveHandler(ByVal value As PrintPageDelegate)
RemoveHandler _printDocument.PrintPage, AddressOf value
_printPageValue = Nothing
End RemoveHandler
End Event
AddHandler(ByVal value As PrintPageDelegate)
AddHandler _printDocument.PrintPage, AddressOf value
_printPageValue = value
End AddHandler
RemoveHandler(ByVal value As PrintPageDelegate)
RemoveHandler _printDocument.PrintPage, AddressOf value
_printPageValue = Nothing
End RemoveHandler
End Event --------------------编程问答-------------------- 请大家看清楚了再回,我写的代码跟各位的一样。真诚希望高手解决一下。 --------------------编程问答-------------------- 错误 13 “AddressOf”操作数必须是某个方法的名称(不带圆括号)。
这一句的意思是指 “AddressOf”后面跟的必须是方法,例如sub,你的value应该不是一个方法.
错误 11 缺少事件“PrintPage”的“RaiseEvent”定义。
这一句是因为控件_printDocument并没有“PrintPage”这个事件. --------------------编程问答-------------------- 关键是value
Public Custom Event PrintPage As PrintPageDelegate
AddHandler(ByVal value As PrintPageDelegate)
AddHandler _printDocument.PrintPage, AddressOf value
_printPageValue = value
End AddHandler
RemoveHandler(ByVal value As PrintPageDelegate)
RemoveHandler _printDocument.PrintPage, AddressOf value
_printPageValue = Nothing
End RemoveHandler
End Event
其实这段代码翻译是对应翻译了但是好像正确应用写法不符合实际情况。 --------------------编程问答--------------------
--------------------编程问答-------------------- vb.net?
private _printPageValue as PrintPageDelegate
Public Custom Event PrintPage As PrintPageDelegate
AddHandler(ByVal value As PrintPageDelegate)
_printPageValue = Directcast([Delegate].Combine(_printPageValue ,value ) , PrintPageDelegate )
End AddHandler
RemoveHandler(ByVal value As PrintPageDelegate)
_printPageValue = Directcast([Delegate].Remove(_printPageValue ,value ) , PrintPageDelegate )
End RemoveHandler
RaiseEvent(byval sender as object, byval e as PrintPageDelegate )
if _printPageValue isnot nothing then
_printPageValue.Invoke(sender,e)
end if
'或者使用下面的
if _printPageValue isnot nothing then
for each hander as PrintPageDelegate in _printPageValue.GetInvocationList()
handler.invoke(sender,e)
if e.cancel then Exit For '如果事件,支持取消,请使用这个, 不然,在多个事件中,可能发生 Cancel被重置的问题
Next
end if
end raiseevent
End Event
不懂,
帮顶 --------------------编程问答-------------------- 呵呵。我今天也遇到这样的问题。
楼主应该是用长江支流的GoldPrinter或MisGoldPrinter的代码吧。
我也是转化成VB.Net的。也是这样的问题。还有待研究。
---
因为GoldPrinter的WebPrinterPageSetting没有实现出来。所以想把那个接口去掉。改成直接可以用到winform的class就好了。接口麻烦。还有待研究。 --------------------编程问答-------------------- 哎,研究了一天。
直接把 AddHandler _printDocument.PrintPage, AddressOf MyDoc_printPage 去掉就好了。
------
在实例化中添加一个MyDoc_printPage(sender as object , e as System.Drawing.Printing.PrintPageEventArgs) Handles PrinterPageSetting1.PrintPage
PrinterPageSetting1就是你是实例对象。
--------
下面是GoldPrinter代码修改的。
''' <summary>
''' 一定要实例化此类后在调用打印/预览之前设置此属性,使之关联一个方法,目的是让具体的打印由实例化者来操作
''' 如PrinterPageSetting1.PrintPage += new PrintPageDelegate(this.PrintPageEventHandler);
''' </summary>
Public Property PrintPage() As PrintPageEventHandler Implements IPrinterPageSetting.PrintPage
'初始委托变量,关联方法
Get
Return _printPage
End Get
Set(ByVal value As PrintPageEventHandler)
_printPage = value
If _printDocument Is Nothing Then
Throw New Exception("关联的打印文档不能为空!")
End If
'在实例中添加这个PrintPageEventHandler:
'AddHandler _printDocument.PrintPage, AddressOf MyDoc_printPage
End Set
End Property --------------------编程问答-------------------- 对于你的代码:
你可以去掉这一句:AddHandler _printDocument.PrintPage, AddressOf value
在实例代码中添加:
private sub MyPrinter(sender as object , e as System.Drawing.Printing.PrintPageEventArgs) Handles PrinterPageSetting1.PrintPage
'添加你要打印代码。打印你的内容。
end sub
补充:.NET技术 , VB.NET