Prevent tab page from changing

  • Thread starter Thread starter kosjanne
  • Start date Start date
K

kosjanne

I need to prevent user from changing a tab page, if the current tab
page has incorrect data. I've been searching these forums for a
solution and have found a suggestion that goes something like this:

[StructLayout(LayoutKind.Sequential)]
public struct NMHDR
{
public int hwndFrom;
public int idFrom;
public int code;
}

protected override void WndProc( ref Message m)
{
if ( m.Msg == 0x4e ) //WM_NOTIFY
{
NMHDR hdr = (NMHDR)Marshal.PtrToStructure(m.LParam, typeof(NMHDR));
if (hdr.code == -552)
{
...
m.Result = new IntPtr(1);
}
}
base.WndProc(ref m);
}

However, this doesn't work for me. The tab page is still changed even
though the m.Result is 1. Any ideas how to get this working? Is my
WndProc in correct place, when it is in the MainForm (which owns the
tab control)? Should it be somewhere else?

Thank you.
 
Yes, it's in the wrong place. It should be in the WndProc of an Inherited
TabControl.

Note that this will only prevent selection via mouse though. A user can
still change selection by using the Arrow keys as well as Ctrl+Tab and
Ctrl+Shift+Tab key combinations.

See the example on my site for a complete solution:
http://www.dotnetrix.co.uk/tabcontrols.html --> Add SelectedIndexChanging
Event.
 
Back
Top