Results 1 to 9 of 9

Thread: Access a piece of code from different forms - Global

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458

    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?
    Thanks

    Tomexx.

  2. #2
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    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...

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    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!

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458

    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:
    1. private const int WM_NCHITTEST = 0x84;
    2.         private const int HTCLIENT = 0x1;
    3.         private const int HTCAPTION = 0x2;
    4.  
    5.         protected override void WndProc(ref System.Windows.Forms.Message m)
    6.         {
    7.             switch (m.Msg)
    8.             {
    9.                 case WM_NCHITTEST:
    10.                     base.WndProc(ref m);
    11.                     if (m.Result.ToInt32() == HTCLIENT) m.Result = new IntPtr(HTCAPTION);
    12.                     break;
    13.  
    14.                 //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.
    15.                 default:
    16.                     //Make sure you pass unhandled messages back to the default message handler.
    17.                     base.WndProc(ref m);
    18.                     break;
    19.             }
    20.         }
    Thanks

    Tomexx.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458

    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?
    Thanks

    Tomexx.

  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    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.

  7. #7
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458

    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#)
    Thanks

    Tomexx.

  9. #9
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width