Filed under

Visual Basic 6

 

Most of things require much lesser code in .NET. Some things NOT!!!

Intro | After a few years dealing with .NET I can say that I'm very please with the fact that I have been working with Microsoft Development tools in my carreer. And this includes starting from GW-Basic, moving to VB5 and 6 and now 7 and 8 and a little C#.
Less Code | Most of things require much lesser code to implement. The code is almost completely managed and there are much fewer chances of using third part components or WIN32 API to get thigs done.
However | There are some other things however that require more code than before. And the question is why? For instance, to detect if the user clicked on the X button of a window to close it was 5 lines of code, including the event declaration, in VB6:

[code language="vb"]
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    If UnloadMode = vbFormControlMenu Then
        Cancel = True
    End If
End Sub
[/code]

Now, the code in NET Framework 2 is the following, as David Kean indicated to me in this thread http://forums.microsoft.com/msdn/ShowPost.aspx?PostID=63488 (its in C#, but the same applies to VB8). It about 10 lines of code, without the brackets, which include a class level variable declaration:

[code language="c#"]
using System;
using System.ComponentModel;
using System.Security.Permissions;
using System.Windows.Forms;

namespace MyApplication
{
    public class Form1 : Form
    {
        private const int WM_SYSCOMMAND = 0x0112;
        private const int SC_CLOSE = 0xF060;
        private bool _UserClose;

        public Form1()
        {
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            if (_UserClose)
            {
                MessageBox.Show("The user clicked the X button.");
            }
            base.OnClosing(e);
        }

        [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_SYSCOMMAND && ((int)m.WParam & 65520) == SC_CLOSE)
            {
                _UserClose = true;
            }
            base.WndProc(ref m);
            _UserClose = false;
        }
    }
}
[/code]

Filed under  //   .NET   C#   Desktop   VB   Visual Basic 6   Windows Forms  

Comments [0]