开发者

Is it possible to add a reference to some source code to include in a source file in vb.net, winforms?

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?


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.


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


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);
    }

}


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜