C#到Vb层次结构中的事件

编程入门 行业动态 更新时间:2024-10-21 03:58:29
本文介绍了C#到Vb层次结构中的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当vb类的父类用C#编写时,使用Handles vb语句会遇到一些问题. 假设我们有一个继承自System.Windows.Forms.Form的C#表单,并在其中粘贴了Protected控件.我们称之为CSharpForm 然后,在另一个Vb.Net项目中,我们继承它,并尝试Handle它的事件之一.

We are having some problems using the Handles vb statement when the parent class of the vb class is written in C#. Let''s suppose that we have a C# form that inherits from System.Windows.Forms.Form and has a Protected control pasted in it. Let''s call it CSharpForm Then in another Vb.Net project we inherit it and try to Handle one of its events.

Public Class VbForm Inherits CShapForm Protected Sub Control_Click (sender As Object, e As EventArgs) Handles Control.Click End Sub End Class

那里的Handles语句生成错误.

The Handles statement there is generating an error.

Handles clause requires a WithEvents variable defined in the containing type or one of its base types.

如果我们更改结构并从vb父类继承,则可以正常工作(这是因为父窗体中的WithEvents子句). 您会问:为什么要从C#表单继承Vb表单?".这是因为我们的框架是用C#编写的,但是我们的一位客户希望用Vb编写他的应用程序. 任何帮助将不胜感激. TIA

If we change our structure and inherit from a vb Parent Class it works fine (this is because of the WithEvents clause in the parent form). You would be asking, "Why do you inherit a Vb form from a C# form?". This is because our framework is written in C#, but one of our customers wants to have his application written in Vb. Any help will be very appreciated. T.I.A.

推荐答案

因此,我对此进行了一些尝试,并进行了一些研究,但看起来您将无法做到这一点.因为C#没有WithEvents关键字,所以您不能直接访问控件的事件.您应该能够执行AddHandler,或者真正应该做的是将事件添加到基本表单中. 因此,添加您自己的按钮单击"事件,或者您想要的任何类型的事件,并且只要该基本表单单击按钮,就引发该事件.然后,您将可以使用Handles关键字. ============ 摘自JSOP:还有另一个理由应将VB从已知的宇宙中驱逐出去. So, I''ve played around a bit with this and done a little research and it doesn''t look like you will be able to do that. Because C# doesn''t have a WithEvents keyword, you can''t access the control''s events directly. You should be able to do an AddHandler, or what you really should do is to add events to the base form. So, add you''re own Button Click event, or whatever type of event you want and whenever that baseform has a button click, raise that event. Then, you would be able to use the Handles keyword. ============= From JSOP: Yet another reason VB should be banished from the known universe.

更多推荐

C#到Vb层次结构中的事件

本文发布于:2023-11-29 16:49:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646994.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:层次   结构   事件   Vb

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!