是否可以添加对某些源代码的引用以包含在 vb.net、winforms 的源文件中?

发布于 2024-08-18 18:42:51 字数 320 浏览 5 评论 0 原文

我不知道这叫什么,所以我一直在努力从谷歌中找到答案,但我对过去的日子有一个模糊的记忆。

我对大约 8 个框架控件进行了子类化(* 见下文),覆盖了一些属性并向每个控件添加了一些功能。 我所做的更改在每种情况下都是相同的。如果我做出更改,我必须浏览每个课程并在那里应用相同的更改。 我希望可能有一个关键字,例如 ,我可以将其放入每个类中。

有机会吗?

(* 注意)我使用术语子类,但我不这样做不知道这是否是正确的术语,我也看到过它用于回调是正确的术语吗?

I don't know what this is called so I've struggled to find an answer from google but I have a vague memory of it from t'old days.

I've sub-classed (* see below) about 8 framework controls, overriden some properties and added some functionality into each one.
The changes I have made are identical in every case. If I make a change, I have to go through each class and apply the same change there.
I was hoping there may be a keyword such as <IncludeSourcefile "common.vb> that I can put into each class.

Any chance?

(* note) I use the term sub-classed but I don't know if that's the correct terminology. I've also seen it used for call-backs. Is sub-classed the correct term to use?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

不离久伴 2024-08-25 18:42:51

我严重怀疑这对你有用。您不能使用包含文件将更改塞入派生控件中。您通常会在一个公共基类(例如从 Control 派生的基类)中进行更改,然后从该基类派生各个控件。但如果您定制了 8 个单独的控制类,那么这就行不通了。复制粘贴是你的职责。

I seriously doubt that's going to work out for you. You can't use an include file to jam the changes into the derived controls. You'd normally make the changes in a common base class, one derived from Control for example, then derive individual controls from that base class. But that's not going to work if you customized 8 separate control classes. Copy-and-paste is your lot.

伴梦长久 2024-08-25 18:42:51

这可能是一个疯狂的想法,但是......

  1. 使用您需要的代码创建一个 common.vb 文件。
  2. 为每个控件创建部分类文件,您将在其中添加示例代码。
  3. 创建一个批处理文件,将数据从 common.vb 复制到部分类中
  4. 创建一个执行该批处理文件的预构建步骤。

如果您需要在编译之前将代码复制到分部类,您始终可以运行批处理文件。

当然,这是假设 VB.NET 具有分部类的概念。不,我没有在家尝试过......;-D

This might be a crazy idea but...

  1. Create a common.vb file with the code you need.
  2. Create partial classes files for each of your controls where you will add the sample code.
  3. Create a batch file that copy the data from your common.vb into your partial classes
  4. Create a pre-building step that executes that batch file.

If you need the code copied to the partial classes before compilation, you can always run the batch file.

And that is of course assuming that VB.NET has the concept of partial classes. And NO, I didn't tried that at home... ;-D

書生途 2024-08-25 18:42:51

我记得在某处读到,微软做出了一个特定的设计决定(至少在 C# 中)不允许以这种方式包含外部源代码。

人们认为,虽然在 C++ 中包含源文件可能很有用,但它也会带来复杂性并降低可读性。

在面向对象语言中通常有很好的替代方案,最明显的是对象组合和委托。

以下是我过去处理这种情况的方法:

class CommonCode
{
    public void HandlePaint(Control c, PaintEventArgs a)
    {
        // Do shared code
    }
    public void HandleResize(Control c)
    {
        // Do shared code
    }

}

class MyButton : Button
{
    private CommonCode m_common=null;

    public MyButton()
    {
        m_common=new CommnCode();        
    }
    protected override OnPaint(PaintEventArgs a)
    {
        m_common.HandlePaint(this,a);
    }

    protected override void OnResize(EventArgs e)
    {
        m_common.HandleResize(this);
    }

}

class MyTextbox :Textbox
{

    private CommonCode m_common=null;

    public MyTextbox()
    {
        m_common=new CommnCode();        
    }
    protected override OnPaint(PaintEventArgs a)
    {
        m_common.HandlePaint(this,a);
    }

    protected override void OnResize(EventArgs e)
    {
        m_common.HandleResize(this);
    }

}

I remember reading somewhere that Microsoft made a specific design decision (at least in C#) not to allow including external source code in this way.

It was felt that while including source files could be useful in C++, it could also introduce complexity and reduce readability.

In an OO language there are usually good alternatives, the most obvious being object composition and delegation.

Here's how I've handled this situation in the past:

class CommonCode
{
    public void HandlePaint(Control c, PaintEventArgs a)
    {
        // Do shared code
    }
    public void HandleResize(Control c)
    {
        // Do shared code
    }

}

class MyButton : Button
{
    private CommonCode m_common=null;

    public MyButton()
    {
        m_common=new CommnCode();        
    }
    protected override OnPaint(PaintEventArgs a)
    {
        m_common.HandlePaint(this,a);
    }

    protected override void OnResize(EventArgs e)
    {
        m_common.HandleResize(this);
    }

}

class MyTextbox :Textbox
{

    private CommonCode m_common=null;

    public MyTextbox()
    {
        m_common=new CommnCode();        
    }
    protected override OnPaint(PaintEventArgs a)
    {
        m_common.HandlePaint(this,a);
    }

    protected override void OnResize(EventArgs e)
    {
        m_common.HandleResize(this);
    }

}
红玫瑰 2024-08-25 18:42:51

将公共类编译到它们自己的 DLL 中。

然后,通过右键单击“解决方案资源管理器”中的“引用”文件夹,在 Visual Studio 中设置 DLL 的引用。

最后,将Using语句添加到新DLL中命名空间的项目类中。

Compile your common classes into their own DLL.

Then set the reference for your DLL in Visual Studio by right-clicking on the References folder in the Solution Explorer.

Finally, add Using statements to your project's classes for the namespace(s) in your new DLL.

海之角 2024-08-25 18:42:51

您可以从 Visual Studio 创建一个类库项目(其输出将是 dll):

New Project -->类库

您可以将所有代码放在那里,然后只需将类库的引用添加到您想要使用它的项目中(在解决方案资源管理器上右键单击引用):

引用 -->添加参考 -->项目 --> MyClassLibrary

为了能够在项目选项卡中找到 MyClassLibrary,两个项目需要位于同一解决方案中 - 否则您可以直接“浏览”并指向 dll 本身。

然后在 .vb 文件中,您需要使用您的类,您可以执行 import MyClassLibrary:

// assuming the namespace is MyClassLibrary
imports MyClassLibrary

此时您可以只使用您的子类和控件。如果您进入MyClassLibrary.,intellisense 将向您显示所有课程。

You can create a class library project (which output will be a dll) from visual studio:

New Project --> Class Library

You can put all your code there then you can simply add a reference to the class library to the project you want to use it from (right click references on solution explorer):

References --> Add Reference --> Projects --> MyClassLibrary

To be able to find MyClassLibrary in the projects tab both projects need to be in the same solution - otherwise ou can just go 'browse' and point to the dll itself.

Then in the .vb file you need to use your classes you can do an import MyClassLibrary:

// assuming the namespace is MyClassLibrary
imports MyClassLibrary

At this point you can just use your subclasses and controls. If you go MyClassLibrary. intellisense will show you all your classes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文