Access a piece of code from different forms - Global
In VB6 we had modules and where we would put functions that would be global across all forms etc.
How would I do that in C#.NET (or VB.NET)?
I have a bunch of borderless forms. One of those forms has and piece of code that moves a borderless form and I'd like to reuse the code in the rest of the forms. Where do I put it?
Re: Access a piece of code from different forms - Global
You still have modules... there is an "Add Module" item in the list when you right click your project in solution explorer...
Re: Access a piece of code from different forms - Global
Is it just a bunch of logic that moves another object, and the object happens to be a borderless form? If so, store it in a class library!
Re: Access a piece of code from different forms - Global
OK, didn't know that, however the code I was thinking about was one to drag a borderless form and I think that has to stay in a form itself?
VB Code:
private const int WM_NCHITTEST = 0x84;
private const int HTCLIENT = 0x1;
private const int HTCAPTION = 0x2;
protected override void WndProc(ref System.Windows.Forms.Message m)
{
switch (m.Msg)
{
case WM_NCHITTEST:
base.WndProc(ref m);
if (m.Result.ToInt32() == HTCLIENT) m.Result = new IntPtr(HTCAPTION);
break;
//If m.Result.ToInt32 = HTCLIENT Then m.Result = IntPtr.op_Explicit(HTCAPTION) 'Try this in VS.NET 2002/2003 if the latter line of code doesn't do it... thx to Suhas for the tip.
default:
//Make sure you pass unhandled messages back to the default message handler.
base.WndProc(ref m);
break;
}
}
Re: Access a piece of code from different forms - Global
Hi Mendhak,
Sorry didn't see your response.
So how would I store in a class and then how would I call the WndProc() in every form that I wanted to move?
Re: Access a piece of code from different forms - Global
Since WndProc is a method of the Form class, you should keep that function call within the form itself, but pass on the Message object to a global function which executes exactly that code you have up there.
Which means, keep the "protected override void WndProc" but have it call a global function.
Re: Access a piece of code from different forms - Global
you could also make a master form that has all the "global" functionality you need, and have all the forms that use it inherit from that form instead of inheriting directly from Windows.Forms.Form
Re: Access a piece of code from different forms - Global
Hi kleinma,
The master form idea sounds good, but not sure how I would go about inheriting from the main form...
I'm very new to the .NET (C#)
Re: Access a piece of code from different forms - Global
In your C# form, there is a line (assuming your form name is form1)
public partial class Form1 : Form
the ": Form" part of that tells it to inherit from the standard windows form. All you have to do is change "Form" to the name of the master form that you want the child to inherit from. Since the master form inherits from form directly, your child form will get all the properties of Form, plus whatever is in your master Form1.. plus whatever individual functionality the child is given by you.